blob: e988c0019284df8e79ac8aa2b05ff11b27c96128 [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
7#define _GNU_SOURCE
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070021#include <stdbool.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080022#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syscall.h>
27#include <sys/capability.h>
28#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050029#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040030#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070031#include <sys/stat.h>
32#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080033#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040035#include <unistd.h>
36
37#include "libminijail.h"
38#include "libminijail-private.h"
39
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070040#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080041#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070042#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080043
Lei Zhangeee31552012-10-17 21:27:10 -070044#ifdef HAVE_SECUREBITS_H
45#include <linux/securebits.h>
46#else
47#define SECURE_ALL_BITS 0x15
48#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
49#endif
50
Will Drewry32ac9f52011-08-18 21:36:27 -050051/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080052#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070053# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080054#endif
55
Andrew Brestickereac28942015-11-11 16:04:46 -080056#ifndef PR_ALT_SYSCALL
57# define PR_ALT_SYSCALL 0x43724f53
58#endif
59
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080060/* For seccomp_filter using BPF. */
61#ifndef PR_SET_NO_NEW_PRIVS
62# define PR_SET_NO_NEW_PRIVS 38
63#endif
64#ifndef SECCOMP_MODE_FILTER
65# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050066#endif
67
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070068#ifdef USE_SECCOMP_SOFTFAIL
69# define SECCOMP_SOFTFAIL 1
70#else
71# define SECCOMP_SOFTFAIL 0
72#endif
73
Dylan Reid648b2202015-10-23 00:50:00 -070074struct mountpoint {
Elly Jones51a5b6c2011-10-12 19:09:26 -040075 char *src;
76 char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -070077 char *type;
78 unsigned long flags;
79 struct mountpoint *next;
Elly Jones51a5b6c2011-10-12 19:09:26 -040080};
81
Will Drewryf89aef52011-09-16 16:48:57 -050082struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070083 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070084 * WARNING: if you add a flag here you need to make sure it's
85 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070086 */
Elly Jonese1749eb2011-10-07 13:54:59 -040087 struct {
88 int uid:1;
89 int gid:1;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080090 int usergroups:1;
91 int suppl_gids:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040092 int caps:1;
93 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070094 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040095 int pids:1;
Dylan Reidf7942472015-11-18 17:55:26 -080096 int ipc:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040097 int net:1;
Dylan Reid1102f5a2015-09-15 11:52:20 -070098 int enter_net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080099 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400100 int seccomp:1;
Dylan Reid791f5772015-09-14 20:02:42 -0700101 int remount_proc_ro:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700102 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400103 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700104 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400105 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800106 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -0700107 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800108 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800109 int pid_file:1;
Andrew Brestickereac28942015-11-11 16:04:46 -0800110 int alt_syscall:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400111 } flags;
112 uid_t uid;
113 gid_t gid;
114 gid_t usergid;
115 char *user;
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800116 size_t suppl_gid_count;
117 gid_t *suppl_gid_list;
Elly Jonese1749eb2011-10-07 13:54:59 -0400118 uint64_t caps;
119 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700120 int mountns_fd;
Dylan Reid1102f5a2015-09-15 11:52:20 -0700121 int netns_fd;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400122 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800123 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800124 char *uidmap;
125 char *gidmap;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800126 size_t filter_len;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800127 struct sock_fprog *filter_prog;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800128 char *alt_syscall_table;
Dylan Reid648b2202015-10-23 00:50:00 -0700129 struct mountpoint *mounts_head;
130 struct mountpoint *mounts_tail;
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800131 size_t mounts_count;
Will Drewryf89aef52011-09-16 16:48:57 -0500132};
133
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700134/*
135 * Strip out flags meant for the parent.
136 * We keep things that are not inherited across execve(2) (e.g. capabilities),
137 * or are easier to set after execve(2) (e.g. seccomp filters).
138 */
139void minijail_preenter(struct minijail *j)
140{
141 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700142 j->flags.enter_vfs = 0;
Dylan Reid791f5772015-09-14 20:02:42 -0700143 j->flags.remount_proc_ro = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700144 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800145 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800146 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700147}
148
149/*
150 * Strip out flags meant for the child.
151 * We keep things that are inherited across execve(2).
152 */
153void minijail_preexec(struct minijail *j)
154{
155 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700156 int enter_vfs = j->flags.enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700157 int remount_proc_ro = j->flags.remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800158 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700159 if (j->user)
160 free(j->user);
161 j->user = NULL;
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -0800162 if (j->suppl_gid_list)
163 free(j->suppl_gid_list);
164 j->suppl_gid_list = NULL;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700165 memset(&j->flags, 0, sizeof(j->flags));
166 /* Now restore anything we meant to keep. */
167 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700168 j->flags.enter_vfs = enter_vfs;
Dylan Reid791f5772015-09-14 20:02:42 -0700169 j->flags.remount_proc_ro = remount_proc_ro;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800170 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700171 /* Note, |pids| will already have been used before this call. */
172}
173
174/* Minijail API. */
175
Will Drewry6ac91122011-10-21 16:38:58 -0500176struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400177{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400178 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400179}
180
Will Drewry6ac91122011-10-21 16:38:58 -0500181void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400182{
183 if (uid == 0)
184 die("useless change to uid 0");
185 j->uid = uid;
186 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400187}
188
Will Drewry6ac91122011-10-21 16:38:58 -0500189void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400190{
191 if (gid == 0)
192 die("useless change to gid 0");
193 j->gid = gid;
194 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400195}
196
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800197int API minijail_set_supplementary_gids(struct minijail *j, size_t size,
198 const gid_t *list)
199{
200 if (j->flags.usergroups)
201 die("cannot inherit *and* set supplementary groups");
202
203 if (size == 0)
204 return -EINVAL;
205
206 /* Copy the gid_t array. */
207 j->suppl_gid_list = calloc(size, sizeof(gid_t));
208 if (!j->suppl_gid_list) {
209 return -ENOMEM;
210 }
211 for (size_t i = 0; i < size; i++) {
212 j->suppl_gid_list[i] = list[i];
213 }
214 j->suppl_gid_count = size;
215 j->flags.suppl_gids = 1;
216 return 0;
217}
218
Will Drewry6ac91122011-10-21 16:38:58 -0500219int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400220{
221 char *buf = NULL;
222 struct passwd pw;
223 struct passwd *ppw = NULL;
224 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
225 if (sz == -1)
226 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400227
Elly Jonesdd3e8512012-01-23 15:13:38 -0500228 /*
229 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400230 * the maximum needed size of the buffer, so we don't have to search.
231 */
232 buf = malloc(sz);
233 if (!buf)
234 return -ENOMEM;
235 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500236 /*
237 * We're safe to free the buffer here. The strings inside pw point
238 * inside buf, but we don't use any of them; this leaves the pointers
239 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
240 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400241 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700242 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400243 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700244 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400245 minijail_change_uid(j, ppw->pw_uid);
246 j->user = strdup(user);
247 if (!j->user)
248 return -ENOMEM;
249 j->usergid = ppw->pw_gid;
250 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400251}
252
Will Drewry6ac91122011-10-21 16:38:58 -0500253int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400254{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700255 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700256 struct group gr;
257 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400258 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
259 if (sz == -1)
260 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400261
Elly Jonesdd3e8512012-01-23 15:13:38 -0500262 /*
263 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400264 * the maximum needed size of the buffer, so we don't have to search.
265 */
266 buf = malloc(sz);
267 if (!buf)
268 return -ENOMEM;
269 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500270 /*
271 * We're safe to free the buffer here. The strings inside gr point
272 * inside buf, but we don't use any of them; this leaves the pointers
273 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
274 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400275 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700276 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400277 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700278 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400279 minijail_change_gid(j, pgr->gr_gid);
280 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400281}
282
Will Drewry6ac91122011-10-21 16:38:58 -0500283void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400284{
285 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400286}
287
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700288void API minijail_no_new_privs(struct minijail *j)
289{
290 j->flags.no_new_privs = 1;
291}
292
Will Drewry6ac91122011-10-21 16:38:58 -0500293void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400294{
295 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500296}
297
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700298void API minijail_log_seccomp_filter_failures(struct minijail *j)
299{
300 j->flags.log_seccomp_filter = 1;
301}
302
Will Drewry6ac91122011-10-21 16:38:58 -0500303void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400304{
305 j->caps = capmask;
306 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400307}
308
Will Drewry6ac91122011-10-21 16:38:58 -0500309void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400310{
311 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400312}
313
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700314void API minijail_namespace_enter_vfs(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->mountns_fd = ns_fd;
321 j->flags.enter_vfs = 1;
322}
323
Will Drewry6ac91122011-10-21 16:38:58 -0500324void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400325{
Elly Jonese58176c2012-01-23 11:46:17 -0500326 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700327 j->flags.remount_proc_ro = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400328 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800329 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400330}
331
Dylan Reidf7942472015-11-18 17:55:26 -0800332void API minijail_namespace_ipc(struct minijail *j)
333{
334 j->flags.ipc = 1;
335}
336
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400337void API minijail_namespace_net(struct minijail *j)
338{
339 j->flags.net = 1;
340}
341
Dylan Reid1102f5a2015-09-15 11:52:20 -0700342void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path)
343{
344 int ns_fd = open(ns_path, O_RDONLY);
345 if (ns_fd < 0) {
346 pdie("failed to open namespace '%s'", ns_path);
347 }
348 j->netns_fd = ns_fd;
349 j->flags.enter_net = 1;
350}
351
Dylan Reid791f5772015-09-14 20:02:42 -0700352void API minijail_remount_proc_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400353{
354 j->flags.vfs = 1;
Dylan Reid791f5772015-09-14 20:02:42 -0700355 j->flags.remount_proc_ro = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400356}
357
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800358void API minijail_namespace_user(struct minijail *j)
359{
360 j->flags.userns = 1;
361}
362
363int API minijail_uidmap(struct minijail *j, const char *uidmap)
364{
365 j->uidmap = strdup(uidmap);
366 if (!j->uidmap)
367 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800368 char *ch;
369 for (ch = j->uidmap; *ch; ch++) {
370 if (*ch == ',')
371 *ch = '\n';
372 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800373 return 0;
374}
375
376int API minijail_gidmap(struct minijail *j, const char *gidmap)
377{
378 j->gidmap = strdup(gidmap);
379 if (!j->gidmap)
380 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800381 char *ch;
382 for (ch = j->gidmap; *ch; ch++) {
383 if (*ch == ',')
384 *ch = '\n';
385 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800386 return 0;
387}
388
Will Drewry6ac91122011-10-21 16:38:58 -0500389void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400390{
391 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400392}
393
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800394void API minijail_run_as_init(struct minijail *j)
395{
396 /*
397 * Since the jailed program will become 'init' in the new PID namespace,
398 * Minijail does not need to fork an 'init' process.
399 */
400 j->flags.do_init = 0;
401}
402
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700403int API minijail_enter_chroot(struct minijail *j, const char *dir)
404{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400405 if (j->chrootdir)
406 return -EINVAL;
407 j->chrootdir = strdup(dir);
408 if (!j->chrootdir)
409 return -ENOMEM;
410 j->flags.chroot = 1;
411 return 0;
412}
413
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800414int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
415{
416 if (j->chrootdir)
417 return -EINVAL;
418 j->chrootdir = strdup(dir);
419 if (!j->chrootdir)
420 return -ENOMEM;
421 j->flags.pivot_root = 1;
422 return 0;
423}
424
Dylan Reida14e08d2015-10-22 21:05:29 -0700425static char *append_external_path(const char *external_path,
426 const char *path_inside_chroot)
Dylan Reid08946cc2015-09-16 19:10:57 -0700427{
Dylan Reida14e08d2015-10-22 21:05:29 -0700428 char *path;
Dylan Reid08946cc2015-09-16 19:10:57 -0700429 size_t pathlen;
430
Dylan Reid08946cc2015-09-16 19:10:57 -0700431 /* One extra char for '/' and one for '\0', hence + 2. */
Dylan Reida14e08d2015-10-22 21:05:29 -0700432 pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2;
433 path = malloc(pathlen);
434 snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700435
Dylan Reida14e08d2015-10-22 21:05:29 -0700436 return path;
437}
438
439char API *minijail_get_original_path(struct minijail *j,
440 const char *path_inside_chroot)
441{
Dylan Reid648b2202015-10-23 00:50:00 -0700442 struct mountpoint *b;
Dylan Reida14e08d2015-10-22 21:05:29 -0700443
Dylan Reid648b2202015-10-23 00:50:00 -0700444 b = j->mounts_head;
Dylan Reida14e08d2015-10-22 21:05:29 -0700445 while (b) {
446 /*
447 * If |path_inside_chroot| is the exact destination of a
Dylan Reid648b2202015-10-23 00:50:00 -0700448 * mount, then the original path is exactly the source of
449 * the mount.
Dylan Reida14e08d2015-10-22 21:05:29 -0700450 * for example: "-b /some/path/exe,/chroot/path/exe"
Dylan Reid648b2202015-10-23 00:50:00 -0700451 * mount source = /some/path/exe, mount dest =
452 * /chroot/path/exe Then when getting the original path of
453 * "/chroot/path/exe", the source of that mount,
454 * "/some/path/exe" is what should be returned.
Dylan Reida14e08d2015-10-22 21:05:29 -0700455 */
456 if (!strcmp(b->dest, path_inside_chroot))
457 return strdup(b->src);
458
459 /*
460 * If |path_inside_chroot| is within the destination path of a
Dylan Reid648b2202015-10-23 00:50:00 -0700461 * mount, take the suffix of the chroot path relative to the
462 * mount destination path, and append it to the mount source
463 * path.
Dylan Reida14e08d2015-10-22 21:05:29 -0700464 */
465 if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) {
466 const char *relative_path =
467 path_inside_chroot + strlen(b->dest);
468 return append_external_path(b->src, relative_path);
469 }
470 b = b->next;
471 }
472
473 /* If there is a chroot path, append |path_inside_chroot| to that. */
474 if (j->chrootdir)
475 return append_external_path(j->chrootdir, path_inside_chroot);
476
477 /* No chroot, so the path outside is the same as it is inside. */
478 return strdup(path_inside_chroot);
Dylan Reid08946cc2015-09-16 19:10:57 -0700479}
480
Lee Campbell11af0622014-05-22 12:36:04 -0700481void API minijail_mount_tmp(struct minijail *j)
482{
483 j->flags.mount_tmp = 1;
484}
485
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800486int API minijail_write_pid_file(struct minijail *j, const char *path)
487{
488 j->pid_file_path = strdup(path);
489 if (!j->pid_file_path)
490 return -ENOMEM;
491 j->flags.pid_file = 1;
492 return 0;
493}
494
Dylan Reid648b2202015-10-23 00:50:00 -0700495int API minijail_mount(struct minijail *j, const char *src, const char *dest,
496 const char *type, unsigned long flags)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700497{
Dylan Reid648b2202015-10-23 00:50:00 -0700498 struct mountpoint *m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400499
500 if (*dest != '/')
501 return -EINVAL;
Dylan Reid648b2202015-10-23 00:50:00 -0700502 m = calloc(1, sizeof(*m));
503 if (!m)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400504 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700505 m->dest = strdup(dest);
506 if (!m->dest)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400507 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700508 m->src = strdup(src);
509 if (!m->src)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400510 goto error;
Dylan Reid648b2202015-10-23 00:50:00 -0700511 m->type = strdup(type);
512 if (!m->type)
513 goto error;
514 m->flags = flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400515
Dylan Reid648b2202015-10-23 00:50:00 -0700516 info("mount %s -> %s type %s", src, dest, type);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400517
Elly Jonesdd3e8512012-01-23 15:13:38 -0500518 /*
Dylan Reid648b2202015-10-23 00:50:00 -0700519 * Force vfs namespacing so the mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400520 * containing vfs namespace.
521 */
522 minijail_namespace_vfs(j);
523
Dylan Reid648b2202015-10-23 00:50:00 -0700524 if (j->mounts_tail)
525 j->mounts_tail->next = m;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400526 else
Dylan Reid648b2202015-10-23 00:50:00 -0700527 j->mounts_head = m;
528 j->mounts_tail = m;
529 j->mounts_count++;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400530
531 return 0;
532
533error:
Dylan Reid648b2202015-10-23 00:50:00 -0700534 free(m->src);
535 free(m->dest);
536 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400537 return -ENOMEM;
538}
539
Dylan Reid648b2202015-10-23 00:50:00 -0700540int API minijail_bind(struct minijail *j, const char *src, const char *dest,
541 int writeable)
542{
543 unsigned long flags = MS_BIND;
544
545 if (!writeable)
546 flags |= MS_RDONLY;
547
548 return minijail_mount(j, src, dest, "", flags);
549}
550
Will Drewry6ac91122011-10-21 16:38:58 -0500551void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400552{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700553 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
554 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800555 warn("not loading seccomp filter,"
556 " seccomp not supported");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700557 return;
558 }
559 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400560 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800561 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700562 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400563 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800564
565 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700566 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
567 die("failed to compile seccomp filter BPF program in '%s'",
568 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800569 }
570
571 j->filter_len = fprog->len;
572 j->filter_prog = fprog;
573
Elly Jonese1749eb2011-10-07 13:54:59 -0400574 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500575}
576
Andrew Brestickereac28942015-11-11 16:04:46 -0800577int API minijail_use_alt_syscall(struct minijail *j, const char *table)
578{
579 j->alt_syscall_table = strdup(table);
580 if (!j->alt_syscall_table)
581 return -ENOMEM;
582 j->flags.alt_syscall = 1;
583 return 0;
584}
585
Will Drewryf89aef52011-09-16 16:48:57 -0500586struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400587 size_t available;
588 size_t total;
589 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500590};
591
Will Drewry6ac91122011-10-21 16:38:58 -0500592void marshal_state_init(struct marshal_state *state,
593 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400594{
595 state->available = available;
596 state->buf = buf;
597 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500598}
599
Will Drewry6ac91122011-10-21 16:38:58 -0500600void marshal_append(struct marshal_state *state,
601 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400602{
603 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500604
Elly Jonese1749eb2011-10-07 13:54:59 -0400605 /* Up to |available| will be written. */
606 if (copy_len) {
607 memcpy(state->buf, src, copy_len);
608 state->buf += copy_len;
609 state->available -= copy_len;
610 }
611 /* |total| will contain the expected length. */
612 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500613}
614
Will Drewry6ac91122011-10-21 16:38:58 -0500615void minijail_marshal_helper(struct marshal_state *state,
616 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400617{
Dylan Reid648b2202015-10-23 00:50:00 -0700618 struct mountpoint *m = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400619 marshal_append(state, (char *)j, sizeof(*j));
620 if (j->user)
621 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400622 if (j->chrootdir)
623 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Andrew Brestickereac28942015-11-11 16:04:46 -0800624 if (j->alt_syscall_table) {
625 marshal_append(state, j->alt_syscall_table,
626 strlen(j->alt_syscall_table) + 1);
627 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800628 if (j->flags.seccomp_filter && j->filter_prog) {
629 struct sock_fprog *fp = j->filter_prog;
630 marshal_append(state, (char *)fp->filter,
631 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400632 }
Dylan Reid648b2202015-10-23 00:50:00 -0700633 for (m = j->mounts_head; m; m = m->next) {
634 marshal_append(state, m->src, strlen(m->src) + 1);
635 marshal_append(state, m->dest, strlen(m->dest) + 1);
636 marshal_append(state, m->type, strlen(m->type) + 1);
637 marshal_append(state, (char *)&m->flags, sizeof(m->flags));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400638 }
Will Drewryf89aef52011-09-16 16:48:57 -0500639}
640
Will Drewry6ac91122011-10-21 16:38:58 -0500641size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400642{
643 struct marshal_state state;
644 marshal_state_init(&state, NULL, 0);
645 minijail_marshal_helper(&state, j);
646 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500647}
648
Elly Jonese1749eb2011-10-07 13:54:59 -0400649int minijail_marshal(const struct minijail *j, char *buf, size_t available)
650{
651 struct marshal_state state;
652 marshal_state_init(&state, buf, available);
653 minijail_marshal_helper(&state, j);
654 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500655}
656
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800657/*
658 * consumebytes: consumes @length bytes from a buffer @buf of length @buflength
Elly Jones51a5b6c2011-10-12 19:09:26 -0400659 * @length Number of bytes to consume
660 * @buf Buffer to consume from
661 * @buflength Size of @buf
662 *
663 * Returns a pointer to the base of the bytes, or NULL for errors.
664 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700665void *consumebytes(size_t length, char **buf, size_t *buflength)
666{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400667 char *p = *buf;
668 if (length > *buflength)
669 return NULL;
670 *buf += length;
671 *buflength -= length;
672 return p;
673}
674
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800675/*
676 * consumestr: consumes a C string from a buffer @buf of length @length
Elly Jones51a5b6c2011-10-12 19:09:26 -0400677 * @buf Buffer to consume
678 * @length Length of buffer
679 *
680 * Returns a pointer to the base of the string, or NULL for errors.
681 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700682char *consumestr(char **buf, size_t *buflength)
683{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400684 size_t len = strnlen(*buf, *buflength);
685 if (len == *buflength)
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700686 /* There's no null-terminator. */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400687 return NULL;
688 return consumebytes(len + 1, buf, buflength);
689}
690
Elly Jonese1749eb2011-10-07 13:54:59 -0400691int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
692{
Jorge Lucangeli Obesc2ba9f52015-12-01 07:58:10 -0800693 size_t i;
694 size_t count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500695 int ret = -EINVAL;
696
Elly Jonese1749eb2011-10-07 13:54:59 -0400697 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500698 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400699 memcpy((void *)j, serialized, sizeof(*j));
700 serialized += sizeof(*j);
701 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500702
Will Drewrybee7ba72011-10-21 20:47:01 -0500703 /* Potentially stale pointers not used as signals. */
Dylan Reid648b2202015-10-23 00:50:00 -0700704 j->mounts_head = NULL;
705 j->mounts_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800706 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500707
Elly Jonese1749eb2011-10-07 13:54:59 -0400708 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400709 char *user = consumestr(&serialized, &length);
710 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500711 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400712 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500713 if (!j->user)
714 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400715 }
Will Drewryf89aef52011-09-16 16:48:57 -0500716
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400717 if (j->chrootdir) { /* stale pointer */
718 char *chrootdir = consumestr(&serialized, &length);
719 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500720 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400721 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500722 if (!j->chrootdir)
723 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400724 }
725
Andrew Brestickereac28942015-11-11 16:04:46 -0800726 if (j->alt_syscall_table) { /* stale pointer */
727 char *alt_syscall_table = consumestr(&serialized, &length);
728 if (!alt_syscall_table)
729 goto bad_syscall_table;
730 j->alt_syscall_table = strdup(alt_syscall_table);
731 if (!j->alt_syscall_table)
732 goto bad_syscall_table;
733 }
734
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800735 if (j->flags.seccomp_filter && j->filter_len > 0) {
736 size_t ninstrs = j->filter_len;
737 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
738 ninstrs > USHRT_MAX)
739 goto bad_filters;
740
741 size_t program_len = ninstrs * sizeof(struct sock_filter);
742 void *program = consumebytes(program_len, &serialized, &length);
743 if (!program)
744 goto bad_filters;
745
746 j->filter_prog = malloc(sizeof(struct sock_fprog));
747 j->filter_prog->len = ninstrs;
748 j->filter_prog->filter = malloc(program_len);
749 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400750 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400751
Dylan Reid648b2202015-10-23 00:50:00 -0700752 count = j->mounts_count;
753 j->mounts_count = 0;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400754 for (i = 0; i < count; ++i) {
Dylan Reid648b2202015-10-23 00:50:00 -0700755 unsigned long *flags;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400756 const char *dest;
Dylan Reid648b2202015-10-23 00:50:00 -0700757 const char *type;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400758 const char *src = consumestr(&serialized, &length);
759 if (!src)
Dylan Reid648b2202015-10-23 00:50:00 -0700760 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400761 dest = consumestr(&serialized, &length);
762 if (!dest)
Dylan Reid648b2202015-10-23 00:50:00 -0700763 goto bad_mounts;
764 type = consumestr(&serialized, &length);
765 if (!type)
766 goto bad_mounts;
767 flags = consumebytes(sizeof(*flags), &serialized, &length);
768 if (!flags)
769 goto bad_mounts;
770 if (minijail_mount(j, src, dest, type, *flags))
771 goto bad_mounts;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400772 }
773
Elly Jonese1749eb2011-10-07 13:54:59 -0400774 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500775
Dylan Reid648b2202015-10-23 00:50:00 -0700776bad_mounts:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800777 if (j->flags.seccomp_filter && j->filter_len > 0) {
778 free(j->filter_prog->filter);
779 free(j->filter_prog);
780 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500781bad_filters:
Andrew Brestickereac28942015-11-11 16:04:46 -0800782 if (j->alt_syscall_table)
783 free(j->alt_syscall_table);
784bad_syscall_table:
Will Drewrybee7ba72011-10-21 20:47:01 -0500785 if (j->chrootdir)
786 free(j->chrootdir);
787bad_chrootdir:
788 if (j->user)
789 free(j->user);
790clear_pointers:
791 j->user = NULL;
792 j->chrootdir = NULL;
Andrew Brestickereac28942015-11-11 16:04:46 -0800793 j->alt_syscall_table = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500794out:
795 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500796}
797
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800798static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
799{
800 int fd, ret, len;
801 size_t sz;
802 char fname[32];
803 close(pipe_fds[0]);
804
805 sz = sizeof(fname);
806 if (j->uidmap) {
807 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700808 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800809 die("failed to write file name of uid_map");
810 fd = open(fname, O_WRONLY);
811 if (fd < 0)
812 pdie("failed to open '%s'", fname);
813 len = strlen(j->uidmap);
814 if (write(fd, j->uidmap, len) < len)
815 die("failed to set uid_map");
816 close(fd);
817 }
818 if (j->gidmap) {
819 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -0700820 if (ret < 0 || (size_t)ret >= sz)
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800821 die("failed to write file name of gid_map");
822 fd = open(fname, O_WRONLY);
823 if (fd < 0)
824 pdie("failed to open '%s'", fname);
825 len = strlen(j->gidmap);
826 if (write(fd, j->gidmap, len) < len)
827 die("failed to set gid_map");
828 close(fd);
829 }
830
831 close(pipe_fds[1]);
832}
833
834static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
835{
836 char buf;
837
838 close(pipe_fds[1]);
839
840 /* Wait for parent to set up uid/gid mappings. */
841 if (read(pipe_fds[0], &buf, 1) != 0)
842 die("failed to sync with parent");
843 close(pipe_fds[0]);
844
845 if (j->uidmap && setresuid(0, 0, 0))
846 pdie("setresuid");
847 if (j->gidmap && setresgid(0, 0, 0))
848 pdie("setresgid");
849}
850
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800851/*
852 * mount_one: Applies mounts from @m for @j, recursing as needed.
Dylan Reid648b2202015-10-23 00:50:00 -0700853 * @j Minijail these mounts are for
854 * @m Head of list of mounts
Elly Jones51a5b6c2011-10-12 19:09:26 -0400855 *
856 * Returns 0 for success.
857 */
Dylan Reid648b2202015-10-23 00:50:00 -0700858static int mount_one(const struct minijail *j, struct mountpoint *m)
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700859{
Dylan Reid648b2202015-10-23 00:50:00 -0700860 int ret;
861 char *dest;
862 int remount_ro = 0;
863
Elly Jones51a5b6c2011-10-12 19:09:26 -0400864 /* dest has a leading "/" */
Dylan Reid648b2202015-10-23 00:50:00 -0700865 if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0)
Elly Jones51a5b6c2011-10-12 19:09:26 -0400866 return -ENOMEM;
Dylan Reid648b2202015-10-23 00:50:00 -0700867
868 /*
869 * R/O bind mounts have to be remounted since bind and ro can't both be
870 * specified in the original bind mount. Remount R/O after the initial
871 * mount.
872 */
873 if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) {
874 remount_ro = 1;
875 m->flags &= ~MS_RDONLY;
Elly Jonesa1059632011-12-15 15:17:07 -0500876 }
Dylan Reid648b2202015-10-23 00:50:00 -0700877
878 ret = mount(m->src, dest, m->type, m->flags, NULL);
879 if (ret)
880 pdie("mount: %s -> %s", m->src, dest);
881
882 if (remount_ro) {
883 m->flags |= MS_RDONLY;
884 ret = mount(m->src, dest, NULL,
885 m->flags | MS_REMOUNT, NULL);
886 if (ret)
887 pdie("bind ro: %s -> %s", m->src, dest);
888 }
889
Elly Jones51a5b6c2011-10-12 19:09:26 -0400890 free(dest);
Dylan Reid648b2202015-10-23 00:50:00 -0700891 if (m->next)
892 return mount_one(j, m->next);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400893 return ret;
894}
895
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700896int enter_chroot(const struct minijail *j)
897{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400898 int ret;
Dylan Reid648b2202015-10-23 00:50:00 -0700899
900 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Elly Jones51a5b6c2011-10-12 19:09:26 -0400901 return ret;
902
903 if (chroot(j->chrootdir))
904 return -errno;
905
906 if (chdir("/"))
907 return -errno;
908
909 return 0;
910}
911
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800912int enter_pivot_root(const struct minijail *j)
913{
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800914 int ret, oldroot, newroot;
Dylan Reid648b2202015-10-23 00:50:00 -0700915
916 if (j->mounts_head && (ret = mount_one(j, j->mounts_head)))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800917 return ret;
918
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800919 /*
920 * Keep the fd for both old and new root.
921 * It will be used in fchdir later.
922 */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800923 oldroot = open("/", O_DIRECTORY | O_RDONLY);
924 if (oldroot < 0)
925 pdie("failed to open / for fchdir");
926 newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY);
927 if (newroot < 0)
928 pdie("failed to open %s for fchdir", j->chrootdir);
929
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800930 /*
931 * To ensure chrootdir is the root of a file system,
932 * do a self bind mount.
933 */
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800934 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
935 pdie("failed to bind mount '%s'", j->chrootdir);
936 if (chdir(j->chrootdir))
937 return -errno;
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800938 if (syscall(SYS_pivot_root, ".", "."))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800939 pdie("pivot_root");
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800940
941 /*
942 * Now the old root is mounted on top of the new root. Use fchdir to
943 * change to the old root and unmount it.
944 */
945 if (fchdir(oldroot))
946 pdie("failed to fchdir to old /");
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800947 /* The old root might be busy, so use lazy unmount. */
Yu-Hsi Chiange0a530e2015-09-08 18:49:49 +0800948 if (umount2(".", MNT_DETACH))
949 pdie("umount(/)");
950 /* Change back to the new root. */
951 if (fchdir(newroot))
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800952 return -errno;
953 if (chroot("/"))
954 return -errno;
Jorge Lucangeli Obes46a55092015-10-12 15:31:59 -0700955 /* Set correct CWD for getcwd(3). */
956 if (chdir("/"))
957 return -errno;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800958
959 return 0;
960}
961
Lee Campbell11af0622014-05-22 12:36:04 -0700962int mount_tmp(void)
963{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800964 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700965}
966
Dylan Reid791f5772015-09-14 20:02:42 -0700967int remount_proc_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400968{
969 const char *kProcPath = "/proc";
970 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500971 /*
972 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400973 * /proc in our namespace, which means using MS_REMOUNT here would
974 * mutate our parent's mount as well, even though we're in a VFS
975 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800976 * and make our own. However, if we are in a new user namespace, /proc
977 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400978 */
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -0700979 if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400980 return -errno;
981 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
982 return -errno;
983 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400984}
985
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800986static void write_pid_file(const struct minijail *j)
987{
988 FILE *fp = fopen(j->pid_file_path, "w");
989
990 if (!fp)
991 pdie("failed to open '%s'", j->pid_file_path);
992 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
993 pdie("fprintf(%s)", j->pid_file_path);
994 if (fclose(fp))
995 pdie("fclose(%s)", j->pid_file_path);
996}
997
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700998void drop_ugid(const struct minijail *j)
999{
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001000 if (j->flags.usergroups && j->flags.suppl_gids) {
1001 die("tried to inherit *and* set supplementary groups;"
1002 " can only do one");
1003 }
1004
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001005 if (j->flags.usergroups) {
1006 if (initgroups(j->user, j->usergid))
1007 pdie("initgroups");
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001008 } else if (j->flags.suppl_gids) {
1009 if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) {
1010 pdie("setgroups");
1011 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001012 } else {
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001013 /*
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -08001014 * Only attempt to clear supplementary groups if we are changing
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001015 * users.
1016 */
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001017 if ((j->uid || j->gid) && setgroups(0, NULL))
1018 pdie("setgroups");
1019 }
1020
1021 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
1022 pdie("setresgid");
1023
1024 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
1025 pdie("setresuid");
1026}
1027
Mike Frysinger3adfef72013-05-09 17:19:08 -04001028/*
1029 * We specifically do not use cap_valid() as that only tells us the last
1030 * valid cap we were *compiled* against (i.e. what the version of kernel
1031 * headers says). If we run on a different kernel version, then it's not
1032 * uncommon for that to be less (if an older kernel) or more (if a newer
1033 * kernel). So suck up the answer via /proc.
1034 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001035static unsigned int get_last_valid_cap()
Mike Frysinger3adfef72013-05-09 17:19:08 -04001036{
Dylan Reidf682d472015-09-17 21:39:07 -07001037 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
1038 FILE *fp = fopen(cap_file, "re");
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001039 unsigned int last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001040
Dylan Reidf682d472015-09-17 21:39:07 -07001041 if (fscanf(fp, "%u", &last_valid_cap) != 1)
1042 pdie("fscanf(%s)", cap_file);
1043 fclose(fp);
Mike Frysinger3adfef72013-05-09 17:19:08 -04001044
Dylan Reidf682d472015-09-17 21:39:07 -07001045 return last_valid_cap;
Mike Frysinger3adfef72013-05-09 17:19:08 -04001046}
1047
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001048void drop_caps(const struct minijail *j, unsigned int last_valid_cap)
Elly Jonese1749eb2011-10-07 13:54:59 -04001049{
1050 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -08001051 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -08001052 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -04001053 unsigned int i;
1054 if (!caps)
1055 die("can't get process caps");
1056 if (cap_clear_flag(caps, CAP_INHERITABLE))
1057 die("can't clear inheritable caps");
1058 if (cap_clear_flag(caps, CAP_EFFECTIVE))
1059 die("can't clear effective caps");
1060 if (cap_clear_flag(caps, CAP_PERMITTED))
1061 die("can't clear permitted caps");
Dylan Reidf682d472015-09-17 21:39:07 -07001062 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cook323878a2013-02-05 15:35:24 -08001063 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001064 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -04001065 continue;
Kees Cook323878a2013-02-05 15:35:24 -08001066 flag[0] = i;
1067 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001068 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -08001069 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001070 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -08001071 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -04001072 die("can't add inheritable cap");
1073 }
1074 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -08001075 die("can't apply initial cleaned capset");
1076
1077 /*
1078 * Instead of dropping bounding set first, do it here in case
1079 * the caller had a more permissive bounding set which could
1080 * have been used above to raise a capability that wasn't already
1081 * present. This requires CAP_SETPCAP, so we raised/kept it above.
1082 */
Dylan Reidf682d472015-09-17 21:39:07 -07001083 for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -08001084 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -04001085 continue;
1086 if (prctl(PR_CAPBSET_DROP, i))
1087 pdie("prctl(PR_CAPBSET_DROP)");
1088 }
Kees Cook323878a2013-02-05 15:35:24 -08001089
1090 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -08001091 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -08001092 flag[0] = CAP_SETPCAP;
1093 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
1094 die("can't clear effective cap");
1095 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
1096 die("can't clear permitted cap");
1097 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
1098 die("can't clear inheritable cap");
1099 }
1100
1101 if (cap_set_proc(caps))
1102 die("can't apply final cleaned capset");
1103
1104 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -04001105}
1106
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001107void set_seccomp_filter(const struct minijail *j)
1108{
1109 /*
1110 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
1111 * in the kernel source tree for an explanation of the parameters.
1112 */
1113 if (j->flags.no_new_privs) {
1114 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1115 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
1116 }
1117
1118 /*
1119 * If we're logging seccomp filter failures,
1120 * install the SIGSYS handler first.
1121 */
1122 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
1123 if (install_sigsys_handler())
1124 pdie("install SIGSYS handler");
1125 warn("logging seccomp filter failures");
1126 }
1127
1128 /*
1129 * Install the syscall filter.
1130 */
1131 if (j->flags.seccomp_filter) {
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001132 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER,
1133 j->filter_prog)) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001134 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1135 warn("seccomp not supported");
1136 return;
1137 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001138 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001139 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001140 }
1141}
1142
Will Drewry6ac91122011-10-21 16:38:58 -05001143void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001144{
Dylan Reidf682d472015-09-17 21:39:07 -07001145 /*
1146 * Get the last valid cap from /proc, since /proc can be unmounted
1147 * before drop_caps().
1148 */
Jorge Lucangeli Obes20342742015-10-27 11:39:59 -07001149 unsigned int last_valid_cap = get_last_valid_cap();
Dylan Reidf682d472015-09-17 21:39:07 -07001150
Elly Jonese1749eb2011-10-07 13:54:59 -04001151 if (j->flags.pids)
1152 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001153 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -04001154
Elly Jonese1749eb2011-10-07 13:54:59 -04001155 if (j->flags.usergroups && !j->user)
1156 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -04001157
Elly Jonesdd3e8512012-01-23 15:13:38 -05001158 /*
1159 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -04001160 * so we don't even try. If any of our operations fail, we abort() the
1161 * entire process.
1162 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -07001163 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
1164 pdie("setns(CLONE_NEWNS)");
1165
Jorge Lucangeli Obes805be392015-10-12 15:55:59 -07001166 if (j->flags.vfs) {
Jorge Lucangeli Obesf7a38682015-12-04 15:43:30 -08001167 if (unshare(CLONE_NEWNS))
1168 pdie("unshare(vfs)");
1169 /*
1170 * Remount all filesystems as private. If they are shared
1171 * new bind mounts will creep out of our namespace.
1172 * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
1173 */
1174 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL))
1175 pdie("mount(/, private)");
1176 }
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001177
Dylan Reidf7942472015-11-18 17:55:26 -08001178 if (j->flags.ipc && unshare(CLONE_NEWIPC)) {
1179 pdie("unshare(ipc)");
1180 }
1181
Dylan Reid1102f5a2015-09-15 11:52:20 -07001182 if (j->flags.enter_net) {
1183 if (setns(j->netns_fd, CLONE_NEWNET))
1184 pdie("setns(CLONE_NEWNET)");
1185 } else if (j->flags.net && unshare(CLONE_NEWNET)) {
Elly Fong-Jones6c086302013-03-20 17:15:28 -04001186 pdie("unshare(net)");
Dylan Reid1102f5a2015-09-15 11:52:20 -07001187 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001188
Elly Jones51a5b6c2011-10-12 19:09:26 -04001189 if (j->flags.chroot && enter_chroot(j))
1190 pdie("chroot");
1191
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +08001192 if (j->flags.pivot_root && enter_pivot_root(j))
1193 pdie("pivot_root");
1194
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -08001195 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -07001196 pdie("mount_tmp");
1197
Dylan Reid791f5772015-09-14 20:02:42 -07001198 if (j->flags.remount_proc_ro && remount_proc_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -04001199 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -04001200
Elly Jonese1749eb2011-10-07 13:54:59 -04001201 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001202 /*
1203 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -04001204 * capability to change uids, our attempt to use setuid()
1205 * below will fail. Hang on to root caps across setuid(), then
1206 * lock securebits.
1207 */
1208 if (prctl(PR_SET_KEEPCAPS, 1))
1209 pdie("prctl(PR_SET_KEEPCAPS)");
1210 if (prctl
1211 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
1212 pdie("prctl(PR_SET_SECUREBITS)");
1213 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001214
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001215 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001216 * If we're setting no_new_privs, we can drop privileges
1217 * before setting seccomp filter. This way filter policies
1218 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001219 */
1220 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001221 drop_ugid(j);
1222 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001223 drop_caps(j, last_valid_cap);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001224
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001225 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001226 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001227 /*
1228 * If we're not setting no_new_privs,
1229 * we need to set seccomp filter *before* dropping privileges.
1230 * WARNING: this means that filter policies *must* allow
1231 * setgroups()/setresgid()/setresuid() for dropping root and
1232 * capget()/capset()/prctl() for dropping caps.
1233 */
1234 set_seccomp_filter(j);
1235
1236 drop_ugid(j);
1237 if (j->flags.caps)
Dylan Reidf682d472015-09-17 21:39:07 -07001238 drop_caps(j, last_valid_cap);
Elly Jonese1749eb2011-10-07 13:54:59 -04001239 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001240
Elly Jonesdd3e8512012-01-23 15:13:38 -05001241 /*
Andrew Brestickereac28942015-11-11 16:04:46 -08001242 * Select the specified alternate syscall table. The table must not
1243 * block prctl(2) if we're using seccomp as well.
1244 */
1245 if (j->flags.alt_syscall) {
1246 if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table))
1247 pdie("prctl(PR_ALT_SYSCALL)");
1248 }
1249
1250 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001251 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001252 * privilege-dropping syscalls :)
1253 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001254 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1255 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1256 warn("seccomp not supported");
1257 return;
1258 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001259 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001260 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001261}
1262
Will Drewry6ac91122011-10-21 16:38:58 -05001263/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001264static int init_exitstatus = 0;
1265
Will Drewry6ac91122011-10-21 16:38:58 -05001266void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001267{
1268 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001269}
1270
Will Drewry6ac91122011-10-21 16:38:58 -05001271int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001272{
1273 pid_t pid;
1274 int status;
1275 /* so that we exit with the right status */
1276 signal(SIGTERM, init_term);
1277 /* TODO(wad) self jail with seccomp_filters here. */
1278 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001279 /*
1280 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001281 * left inside our pid namespace or we get a signal.
1282 */
1283 if (pid == rootpid)
1284 init_exitstatus = status;
1285 }
1286 if (!WIFEXITED(init_exitstatus))
1287 _exit(MINIJAIL_ERR_INIT);
1288 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001289}
1290
Will Drewry6ac91122011-10-21 16:38:58 -05001291int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001292{
1293 size_t sz = 0;
1294 size_t bytes = read(fd, &sz, sizeof(sz));
1295 char *buf;
1296 int r;
1297 if (sizeof(sz) != bytes)
1298 return -EINVAL;
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001299 if (sz > USHRT_MAX) /* arbitrary sanity check */
Elly Jonese1749eb2011-10-07 13:54:59 -04001300 return -E2BIG;
1301 buf = malloc(sz);
1302 if (!buf)
1303 return -ENOMEM;
1304 bytes = read(fd, buf, sz);
1305 if (bytes != sz) {
1306 free(buf);
1307 return -EINVAL;
1308 }
1309 r = minijail_unmarshal(j, buf, sz);
1310 free(buf);
1311 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001312}
1313
Will Drewry6ac91122011-10-21 16:38:58 -05001314int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001315{
1316 char *buf;
1317 size_t sz = minijail_size(j);
1318 ssize_t written;
1319 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001320
Elly Jonese1749eb2011-10-07 13:54:59 -04001321 if (!sz)
1322 return -EINVAL;
1323 buf = malloc(sz);
1324 r = minijail_marshal(j, buf, sz);
1325 if (r) {
1326 free(buf);
1327 return r;
1328 }
1329 /* Sends [size][minijail]. */
1330 written = write(fd, &sz, sizeof(sz));
1331 if (written != sizeof(sz)) {
1332 free(buf);
1333 return -EFAULT;
1334 }
1335 written = write(fd, buf, sz);
1336 if (written < 0 || (size_t) written != sz) {
1337 free(buf);
1338 return -EFAULT;
1339 }
1340 free(buf);
1341 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001342}
Elly Jonescd7a9042011-07-22 13:56:51 -04001343
Will Drewry6ac91122011-10-21 16:38:58 -05001344int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001345{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001346#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001347 /* Don't use LDPRELOAD on Brillo. */
1348 return 0;
1349#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001350 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1351 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1352 if (!newenv)
1353 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001354
Elly Jonese1749eb2011-10-07 13:54:59 -04001355 /* Only insert a separating space if we have something to separate... */
1356 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1357 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001358
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001359 /* setenv() makes a copy of the string we give it. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001360 setenv(kLdPreloadEnvVar, newenv, 1);
1361 free(newenv);
1362 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001363#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001364}
1365
Will Drewry6ac91122011-10-21 16:38:58 -05001366int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001367{
1368 int r = pipe(fds);
1369 char fd_buf[11];
1370 if (r)
1371 return r;
1372 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1373 if (r <= 0)
1374 return -EINVAL;
1375 setenv(kFdEnvVar, fd_buf, 1);
1376 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001377}
1378
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001379int setup_pipe_end(int fds[2], size_t index)
1380{
1381 if (index > 1)
1382 return -1;
1383
1384 close(fds[1 - index]);
1385 return fds[index];
1386}
1387
1388int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1389{
1390 if (index > 1)
1391 return -1;
1392
1393 close(fds[1 - index]);
1394 /* dup2(2) the corresponding end of the pipe into |fd|. */
1395 return dup2(fds[index], fd);
1396}
1397
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001398int minijail_run_internal(struct minijail *j, const char *filename,
1399 char *const argv[], pid_t *pchild_pid,
1400 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1401 int use_preload);
1402
Will Drewry6ac91122011-10-21 16:38:58 -05001403int API minijail_run(struct minijail *j, const char *filename,
1404 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001405{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001406 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1407 true);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001408}
1409
1410int API minijail_run_pid(struct minijail *j, const char *filename,
1411 char *const argv[], pid_t *pchild_pid)
1412{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001413 return minijail_run_internal(j, filename, argv, pchild_pid,
1414 NULL, NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001415}
1416
1417int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001418 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001419{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001420 return minijail_run_internal(j, filename, argv, NULL, pstdin_fd,
1421 NULL, NULL, true);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001422}
1423
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001424int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001425 char *const argv[], pid_t *pchild_pid,
1426 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001427{
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001428 return minijail_run_internal(j, filename, argv, pchild_pid,
1429 pstdin_fd, pstdout_fd, pstderr_fd, true);
1430}
1431
1432int API minijail_run_no_preload(struct minijail *j, const char *filename,
1433 char *const argv[])
1434{
1435 return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL,
1436 false);
1437}
1438
Samuel Tan63187f42015-10-16 13:01:53 -07001439int API minijail_run_pid_pipes_no_preload(struct minijail *j,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001440 const char *filename,
1441 char *const argv[],
Samuel Tan63187f42015-10-16 13:01:53 -07001442 pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -08001443 int *pstdin_fd, int *pstdout_fd,
1444 int *pstderr_fd) {
Samuel Tan63187f42015-10-16 13:01:53 -07001445 return minijail_run_internal(j, filename, argv, pchild_pid,
1446 pstdin_fd, pstdout_fd, pstderr_fd, false);
1447}
1448
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001449int minijail_run_internal(struct minijail *j, const char *filename,
1450 char *const argv[], pid_t *pchild_pid,
1451 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd,
1452 int use_preload)
1453{
Elly Jonese1749eb2011-10-07 13:54:59 -04001454 char *oldenv, *oldenv_copy = NULL;
1455 pid_t child_pid;
1456 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001457 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001458 int stdout_fds[2];
1459 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001460 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001461 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001462 /* We need to remember this across the minijail_preexec() call. */
1463 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001464 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001465
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001466 if (use_preload) {
1467 oldenv = getenv(kLdPreloadEnvVar);
1468 if (oldenv) {
1469 oldenv_copy = strdup(oldenv);
1470 if (!oldenv_copy)
1471 return -ENOMEM;
1472 }
1473
1474 if (setup_preload())
1475 return -EFAULT;
Elly Jonese1749eb2011-10-07 13:54:59 -04001476 }
Will Drewryf89aef52011-09-16 16:48:57 -05001477
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001478 if (!use_preload) {
1479 if (j->flags.caps)
1480 die("Capabilities are not supported without "
1481 "LD_PRELOAD");
1482 }
Will Drewry2f54b6a2011-09-16 13:45:31 -05001483
Elly Jonesdd3e8512012-01-23 15:13:38 -05001484 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001485 * Make the process group ID of this process equal to its PID, so that
1486 * both the Minijail process and the jailed process can be killed
1487 * together.
1488 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1489 * the process is already a process group leader.
1490 */
1491 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1492 if (errno != EPERM) {
1493 pdie("setpgid(0, 0)");
1494 }
1495 }
1496
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001497 if (use_preload) {
1498 /*
1499 * Before we fork(2) and execve(2) the child process, we need
1500 * to open a pipe(2) to send the minijail configuration over.
1501 */
1502 if (setup_pipe(pipe_fds))
1503 return -EFAULT;
1504 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001505
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001506 /*
1507 * If we want to write to the child process' standard input,
1508 * create the pipe(2) now.
1509 */
1510 if (pstdin_fd) {
1511 if (pipe(stdin_fds))
1512 return -EFAULT;
1513 }
1514
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001515 /*
1516 * If we want to read from the child process' standard output,
1517 * create the pipe(2) now.
1518 */
1519 if (pstdout_fd) {
1520 if (pipe(stdout_fds))
1521 return -EFAULT;
1522 }
1523
1524 /*
1525 * If we want to read from the child process' standard error,
1526 * create the pipe(2) now.
1527 */
1528 if (pstderr_fd) {
1529 if (pipe(stderr_fds))
1530 return -EFAULT;
1531 }
1532
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001533 /*
1534 * If we want to set up a new uid/gid mapping in the user namespace,
1535 * create the pipe(2) to sync between parent and child.
1536 */
1537 if (j->flags.userns) {
1538 if (pipe(userns_pipe_fds))
1539 return -EFAULT;
1540 }
1541
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08001542 /*
1543 * Use sys_clone() if and only if we're creating a pid namespace.
Elly Jones761b7412012-06-13 15:49:52 -04001544 *
1545 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1546 *
1547 * In multithreaded programs, there are a bunch of locks inside libc,
1548 * some of which may be held by other threads at the time that we call
1549 * minijail_run_pid(). If we call fork(), glibc does its level best to
1550 * ensure that we hold all of these locks before it calls clone()
1551 * internally and drop them after clone() returns, but when we call
1552 * sys_clone(2) directly, all that gets bypassed and we end up with a
1553 * child address space where some of libc's important locks are held by
1554 * other threads (which did not get cloned, and hence will never release
1555 * those locks). This is okay so long as we call exec() immediately
1556 * after, but a bunch of seemingly-innocent libc functions like setenv()
1557 * take locks.
1558 *
1559 * Hence, only call sys_clone() if we need to, in order to get at pid
1560 * namespacing. If we follow this path, the child's address space might
1561 * have broken locks; you may only call functions that do not acquire
1562 * any locks.
1563 *
1564 * Unfortunately, fork() acquires every lock it can get its hands on, as
1565 * previously detailed, so this function is highly likely to deadlock
1566 * later on (see "deadlock here") if we're multithreaded.
1567 *
1568 * We might hack around this by having the clone()d child (init of the
1569 * pid namespace) return directly, rather than leaving the clone()d
1570 * process hanging around to be init for the new namespace (and having
1571 * its fork()ed child return in turn), but that process would be crippled
1572 * with its libc locks potentially broken. We might try fork()ing in the
1573 * parent before we clone() to ensure that we own all the locks, but
1574 * then we have to have the forked child hanging around consuming
1575 * resources (and possibly having file descriptors / shared memory
1576 * regions / etc attached). We'd need to keep the child around to avoid
1577 * having its children get reparented to init.
1578 *
1579 * TODO(ellyjones): figure out if the "forked child hanging around"
1580 * problem is fixable or not. It would be nice if we worked in this
1581 * case.
1582 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001583 if (pid_namespace) {
1584 int clone_flags = CLONE_NEWPID | SIGCHLD;
1585 if (j->flags.userns)
1586 clone_flags |= CLONE_NEWUSER;
1587 child_pid = syscall(SYS_clone, clone_flags, NULL);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001588 } else {
Elly Jones761b7412012-06-13 15:49:52 -04001589 child_pid = fork();
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001590 }
Elly Jones761b7412012-06-13 15:49:52 -04001591
Elly Jonese1749eb2011-10-07 13:54:59 -04001592 if (child_pid < 0) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001593 if (use_preload) {
1594 free(oldenv_copy);
1595 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001596 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001597 }
Will Drewryf89aef52011-09-16 16:48:57 -05001598
Elly Jonese1749eb2011-10-07 13:54:59 -04001599 if (child_pid) {
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001600 if (use_preload) {
1601 /* Restore parent's LD_PRELOAD. */
1602 if (oldenv_copy) {
1603 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1604 free(oldenv_copy);
1605 } else {
1606 unsetenv(kLdPreloadEnvVar);
1607 }
1608 unsetenv(kFdEnvVar);
Elly Jonese1749eb2011-10-07 13:54:59 -04001609 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001610
Elly Jonese1749eb2011-10-07 13:54:59 -04001611 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001612
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001613 if (j->flags.pid_file)
1614 write_pid_file(j);
1615
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001616 if (j->flags.userns)
1617 write_ugid_mappings(j, userns_pipe_fds);
1618
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001619 if (use_preload) {
1620 /* Send marshalled minijail. */
1621 close(pipe_fds[0]); /* read endpoint */
1622 ret = minijail_to_fd(j, pipe_fds[1]);
1623 close(pipe_fds[1]); /* write endpoint */
1624 if (ret) {
1625 kill(j->initpid, SIGKILL);
1626 die("failed to send marshalled minijail");
1627 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001628 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001629
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001630 if (pchild_pid)
1631 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001632
1633 /*
1634 * If we want to write to the child process' standard input,
1635 * set up the write end of the pipe.
1636 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001637 if (pstdin_fd)
1638 *pstdin_fd = setup_pipe_end(stdin_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001639 1 /* write end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001640
1641 /*
1642 * If we want to read from the child process' standard output,
1643 * set up the read end of the pipe.
1644 */
1645 if (pstdout_fd)
1646 *pstdout_fd = setup_pipe_end(stdout_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001647 0 /* read end */);
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001648
1649 /*
1650 * If we want to read from the child process' standard error,
1651 * set up the read end of the pipe.
1652 */
1653 if (pstderr_fd)
1654 *pstderr_fd = setup_pipe_end(stderr_fds,
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001655 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001656
Elly Jonese1749eb2011-10-07 13:54:59 -04001657 return 0;
1658 }
1659 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001660
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001661 if (j->flags.userns)
1662 enter_user_namespace(j, userns_pipe_fds);
1663
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001664 /*
1665 * If we want to write to the jailed process' standard input,
1666 * set up the read end of the pipe.
1667 */
1668 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001669 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1670 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001671 die("failed to set up stdin pipe");
1672 }
1673
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001674 /*
1675 * If we want to read from the jailed process' standard output,
1676 * set up the write end of the pipe.
1677 */
1678 if (pstdout_fd) {
1679 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1680 STDOUT_FILENO) < 0)
1681 die("failed to set up stdout pipe");
1682 }
1683
1684 /*
1685 * If we want to read from the jailed process' standard error,
1686 * set up the write end of the pipe.
1687 */
1688 if (pstderr_fd) {
1689 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1690 STDERR_FILENO) < 0)
1691 die("failed to set up stderr pipe");
1692 }
1693
Dylan Reid791f5772015-09-14 20:02:42 -07001694 /* If running an init program, let it decide when/how to mount /proc. */
1695 if (pid_namespace && !do_init)
1696 j->flags.remount_proc_ro = 0;
1697
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001698 if (use_preload) {
1699 /* Strip out flags that cannot be inherited across execve(2). */
1700 minijail_preexec(j);
1701 } else {
1702 j->flags.pids = 0;
1703 }
1704 /* Jail this process, then execve() the target. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001705 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001706
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001707 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001708 /*
1709 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001710 * namespace. We don't want all programs we might exec to have
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001711 * to know how to be init. Normally (do_init == 1) we fork off
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001712 * a child to actually run the program. If |do_init == 0|, we
1713 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001714 *
1715 * If we're multithreaded, we'll probably deadlock here. See
1716 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001717 */
1718 child_pid = fork();
1719 if (child_pid < 0)
1720 _exit(child_pid);
1721 else if (child_pid > 0)
1722 init(child_pid); /* never returns */
1723 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001724
Elly Jonesdd3e8512012-01-23 15:13:38 -05001725 /*
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -07001726 * If we aren't pid-namespaced, or the jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001727 * calling process
1728 * -> execve()-ing process
1729 * If we are:
1730 * calling process
1731 * -> init()-ing process
1732 * -> execve()-ing process
1733 */
1734 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001735}
1736
Will Drewry6ac91122011-10-21 16:38:58 -05001737int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001738{
1739 int st;
1740 if (kill(j->initpid, SIGTERM))
1741 return -errno;
1742 if (waitpid(j->initpid, &st, 0) < 0)
1743 return -errno;
1744 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001745}
1746
Will Drewry6ac91122011-10-21 16:38:58 -05001747int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001748{
1749 int st;
1750 if (waitpid(j->initpid, &st, 0) < 0)
1751 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001752
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001753 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001754 int error_status = st;
1755 if (WIFSIGNALED(st)) {
1756 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001757 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001758 j->initpid, signum);
1759 /*
1760 * We return MINIJAIL_ERR_JAIL if the process received
1761 * SIGSYS, which happens when a syscall is blocked by
1762 * seccomp filters.
1763 * If not, we do what bash(1) does:
1764 * $? = 128 + signum
1765 */
1766 if (signum == SIGSYS) {
1767 error_status = MINIJAIL_ERR_JAIL;
1768 } else {
1769 error_status = 128 + signum;
1770 }
1771 }
1772 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001773 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001774
1775 int exit_status = WEXITSTATUS(st);
1776 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001777 info("child process %d exited with status %d",
1778 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001779
1780 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001781}
1782
Will Drewry6ac91122011-10-21 16:38:58 -05001783void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001784{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001785 if (j->flags.seccomp_filter && j->filter_prog) {
1786 free(j->filter_prog->filter);
1787 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001788 }
Dylan Reid648b2202015-10-23 00:50:00 -07001789 while (j->mounts_head) {
1790 struct mountpoint *m = j->mounts_head;
1791 j->mounts_head = j->mounts_head->next;
1792 free(m->type);
1793 free(m->dest);
1794 free(m->src);
1795 free(m);
Elly Jones51a5b6c2011-10-12 19:09:26 -04001796 }
Dylan Reid648b2202015-10-23 00:50:00 -07001797 j->mounts_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001798 if (j->user)
1799 free(j->user);
Jorge Lucangeli Obese81a52f2015-12-04 16:05:23 -08001800 if (j->suppl_gid_list)
1801 free(j->suppl_gid_list);
Will Drewrybee7ba72011-10-21 20:47:01 -05001802 if (j->chrootdir)
1803 free(j->chrootdir);
Andrew Brestickereac28942015-11-11 16:04:46 -08001804 if (j->alt_syscall_table)
1805 free(j->alt_syscall_table);
Elly Jonese1749eb2011-10-07 13:54:59 -04001806 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001807}