blob: ac701e6c8b1aefb546c50affb9285f21b3003775 [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;
90 int caps:1;
91 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070092 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040093 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -080094 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040095 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070096 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080097 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040098 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -070099 int remount_proc_ro:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400100 int usergroups:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700101 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400102 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700103 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400104 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800105 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700106 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800107 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800108 int pid_file:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800109 int alt_syscall:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400110 } flags;
111 uid_t uid;
112 gid_t gid;
113 gid_t usergid;
114 char *user;
115 uint64_t caps;
116 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700117 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700118 int netns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800119 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400120 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800121 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800122 char *uidmap;
123 char *gidmap;
Andrew Brestickereac28942015-11-11 16:04:46 -0800124 char *alt_syscall_table;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800125 struct sock_fprog *filter_prog;
Dylan Reid648b2202015-10-23 00:50:00 -0700126 struct mountpoint *mounts_head;
127 struct mountpoint *mounts_tail;
128 int mounts_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500129};
130
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700131/*
132 * Strip out flags meant for the parent.
133 * We keep things that are not inherited across execve(2) (e.g. capabilities),
134 * or are easier to set after execve(2) (e.g. seccomp filters).
135 */
136void minijail_preenter(struct minijail *j)
137{
138 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700139 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700140 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700141 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800142 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800143 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700144}
145
146/*
147 * Strip out flags meant for the child.
148 * We keep things that are inherited across execve(2).
149 */
150void minijail_preexec(struct minijail *j)
151{
152 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700153 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700154 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800155 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700156 if (j->user)
157 free(j->user);
158 j->user = NULL;
159 memset(&j->flags, 0, sizeof(j->flags));
160 /* Now restore anything we meant to keep. */
161 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700162 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700163 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800164 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700165 /* Note, |pids| will already have been used before this call. */
166}
167
168/* Minijail API. */
169
Will Drewry6ac91122011-10-21 16:38:58 -0500170struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400171{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400172 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400173}
174
Will Drewry6ac91122011-10-21 16:38:58 -0500175void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400176{
177 if (uid == 0)
178 die("useless change to uid 0");
179 j->uid = uid;
180 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400181}
182
Will Drewry6ac91122011-10-21 16:38:58 -0500183void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400184{
185 if (gid == 0)
186 die("useless change to gid 0");
187 j->gid = gid;
188 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400189}
190
Will Drewry6ac91122011-10-21 16:38:58 -0500191int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400192{
193 char *buf = NULL;
194 struct passwd pw;
195 struct passwd *ppw = NULL;
196 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
197 if (sz == -1)
198 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400199
Elly Jonesdd3e8512012-01-23 15:13:38 -0500200 /*
201 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400202 * the maximum needed size of the buffer, so we don't have to search.
203 */
204 buf = malloc(sz);
205 if (!buf)
206 return -ENOMEM;
207 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500208 /*
209 * We're safe to free the buffer here. The strings inside pw point
210 * inside buf, but we don't use any of them; this leaves the pointers
211 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
212 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400213 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700214 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400215 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700216 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400217 minijail_change_uid(j, ppw->pw_uid);
218 j->user = strdup(user);
219 if (!j->user)
220 return -ENOMEM;
221 j->usergid = ppw->pw_gid;
222 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400223}
224
Will Drewry6ac91122011-10-21 16:38:58 -0500225int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400226{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700227 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700228 struct group gr;
229 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400230 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
231 if (sz == -1)
232 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400233
Elly Jonesdd3e8512012-01-23 15:13:38 -0500234 /*
235 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400236 * the maximum needed size of the buffer, so we don't have to search.
237 */
238 buf = malloc(sz);
239 if (!buf)
240 return -ENOMEM;
241 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500242 /*
243 * We're safe to free the buffer here. The strings inside gr point
244 * inside buf, but we don't use any of them; this leaves the pointers
245 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
246 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400247 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700248 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400249 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700250 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400251 minijail_change_gid(j, pgr->gr_gid);
252 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400253}
254
Will Drewry6ac91122011-10-21 16:38:58 -0500255void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400256{
257 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400258}
259
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700260void API minijail_no_new_privs(struct minijail *j)
261{
262 j->flags.no_new_privs = 1;
263}
264
Will Drewry6ac91122011-10-21 16:38:58 -0500265void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400266{
267 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500268}
269
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700270void API minijail_log_seccomp_filter_failures(struct minijail *j)
271{
272 j->flags.log_seccomp_filter = 1;
273}
274
Will Drewry6ac91122011-10-21 16:38:58 -0500275void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400276{
277 j->caps = capmask;
278 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400279}
280
Will Drewry6ac91122011-10-21 16:38:58 -0500281void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400282{
283 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400284}
285
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700286void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
287{
288 int ns_fd = open(ns_path, O_RDONLY);
289 if (ns_fd < 0) {
290 pdie("failed to open namespace '%s'", ns_path);
291 }
292 j->mountns_fd = ns_fd;
293 j->flags.enter_vfs = 1;
294}
295
Will Drewry6ac91122011-10-21 16:38:58 -0500296void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400297{
Elly Jonese58176c2012-01-23 11:46:17 -0500298 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700299 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400300 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800301 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400302}
303
Dylan Reidf7942472015-11-18 17:55:26 -0800304void API minijail_namespace_ipc(struct minijail *j)
305{
306 j->flags.ipc = 1;
307}
308
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400309void API minijail_namespace_net(struct minijail *j)
310{
311 j->flags.net = 1;
312}
313
Dylan Reid1102f5a2015-09-15 11:52:20 -0700314void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
315{
316 int ns_fd = open(ns_path, O_RDONLY);
317 if (ns_fd < 0) {
318 pdie("failed to open namespace '%s'", ns_path);
319 }
320 j->netns_fd = ns_fd;
321 j->flags.enter_net = 1;
322}
323
Dylan Reid791f5772015-09-14 20:02:42 -0700324void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400325{
326 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700327 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400328}
329
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800330void API minijail_namespace_user(struct minijail *j)
331{
332 j->flags.userns = 1;
333}
334
335int API minijail_uidmap(struct minijail *j, const char *uidmap)
336{
337 j->uidmap = strdup(uidmap);
338 if (!j->uidmap)
339 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800340 char *ch;
341 for (ch = j->uidmap; *ch; ch++) {
342 if (*ch == ',')
343 *ch = '\n';
344 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800345 return 0;
346}
347
348int API minijail_gidmap(struct minijail *j, const char *gidmap)
349{
350 j->gidmap = strdup(gidmap);
351 if (!j->gidmap)
352 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800353 char *ch;
354 for (ch = j->gidmap; *ch; ch++) {
355 if (*ch == ',')
356 *ch = '\n';
357 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800358 return 0;
359}
360
Will Drewry6ac91122011-10-21 16:38:58 -0500361void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400362{
363 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400364}
365
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800366void API minijail_run_as_init(struct minijail *j)
367{
368 /*
369 * Since the jailed program will become 'init' in the new PID namespace,
370 * Minijail does not need to fork an 'init' process.
371 */
372 j->flags.do_init = 0;
373}
374
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700375int API minijail_enter_chroot(struct minijail *j, const char *dir)
376{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400377 if (j->chrootdir)
378 return -EINVAL;
379 j->chrootdir = strdup(dir);
380 if (!j->chrootdir)
381 return -ENOMEM;
382 j->flags.chroot = 1;
383 return 0;
384}
385
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800386int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
387{
388 if (j->chrootdir)
389 return -EINVAL;
390 j->chrootdir = strdup(dir);
391 if (!j->chrootdir)
392 return -ENOMEM;
393 j->flags.pivot_root = 1;
394 return 0;
395}
396
Dylan Reida14e08d2015-10-22 21:05:29 -0700397static char *append_external_path(const char *external_path,
398 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700399{
Dylan Reida14e08d2015-10-22 21:05:29 -0700400 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700401 size_t pathlen;
402
Dylan Reid08946cc2015-09-16 19:10:57 -0700403 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700404 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
405 path = malloc(pathlen);
406 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700407
Dylan Reida14e08d2015-10-22 21:05:29 -0700408 return path;
409}
410
411char API *minijail_get_original_path(struct minijail *j,
412 const char *path_inside_chroot)
413{
Dylan Reid648b2202015-10-23 00:50:00 -0700414 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700415
Dylan Reid648b2202015-10-23 00:50:00 -0700416 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700417 while (b) {
418 /*
419 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700420 * mount, then the original path is exactly the source of
421 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700422 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700423 * mount source = /some/path/exe, mount dest =
424 * /chroot/path/exe Then when getting the original path of
425 * "/chroot/path/exe", the source of that mount,
426 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700427 */
428 if (!strcmp(b->dest, path_inside_chroot))
429 return strdup(b->src);
430
431 /*
432 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700433 * mount, take the suffix of the chroot path relative to the
434 * mount destination path, and append it to the mount source
435 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700436 */
437 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
438 const char *relative_path =
439 path_inside_chroot + strlen(b->dest);
440 return append_external_path(b->src, relative_path);
441 }
442 b = b->next;
443 }
444
445 /* If there is a chroot path, append |path_inside_chroot| to that. */
446 if (j->chrootdir)
447 return append_external_path(j->chrootdir, path_inside_chroot);
448
449 /* No chroot, so the path outside is the same as it is inside. */
450 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700451}
452
Lee Campbell11af0622014-05-22 12:36:04 -0700453void API minijail_mount_tmp(struct minijail *j)
454{
455 j->flags.mount_tmp = 1;
456}
457
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800458int API minijail_write_pid_file(struct minijail *j, const char *path)
459{
460 j->pid_file_path = strdup(path);
461 if (!j->pid_file_path)
462 return -ENOMEM;
463 j->flags.pid_file = 1;
464 return 0;
465}
466
Dylan Reid648b2202015-10-23 00:50:00 -0700467int API minijail_mount(struct minijail *j, const char *src, const char *dest,
468 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700469{
Dylan Reid648b2202015-10-23 00:50:00 -0700470 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400471
472 if (*dest != '/')
473 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700474 m = calloc(1, sizeof(*m));
475 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400476 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700477 m->dest = strdup(dest);
478 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400479 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700480 m->src = strdup(src);
481 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400482 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700483 m->type = strdup(type);
484 if (!m->type)
485 goto error;
486 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400487
Dylan Reid648b2202015-10-23 00:50:00 -0700488 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400489
Elly Jonesdd3e8512012-01-23 15:13:38 -0500490 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700491 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400492 * containing vfs namespace.
493 */
494 minijail_namespace_vfs(j);
495
Dylan Reid648b2202015-10-23 00:50:00 -0700496 if (j->mounts_tail)
497 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400498 else
Dylan Reid648b2202015-10-23 00:50:00 -0700499 j->mounts_head = m;
500 j->mounts_tail = m;
501 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400502
503 return 0;
504
505error:
Dylan Reid648b2202015-10-23 00:50:00 -0700506 free(m->src);
507 free(m->dest);
508 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400509 return -ENOMEM;
510}
511
Dylan Reid648b2202015-10-23 00:50:00 -0700512int API minijail_bind(struct minijail *j, const char *src, const char *dest,
513 int writeable)
514{
515 unsigned long flags = MS_BIND;
516
517 if (!writeable)
518 flags |= MS_RDONLY;
519
520 return minijail_mount(j, src, dest, "", flags);
521}
522
Will Drewry6ac91122011-10-21 16:38:58 -0500523void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400524{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700525 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
526 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
527 warn("not loading seccomp filter, seccomp not supported");
528 return;
529 }
530 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400531 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800532 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700533 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400534 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800535
536 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700537 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
538 die("failed to compile seccomp filter BPF program in '%s'",
539 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800540 }
541
542 j->filter_len = fprog->len;
543 j->filter_prog = fprog;
544
Elly Jonese1749eb2011-10-07 13:54:59 -0400545 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500546}
547
Andrew Brestickereac28942015-11-11 16:04:46 -0800548int API minijail_use_alt_syscall(struct minijail *j, const char *table)
549{
550 j->alt_syscall_table = strdup(table);
551 if (!j->alt_syscall_table)
552 return -ENOMEM;
553 j->flags.alt_syscall = 1;
554 return 0;
555}
556
Will Drewryf89aef52011-09-16 16:48:57 -0500557struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400558 size_t available;
559 size_t total;
560 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500561};
562
Will Drewry6ac91122011-10-21 16:38:58 -0500563void marshal_state_init(struct marshal_state *state,
564 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400565{
566 state->available = available;
567 state->buf = buf;
568 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500569}
570
Will Drewry6ac91122011-10-21 16:38:58 -0500571void marshal_append(struct marshal_state *state,
572 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400573{
574 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500575
Elly Jonese1749eb2011-10-07 13:54:59 -0400576 /* Up to |available| will be written. */
577 if (copy_len) {
578 memcpy(state->buf, src, copy_len);
579 state->buf += copy_len;
580 state->available -= copy_len;
581 }
582 /* |total| will contain the expected length. */
583 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500584}
585
Will Drewry6ac91122011-10-21 16:38:58 -0500586void minijail_marshal_helper(struct marshal_state *state,
587 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400588{
Dylan Reid648b2202015-10-23 00:50:00 -0700589 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400590 marshal_append(state, (char *)j, sizeof(*j));
591 if (j->user)
592 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400593 if (j->chrootdir)
594 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800595 if (j->alt_syscall_table) {
596 marshal_append(state, j->alt_syscall_table,
597 strlen(j->alt_syscall_table) + 1);
598 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800599 if (j->flags.seccomp_filter && j->filter_prog) {
600 struct sock_fprog *fp = j->filter_prog;
601 marshal_append(state, (char *)fp->filter,
602 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400603 }
Dylan Reid648b2202015-10-23 00:50:00 -0700604 for (m = j->mounts_head; m; m = m->next) {
605 marshal_append(state, m->src, strlen(m->src) + 1);
606 marshal_append(state, m->dest, strlen(m->dest) + 1);
607 marshal_append(state, m->type, strlen(m->type) + 1);
608 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400609 }
Will Drewryf89aef52011-09-16 16:48:57 -0500610}
611
Will Drewry6ac91122011-10-21 16:38:58 -0500612size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400613{
614 struct marshal_state state;
615 marshal_state_init(&state, NULL, 0);
616 minijail_marshal_helper(&state, j);
617 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500618}
619
Elly Jonese1749eb2011-10-07 13:54:59 -0400620int minijail_marshal(const struct minijail *j, char *buf, size_t available)
621{
622 struct marshal_state state;
623 marshal_state_init(&state, buf, available);
624 minijail_marshal_helper(&state, j);
625 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500626}
627
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800628/*
629 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400630 * @length Number of bytes to consume
631 * @buf Buffer to consume from
632 * @buflength Size of @buf
633 *
634 * Returns a pointer to the base of the bytes, or NULL for errors.
635 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700636void *consumebytes(size_t length, char **buf, size_t *buflength)
637{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400638 char *p = *buf;
639 if (length > *buflength)
640 return NULL;
641 *buf += length;
642 *buflength -= length;
643 return p;
644}
645
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800646/*
647 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400648 * @buf Buffer to consume
649 * @length Length of buffer
650 *
651 * Returns a pointer to the base of the string, or NULL for errors.
652 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700653char *consumestr(char **buf, size_t *buflength)
654{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400655 size_t len = strnlen(*buf, *buflength);
656 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700657 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400658 return NULL;
659 return consumebytes(len + 1, buf, buflength);
660}
661
Elly Jonese1749eb2011-10-07 13:54:59 -0400662int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
663{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400664 int i;
665 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500666 int ret = -EINVAL;
667
Elly Jonese1749eb2011-10-07 13:54:59 -0400668 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500669 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400670 memcpy((void *)j, serialized, sizeof(*j));
671 serialized += sizeof(*j);
672 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500673
Will Drewrybee7ba72011-10-21 20:47:01 -0500674 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700675 j->mounts_head = NULL;
676 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800677 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500678
Elly Jonese1749eb2011-10-07 13:54:59 -0400679 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400680 char *user = consumestr(&serialized, &length);
681 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500682 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400683 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500684 if (!j->user)
685 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400686 }
Will Drewryf89aef52011-09-16 16:48:57 -0500687
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400688 if (j->chrootdir) { /* stale pointer */
689 char *chrootdir = consumestr(&serialized, &length);
690 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500691 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400692 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500693 if (!j->chrootdir)
694 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400695 }
696
Andrew Brestickereac28942015-11-11 16:04:46 -0800697 if (j->alt_syscall_table) { /* stale pointer */
698 char *alt_syscall_table = consumestr(&serialized, &length);
699 if (!alt_syscall_table)
700 goto bad_syscall_table;
701 j->alt_syscall_table = strdup(alt_syscall_table);
702 if (!j->alt_syscall_table)
703 goto bad_syscall_table;
704 }
705
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800706 if (j->flags.seccomp_filter && j->filter_len > 0) {
707 size_t ninstrs = j->filter_len;
708 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
709 ninstrs > USHRT_MAX)
710 goto bad_filters;
711
712 size_t program_len = ninstrs * sizeof(struct sock_filter);
713 void *program = consumebytes(program_len, &serialized, &length);
714 if (!program)
715 goto bad_filters;
716
717 j->filter_prog = malloc(sizeof(struct sock_fprog));
718 j->filter_prog->len = ninstrs;
719 j->filter_prog->filter = malloc(program_len);
720 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400721 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400722
Dylan Reid648b2202015-10-23 00:50:00 -0700723 count = j->mounts_count;
724 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400725 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700726 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400727 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700728 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400729 const char *src = consumestr(&serialized, &length);
730 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700731 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400732 dest = consumestr(&serialized, &length);
733 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700734 goto bad_mounts;
735 type = consumestr(&serialized, &length);
736 if (!type)
737 goto bad_mounts;
738 flags = consumebytes(sizeof(*flags), &serialized, &length);
739 if (!flags)
740 goto bad_mounts;
741 if (minijail_mount(j, src, dest, type, *flags))
742 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400743 }
744
Elly Jonese1749eb2011-10-07 13:54:59 -0400745 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500746
Dylan Reid648b2202015-10-23 00:50:00 -0700747bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800748 if (j->flags.seccomp_filter && j->filter_len > 0) {
749 free(j->filter_prog->filter);
750 free(j->filter_prog);
751 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500752bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800753 if (j->alt_syscall_table)
754 free(j->alt_syscall_table);
755bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500756 if (j->chrootdir)
757 free(j->chrootdir);
758bad_chrootdir:
759 if (j->user)
760 free(j->user);
761clear_pointers:
762 j->user = NULL;
763 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800764 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500765out:
766 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500767}
768
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800769static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
770{
771 int fd, ret, len;
772 size_t sz;
773 char fname[32];
774 close(pipe_fds[0]);
775
776 sz = sizeof(fname);
777 if (j->uidmap) {
778 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700779 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800780 die("failed to write file name of uid_map");
781 fd = open(fname, O_WRONLY);
782 if (fd < 0)
783 pdie("failed to open '%s'", fname);
784 len = strlen(j->uidmap);
785 if (write(fd, j->uidmap, len) < len)
786 die("failed to set uid_map");
787 close(fd);
788 }
789 if (j->gidmap) {
790 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700791 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800792 die("failed to write file name of gid_map");
793 fd = open(fname, O_WRONLY);
794 if (fd < 0)
795 pdie("failed to open '%s'", fname);
796 len = strlen(j->gidmap);
797 if (write(fd, j->gidmap, len) < len)
798 die("failed to set gid_map");
799 close(fd);
800 }
801
802 close(pipe_fds[1]);
803}
804
805static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
806{
807 char buf;
808
809 close(pipe_fds[1]);
810
811 /* Wait for parent to set up uid/gid mappings. */
812 if (read(pipe_fds[0], &buf, 1) != 0)
813 die("failed to sync with parent");
814 close(pipe_fds[0]);
815
816 if (j->uidmap && setresuid(0, 0, 0))
817 pdie("setresuid");
818 if (j->gidmap && setresgid(0, 0, 0))
819 pdie("setresgid");
820}
821
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800822/*
823 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700824 * @j Minijail these mounts are for
825 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400826 *
827 * Returns 0 for success.
828 */
Dylan Reid648b2202015-10-23 00:50:00 -0700829static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700830{
Dylan Reid648b2202015-10-23 00:50:00 -0700831 int ret;
832 char *dest;
833 int remount_ro = 0;
834
Elly Jones51a5b6c2011-10-12 19:09:26 -0400835 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700836 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400837 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700838
839 /*
840 * R/O bind mounts have to be remounted since bind and ro can't both be
841 * specified in the original bind mount. Remount R/O after the initial
842 * mount.
843 */
844 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
845 remount_ro = 1;
846 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500847 }
Dylan Reid648b2202015-10-23 00:50:00 -0700848
849 ret = mount(m->src, dest, m->type, m->flags, NULL);
850 if (ret)
851 pdie("mount: %s -> %s", m->src, dest);
852
853 if (remount_ro) {
854 m->flags |= MS_RDONLY;
855 ret = mount(m->src, dest, NULL,
856 m->flags | MS_REMOUNT, NULL);
857 if (ret)
858 pdie("bind ro: %s -> %s", m->src, dest);
859 }
860
Elly Jones51a5b6c2011-10-12 19:09:26 -0400861 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700862 if (m->next)
863 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400864 return ret;
865}
866
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700867int enter_chroot(const struct minijail *j)
868{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400869 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700870
871 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400872 return ret;
873
874 if (chroot(j->chrootdir))
875 return -errno;
876
877 if (chdir("/"))
878 return -errno;
879
880 return 0;
881}
882
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800883int enter_pivot_root(const struct minijail *j)
884{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800885 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700886
887 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800888 return ret;
889
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800890 /* Keep the fd for both old and new root. It will be used in fchdir later. */
891 oldroot = open("/", O_DIRECTORY | O_RDONLY);
892 if (oldroot < 0)
893 pdie("failed to open / for fchdir");
894 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
895 if (newroot < 0)
896 pdie("failed to open %s for fchdir", j->chrootdir);
897
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800898 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
899 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
900 pdie("failed to bind mount '%s'", j->chrootdir);
901 if (chdir(j->chrootdir))
902 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800903 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800904 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800905
906 /*
907 * Now the old root is mounted on top of the new root. Use fchdir to
908 * change to the old root and unmount it.
909 */
910 if (fchdir(oldroot))
911 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800912 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800913 if (umount2(".", MNT_DETACH))
914 pdie("umount(/)");
915 /* Change back to the new root. */
916 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800917 return -errno;
918 if (chroot("/"))
919 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700920 /* Set correct CWD for getcwd(3). */
921 if (chdir("/"))
922 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800923
924 return 0;
925}
926
Lee Campbell11af0622014-05-22 12:36:04 -0700927int mount_tmp(void)
928{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800929 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700930}
931
Dylan Reid791f5772015-09-14 20:02:42 -0700932int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400933{
934 const char *kProcPath = "/proc";
935 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500936 /*
937 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400938 * /proc in our namespace, which means using MS_REMOUNT here would
939 * mutate our parent's mount as well, even though we're in a VFS
940 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800941 * and make our own. However, if we are in a new user namespace, /proc
942 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400943 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700944 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400945 return -errno;
946 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
947 return -errno;
948 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400949}
950
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800951static void write_pid_file(const struct minijail *j)
952{
953 FILE *fp = fopen(j->pid_file_path, "w");
954
955 if (!fp)
956 pdie("failed to open '%s'", j->pid_file_path);
957 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
958 pdie("fprintf(%s)", j->pid_file_path);
959 if (fclose(fp))
960 pdie("fclose(%s)", j->pid_file_path);
961}
962
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700963void drop_ugid(const struct minijail *j)
964{
965 if (j->flags.usergroups) {
966 if (initgroups(j->user, j->usergid))
967 pdie("initgroups");
968 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800969 /*
970 * Only attempt to clear supplemental groups if we are changing
971 * users.
972 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700973 if ((j->uid || j->gid) && setgroups(0, NULL))
974 pdie("setgroups");
975 }
976
977 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
978 pdie("setresgid");
979
980 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
981 pdie("setresuid");
982}
983
Mike Frysinger3adfef72013-05-09 17:19:08 -0400984/*
985 * We specifically do not use cap_valid() as that only tells us the last
986 * valid cap we were *compiled* against (i.e. what the version of kernel
987 * headers says). If we run on a different kernel version, then it's not
988 * uncommon for that to be less (if an older kernel) or more (if a newer
989 * kernel). So suck up the answer via /proc.
990 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700991static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -0400992{
Dylan Reidf682d472015-09-17 21:39:07 -0700993 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
994 FILE *fp = fopen(cap_file, "re");
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700995 unsigned int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -0400996
Dylan Reidf682d472015-09-17 21:39:07 -0700997 if (fscanf(fp, "%u", &last_valid_cap) != 1)
998 pdie("fscanf(%s)", cap_file);
999 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001000
Dylan Reidf682d472015-09-17 21:39:07 -07001001 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001002}
1003
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001004void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001005{
1006 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001007 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001008 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001009 unsigned int i;
1010 if (!caps)
1011 die("can't get process caps");
1012 if (cap_clear_flag(caps, CAP_INHERITABLE))
1013 die("can't clear inheritable caps");
1014 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1015 die("can't clear effective caps");
1016 if (cap_clear_flag(caps, CAP_PERMITTED))
1017 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001018 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001019 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001020 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001021 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001022 flag[0] = i;
1023 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001024 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001025 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001026 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001027 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001028 die("can't add inheritable cap");
1029 }
1030 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001031 die("can't apply initial cleaned capset");
1032
1033 /*
1034 * Instead of dropping bounding set first, do it here in case
1035 * the caller had a more permissive bounding set which could
1036 * have been used above to raise a capability that wasn't already
1037 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1038 */
Dylan Reidf682d472015-09-17 21:39:07 -07001039 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001040 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001041 continue;
1042 if (prctl(PR_CAPBSET_DROP, i))
1043 pdie("prctl(PR_CAPBSET_DROP)");
1044 }
Kees Cook323878a2013-02-05 15:35:24 -08001045
1046 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001047 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001048 flag[0] = CAP_SETPCAP;
1049 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1050 die("can't clear effective cap");
1051 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1052 die("can't clear permitted cap");
1053 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1054 die("can't clear inheritable cap");
1055 }
1056
1057 if (cap_set_proc(caps))
1058 die("can't apply final cleaned capset");
1059
1060 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001061}
1062
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001063void set_seccomp_filter(const struct minijail *j)
1064{
1065 /*
1066 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1067 * in the kernel source tree for an explanation of the parameters.
1068 */
1069 if (j->flags.no_new_privs) {
1070 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1071 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1072 }
1073
1074 /*
1075 * If we're logging seccomp filter failures,
1076 * install the SIGSYS handler first.
1077 */
1078 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1079 if (install_sigsys_handler())
1080 pdie("install SIGSYS handler");
1081 warn("logging seccomp filter failures");
1082 }
1083
1084 /*
1085 * Install the syscall filter.
1086 */
1087 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001088 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
1089 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1090 warn("seccomp not supported");
1091 return;
1092 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001093 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001094 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001095 }
1096}
1097
Will Drewry6ac91122011-10-21 16:38:58 -05001098void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001099{
Dylan Reidf682d472015-09-17 21:39:07 -07001100 /*
1101 * Get the last valid cap from /proc, since /proc can be unmounted
1102 * before drop_caps().
1103 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001104 unsigned int last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001105
Elly Jonese1749eb2011-10-07 13:54:59 -04001106 if (j->flags.pids)
1107 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001108 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001109
Elly Jonese1749eb2011-10-07 13:54:59 -04001110 if (j->flags.usergroups && !j->user)
1111 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001112
Elly Jonesdd3e8512012-01-23 15:13:38 -05001113 /*
1114 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001115 * so we don't even try. If any of our operations fail, we abort() the
1116 * entire process.
1117 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001118 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1119 pdie("setns(CLONE_NEWNS)");
1120
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001121 if (j->flags.vfs) {
1122 if (unshare(CLONE_NEWNS))
1123 pdie("unshare(vfs)");
1124 /*
1125 * Remount all filesystems as private. If they are shared
1126 * new bind mounts will creep out of our namespace.
1127 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1128 */
1129 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1130 pdie("mount(/, private)");
1131 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001132
Dylan Reidf7942472015-11-18 17:55:26 -08001133 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1134 pdie("unshare(ipc)");
1135 }
1136
Dylan Reid1102f5a2015-09-15 11:52:20 -07001137 if (j->flags.enter_net) {
1138 if (setns(j->netns_fd, CLONE_NEWNET))
1139 pdie("setns(CLONE_NEWNET)");
1140 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001141 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001142 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001143
Elly Jones51a5b6c2011-10-12 19:09:26 -04001144 if (j->flags.chroot && enter_chroot(j))
1145 pdie("chroot");
1146
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001147 if (j->flags.pivot_root && enter_pivot_root(j))
1148 pdie("pivot_root");
1149
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001150 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001151 pdie("mount_tmp");
1152
Dylan Reid791f5772015-09-14 20:02:42 -07001153 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001154 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001155
Elly Jonese1749eb2011-10-07 13:54:59 -04001156 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001157 /*
1158 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001159 * capability to change uids, our attempt to use setuid()
1160 * below will fail. Hang on to root caps across setuid(), then
1161 * lock securebits.
1162 */
1163 if (prctl(PR_SET_KEEPCAPS, 1))
1164 pdie("prctl(PR_SET_KEEPCAPS)");
1165 if (prctl
1166 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1167 pdie("prctl(PR_SET_SECUREBITS)");
1168 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001169
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001170 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001171 * If we're setting no_new_privs, we can drop privileges
1172 * before setting seccomp filter. This way filter policies
1173 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001174 */
1175 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001176 drop_ugid(j);
1177 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001178 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001179
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001180 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001181 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001182 /*
1183 * If we're not setting no_new_privs,
1184 * we need to set seccomp filter *before* dropping privileges.
1185 * WARNING: this means that filter policies *must* allow
1186 * setgroups()/setresgid()/setresuid() for dropping root and
1187 * capget()/capset()/prctl() for dropping caps.
1188 */
1189 set_seccomp_filter(j);
1190
1191 drop_ugid(j);
1192 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001193 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001194 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001195
Elly Jonesdd3e8512012-01-23 15:13:38 -05001196 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001197 * Select the specified alternate syscall table. The table must not
1198 * block prctl(2) if we're using seccomp as well.
1199 */
1200 if (j->flags.alt_syscall) {
1201 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1202 pdie("prctl(PR_ALT_SYSCALL)");
1203 }
1204
1205 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001206 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001207 * privilege-dropping syscalls :)
1208 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001209 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1210 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1211 warn("seccomp not supported");
1212 return;
1213 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001214 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001215 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001216}
1217
Will Drewry6ac91122011-10-21 16:38:58 -05001218/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001219static int init_exitstatus = 0;
1220
Will Drewry6ac91122011-10-21 16:38:58 -05001221void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001222{
1223 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001224}
1225
Will Drewry6ac91122011-10-21 16:38:58 -05001226int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001227{
1228 pid_t pid;
1229 int status;
1230 /* so that we exit with the right status */
1231 signal(SIGTERM, init_term);
1232 /* TODO(wad) self jail with seccomp_filters here. */
1233 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001234 /*
1235 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001236 * left inside our pid namespace or we get a signal.
1237 */
1238 if (pid == rootpid)
1239 init_exitstatus = status;
1240 }
1241 if (!WIFEXITED(init_exitstatus))
1242 _exit(MINIJAIL_ERR_INIT);
1243 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001244}
1245
Will Drewry6ac91122011-10-21 16:38:58 -05001246int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001247{
1248 size_t sz = 0;
1249 size_t bytes = read(fd, &sz, sizeof(sz));
1250 char *buf;
1251 int r;
1252 if (sizeof(sz) != bytes)
1253 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001254 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001255 return -E2BIG;
1256 buf = malloc(sz);
1257 if (!buf)
1258 return -ENOMEM;
1259 bytes = read(fd, buf, sz);
1260 if (bytes != sz) {
1261 free(buf);
1262 return -EINVAL;
1263 }
1264 r = minijail_unmarshal(j, buf, sz);
1265 free(buf);
1266 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001267}
1268
Will Drewry6ac91122011-10-21 16:38:58 -05001269int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001270{
1271 char *buf;
1272 size_t sz = minijail_size(j);
1273 ssize_t written;
1274 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001275
Elly Jonese1749eb2011-10-07 13:54:59 -04001276 if (!sz)
1277 return -EINVAL;
1278 buf = malloc(sz);
1279 r = minijail_marshal(j, buf, sz);
1280 if (r) {
1281 free(buf);
1282 return r;
1283 }
1284 /* Sends [size][minijail]. */
1285 written = write(fd, &sz, sizeof(sz));
1286 if (written != sizeof(sz)) {
1287 free(buf);
1288 return -EFAULT;
1289 }
1290 written = write(fd, buf, sz);
1291 if (written < 0 || (size_t) written != sz) {
1292 free(buf);
1293 return -EFAULT;
1294 }
1295 free(buf);
1296 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001297}
Elly Jonescd7a9042011-07-22 13:56:51 -04001298
Will Drewry6ac91122011-10-21 16:38:58 -05001299int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001300{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001301#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001302 /* Don't use LDPRELOAD on Brillo. */
1303 return 0;
1304#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001305 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1306 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1307 if (!newenv)
1308 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001309
Elly Jonese1749eb2011-10-07 13:54:59 -04001310 /* Only insert a separating space if we have something to separate... */
1311 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1312 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001313
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001314 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001315 setenv(kLdPreloadEnvVar, newenv, 1);
1316 free(newenv);
1317 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001318#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001319}
1320
Will Drewry6ac91122011-10-21 16:38:58 -05001321int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001322{
1323 int r = pipe(fds);
1324 char fd_buf[11];
1325 if (r)
1326 return r;
1327 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1328 if (r <= 0)
1329 return -EINVAL;
1330 setenv(kFdEnvVar, fd_buf, 1);
1331 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001332}
1333
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001334int setup_pipe_end(int fds[2], size_t index)
1335{
1336 if (index > 1)
1337 return -1;
1338
1339 close(fds[1 - index]);
1340 return fds[index];
1341}
1342
1343int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1344{
1345 if (index > 1)
1346 return -1;
1347
1348 close(fds[1 - index]);
1349 /* dup2(2) the corresponding end of the pipe into |fd|. */
1350 return dup2(fds[index], fd);
1351}
1352
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001353int minijail_run_internal(struct minijail *j, const char *filename,
1354 char *const argv[], pid_t *pchild_pid,
1355 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1356 int use_preload);
1357
Will Drewry6ac91122011-10-21 16:38:58 -05001358int API minijail_run(struct minijail *j, const char *filename,
1359 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001360{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001361 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1362 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001363}
1364
1365int API minijail_run_pid(struct minijail *j, const char *filename,
1366 char *const argv[], pid_t *pchild_pid)
1367{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001368 return minijail_run_internal(j, filename, argv, pchild_pid,
1369 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001370}
1371
1372int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001373 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001374{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001375 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1376 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001377}
1378
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001379int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001380 char *const argv[], pid_t *pchild_pid,
1381 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001382{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001383 return minijail_run_internal(j, filename, argv, pchild_pid,
1384 pstdin_fd, pstdout_fd, pstderr_fd, true);
1385}
1386
1387int API minijail_run_no_preload(struct minijail *j, const char *filename,
1388 char *const argv[])
1389{
1390 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1391 false);
1392}
1393
Samuel Tan63187f42015-10-16 13:01:53 -07001394int API minijail_run_pid_pipes_no_preload(struct minijail *j,
1395 const char *filename, char *const argv[],
1396 pid_t *pchild_pid,
1397 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) {
1398 return minijail_run_internal(j, filename, argv, pchild_pid,
1399 pstdin_fd, pstdout_fd, pstderr_fd, false);
1400}
1401
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001402int minijail_run_internal(struct minijail *j, const char *filename,
1403 char *const argv[], pid_t *pchild_pid,
1404 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1405 int use_preload)
1406{
Elly Jonese1749eb2011-10-07 13:54:59 -04001407 char *oldenv, *oldenv_copy = NULL;
1408 pid_t child_pid;
1409 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001410 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001411 int stdout_fds[2];
1412 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001413 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001414 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001415 /* We need to remember this across the minijail_preexec() call. */
1416 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001417 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001418
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001419 if (use_preload) {
1420 oldenv = getenv(kLdPreloadEnvVar);
1421 if (oldenv) {
1422 oldenv_copy = strdup(oldenv);
1423 if (!oldenv_copy)
1424 return -ENOMEM;
1425 }
1426
1427 if (setup_preload())
1428 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001429 }
Will Drewryf89aef52011-09-16 16:48:57 -05001430
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001431 if (!use_preload) {
1432 if (j->flags.caps)
1433 die("Capabilities are not supported without "
1434 "LD_PRELOAD");
1435 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001436
Elly Jonesdd3e8512012-01-23 15:13:38 -05001437 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001438 * Make the process group ID of this process equal to its PID, so that
1439 * both the Minijail process and the jailed process can be killed
1440 * together.
1441 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1442 * the process is already a process group leader.
1443 */
1444 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1445 if (errno != EPERM) {
1446 pdie("setpgid(0, 0)");
1447 }
1448 }
1449
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001450 if (use_preload) {
1451 /*
1452 * Before we fork(2) and execve(2) the child process, we need
1453 * to open a pipe(2) to send the minijail configuration over.
1454 */
1455 if (setup_pipe(pipe_fds))
1456 return -EFAULT;
1457 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001458
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001459 /*
1460 * If we want to write to the child process' standard input,
1461 * create the pipe(2) now.
1462 */
1463 if (pstdin_fd) {
1464 if (pipe(stdin_fds))
1465 return -EFAULT;
1466 }
1467
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001468 /*
1469 * If we want to read from the child process' standard output,
1470 * create the pipe(2) now.
1471 */
1472 if (pstdout_fd) {
1473 if (pipe(stdout_fds))
1474 return -EFAULT;
1475 }
1476
1477 /*
1478 * If we want to read from the child process' standard error,
1479 * create the pipe(2) now.
1480 */
1481 if (pstderr_fd) {
1482 if (pipe(stderr_fds))
1483 return -EFAULT;
1484 }
1485
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001486 /*
1487 * If we want to set up a new uid/gid mapping in the user namespace,
1488 * create the pipe(2) to sync between parent and child.
1489 */
1490 if (j->flags.userns) {
1491 if (pipe(userns_pipe_fds))
1492 return -EFAULT;
1493 }
1494
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001495 /*
1496 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001497 *
1498 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1499 *
1500 * In multithreaded programs, there are a bunch of locks inside libc,
1501 * some of which may be held by other threads at the time that we call
1502 * minijail_run_pid(). If we call fork(), glibc does its level best to
1503 * ensure that we hold all of these locks before it calls clone()
1504 * internally and drop them after clone() returns, but when we call
1505 * sys_clone(2) directly, all that gets bypassed and we end up with a
1506 * child address space where some of libc's important locks are held by
1507 * other threads (which did not get cloned, and hence will never release
1508 * those locks). This is okay so long as we call exec() immediately
1509 * after, but a bunch of seemingly-innocent libc functions like setenv()
1510 * take locks.
1511 *
1512 * Hence, only call sys_clone() if we need to, in order to get at pid
1513 * namespacing. If we follow this path, the child's address space might
1514 * have broken locks; you may only call functions that do not acquire
1515 * any locks.
1516 *
1517 * Unfortunately, fork() acquires every lock it can get its hands on, as
1518 * previously detailed, so this function is highly likely to deadlock
1519 * later on (see "deadlock here") if we're multithreaded.
1520 *
1521 * We might hack around this by having the clone()d child (init of the
1522 * pid namespace) return directly, rather than leaving the clone()d
1523 * process hanging around to be init for the new namespace (and having
1524 * its fork()ed child return in turn), but that process would be crippled
1525 * with its libc locks potentially broken. We might try fork()ing in the
1526 * parent before we clone() to ensure that we own all the locks, but
1527 * then we have to have the forked child hanging around consuming
1528 * resources (and possibly having file descriptors / shared memory
1529 * regions / etc attached). We'd need to keep the child around to avoid
1530 * having its children get reparented to init.
1531 *
1532 * TODO(ellyjones): figure out if the "forked child hanging around"
1533 * problem is fixable or not. It would be nice if we worked in this
1534 * case.
1535 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001536 if (pid_namespace) {
1537 int clone_flags = CLONE_NEWPID | SIGCHLD;
1538 if (j->flags.userns)
1539 clone_flags |= CLONE_NEWUSER;
1540 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001541 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001542 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001543 }
Elly Jones761b7412012-06-13 15:49:52 -04001544
Elly Jonese1749eb2011-10-07 13:54:59 -04001545 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001546 if (use_preload) {
1547 free(oldenv_copy);
1548 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001549 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001550 }
Will Drewryf89aef52011-09-16 16:48:57 -05001551
Elly Jonese1749eb2011-10-07 13:54:59 -04001552 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001553 if (use_preload) {
1554 /* Restore parent's LD_PRELOAD. */
1555 if (oldenv_copy) {
1556 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1557 free(oldenv_copy);
1558 } else {
1559 unsetenv(kLdPreloadEnvVar);
1560 }
1561 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001562 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001563
Elly Jonese1749eb2011-10-07 13:54:59 -04001564 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001565
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001566 if (j->flags.pid_file)
1567 write_pid_file(j);
1568
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001569 if (j->flags.userns)
1570 write_ugid_mappings(j, userns_pipe_fds);
1571
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001572 if (use_preload) {
1573 /* Send marshalled minijail. */
1574 close(pipe_fds[0]); /* read endpoint */
1575 ret = minijail_to_fd(j, pipe_fds[1]);
1576 close(pipe_fds[1]); /* write endpoint */
1577 if (ret) {
1578 kill(j->initpid, SIGKILL);
1579 die("failed to send marshalled minijail");
1580 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001581 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001582
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001583 if (pchild_pid)
1584 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001585
1586 /*
1587 * If we want to write to the child process' standard input,
1588 * set up the write end of the pipe.
1589 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001590 if (pstdin_fd)
1591 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001592 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001593
1594 /*
1595 * If we want to read from the child process' standard output,
1596 * set up the read end of the pipe.
1597 */
1598 if (pstdout_fd)
1599 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001600 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001601
1602 /*
1603 * If we want to read from the child process' standard error,
1604 * set up the read end of the pipe.
1605 */
1606 if (pstderr_fd)
1607 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001608 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001609
Elly Jonese1749eb2011-10-07 13:54:59 -04001610 return 0;
1611 }
1612 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001613
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001614 if (j->flags.userns)
1615 enter_user_namespace(j, userns_pipe_fds);
1616
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001617 /*
1618 * If we want to write to the jailed process' standard input,
1619 * set up the read end of the pipe.
1620 */
1621 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001622 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1623 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001624 die("failed to set up stdin pipe");
1625 }
1626
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001627 /*
1628 * If we want to read from the jailed process' standard output,
1629 * set up the write end of the pipe.
1630 */
1631 if (pstdout_fd) {
1632 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1633 STDOUT_FILENO) < 0)
1634 die("failed to set up stdout pipe");
1635 }
1636
1637 /*
1638 * If we want to read from the jailed process' standard error,
1639 * set up the write end of the pipe.
1640 */
1641 if (pstderr_fd) {
1642 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1643 STDERR_FILENO) < 0)
1644 die("failed to set up stderr pipe");
1645 }
1646
Dylan Reid791f5772015-09-14 20:02:42 -07001647 /* If running an init program, let it decide when/how to mount /proc. */
1648 if (pid_namespace && !do_init)
1649 j->flags.remount_proc_ro = 0;
1650
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001651 if (use_preload) {
1652 /* Strip out flags that cannot be inherited across execve(2). */
1653 minijail_preexec(j);
1654 } else {
1655 j->flags.pids = 0;
1656 }
1657 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001658 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001659
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001660 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001661 /*
1662 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001663 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001664 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001665 * a child to actually run the program. If |do_init == 0|, we
1666 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001667 *
1668 * If we're multithreaded, we'll probably deadlock here. See
1669 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001670 */
1671 child_pid = fork();
1672 if (child_pid < 0)
1673 _exit(child_pid);
1674 else if (child_pid > 0)
1675 init(child_pid); /* never returns */
1676 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001677
Elly Jonesdd3e8512012-01-23 15:13:38 -05001678 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001679 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001680 * calling process
1681 * -> execve()-ing process
1682 * If we are:
1683 * calling process
1684 * -> init()-ing process
1685 * -> execve()-ing process
1686 */
1687 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001688}
1689
Will Drewry6ac91122011-10-21 16:38:58 -05001690int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001691{
1692 int st;
1693 if (kill(j->initpid, SIGTERM))
1694 return -errno;
1695 if (waitpid(j->initpid, &st, 0) < 0)
1696 return -errno;
1697 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001698}
1699
Will Drewry6ac91122011-10-21 16:38:58 -05001700int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001701{
1702 int st;
1703 if (waitpid(j->initpid, &st, 0) < 0)
1704 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001705
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001706 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001707 int error_status = st;
1708 if (WIFSIGNALED(st)) {
1709 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001710 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001711 j->initpid, signum);
1712 /*
1713 * We return MINIJAIL_ERR_JAIL if the process received
1714 * SIGSYS, which happens when a syscall is blocked by
1715 * seccomp filters.
1716 * If not, we do what bash(1) does:
1717 * $? = 128 + signum
1718 */
1719 if (signum == SIGSYS) {
1720 error_status = MINIJAIL_ERR_JAIL;
1721 } else {
1722 error_status = 128 + signum;
1723 }
1724 }
1725 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001726 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001727
1728 int exit_status = WEXITSTATUS(st);
1729 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001730 info("child process %d exited with status %d",
1731 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001732
1733 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001734}
1735
Will Drewry6ac91122011-10-21 16:38:58 -05001736void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001737{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001738 if (j->flags.seccomp_filter && j->filter_prog) {
1739 free(j->filter_prog->filter);
1740 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001741 }
Dylan Reid648b2202015-10-23 00:50:00 -07001742 while (j->mounts_head) {
1743 struct mountpoint *m = j->mounts_head;
1744 j->mounts_head = j->mounts_head->next;
1745 free(m->type);
1746 free(m->dest);
1747 free(m->src);
1748 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001749 }
Dylan Reid648b2202015-10-23 00:50:00 -07001750 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001751 if (j->user)
1752 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001753 if (j->chrootdir)
1754 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001755 if (j->alt_syscall_table)
1756 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001757 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001758}