blob: 8e05094cb98ae5243a9ba816c3c4d6c2cb649b8d [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 Obes524c0402012-01-17 11:30:23 -080021#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <syscall.h>
26#include <sys/capability.h>
27#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050028#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040029#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070030#include <sys/stat.h>
31#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070041#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042
Lei Zhangeee31552012-10-17 21:27:10 -070043#ifdef HAVE_SECUREBITS_H
44#include <linux/securebits.h>
45#else
46#define SECURE_ALL_BITS 0x15
47#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
48#endif
49
Will Drewry32ac9f52011-08-18 21:36:27 -050050/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070052# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080053#endif
54
55/* For seccomp_filter using BPF. */
56#ifndef PR_SET_NO_NEW_PRIVS
57# define PR_SET_NO_NEW_PRIVS 38
58#endif
59#ifndef SECCOMP_MODE_FILTER
60# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050061#endif
62
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070063#ifdef USE_SECCOMP_SOFTFAIL
64# define SECCOMP_SOFTFAIL 1
65#else
66# define SECCOMP_SOFTFAIL 0
67#endif
68
Elly Jones51a5b6c2011-10-12 19:09:26 -040069struct binding {
70 char *src;
71 char *dest;
72 int writeable;
73 struct binding *next;
74};
75
Will Drewryf89aef52011-09-16 16:48:57 -050076struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070077 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070078 * WARNING: if you add a flag here you need to make sure it's
79 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070080 */
Elly Jonese1749eb2011-10-07 13:54:59 -040081 struct {
82 int uid:1;
83 int gid:1;
84 int caps:1;
85 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070086 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040087 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040088 int net:1;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080089 int userns:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040090 int seccomp:1;
91 int readonly:1;
92 int usergroups:1;
93 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070094 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040095 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070096 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040097 int chroot:1;
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +080098 int pivot_root:1;
Lee Campbell11af0622014-05-22 12:36:04 -070099 int mount_tmp:1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800100 int do_init:1;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800101 int pid_file:1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400102 } flags;
103 uid_t uid;
104 gid_t gid;
105 gid_t usergid;
106 char *user;
107 uint64_t caps;
108 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700109 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800110 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400111 int binding_count;
112 char *chrootdir;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800113 char *pid_file_path;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800114 char *uidmap;
115 char *gidmap;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800116 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400117 struct binding *bindings_head;
118 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500119};
120
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700121/*
122 * Strip out flags meant for the parent.
123 * We keep things that are not inherited across execve(2) (e.g. capabilities),
124 * or are easier to set after execve(2) (e.g. seccomp filters).
125 */
126void minijail_preenter(struct minijail *j)
127{
128 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700129 j->flags.enter_vfs = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700130 j->flags.readonly = 0;
131 j->flags.pids = 0;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800132 j->flags.do_init = 0;
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800133 j->flags.pid_file = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700134}
135
136/*
137 * Strip out flags meant for the child.
138 * We keep things that are inherited across execve(2).
139 */
140void minijail_preexec(struct minijail *j)
141{
142 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700143 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700144 int readonly = j->flags.readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800145 int userns = j->flags.userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700146 if (j->user)
147 free(j->user);
148 j->user = NULL;
149 memset(&j->flags, 0, sizeof(j->flags));
150 /* Now restore anything we meant to keep. */
151 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700152 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700153 j->flags.readonly = readonly;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800154 j->flags.userns = userns;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700155 /* Note, |pids| will already have been used before this call. */
156}
157
158/* Minijail API. */
159
Will Drewry6ac91122011-10-21 16:38:58 -0500160struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400161{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400162 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400163}
164
Will Drewry6ac91122011-10-21 16:38:58 -0500165void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400166{
167 if (uid == 0)
168 die("useless change to uid 0");
169 j->uid = uid;
170 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400171}
172
Will Drewry6ac91122011-10-21 16:38:58 -0500173void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400174{
175 if (gid == 0)
176 die("useless change to gid 0");
177 j->gid = gid;
178 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400179}
180
Will Drewry6ac91122011-10-21 16:38:58 -0500181int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400182{
183 char *buf = NULL;
184 struct passwd pw;
185 struct passwd *ppw = NULL;
186 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
187 if (sz == -1)
188 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400189
Elly Jonesdd3e8512012-01-23 15:13:38 -0500190 /*
191 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400192 * the maximum needed size of the buffer, so we don't have to search.
193 */
194 buf = malloc(sz);
195 if (!buf)
196 return -ENOMEM;
197 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500198 /*
199 * We're safe to free the buffer here. The strings inside pw point
200 * inside buf, but we don't use any of them; this leaves the pointers
201 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
202 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400203 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700204 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400205 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700206 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400207 minijail_change_uid(j, ppw->pw_uid);
208 j->user = strdup(user);
209 if (!j->user)
210 return -ENOMEM;
211 j->usergid = ppw->pw_gid;
212 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400213}
214
Will Drewry6ac91122011-10-21 16:38:58 -0500215int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400216{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700217 char *buf = NULL;
Yabin Cui1b21c8f2015-07-22 10:34:45 -0700218 struct group gr;
219 struct group *pgr = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400220 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
221 if (sz == -1)
222 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400223
Elly Jonesdd3e8512012-01-23 15:13:38 -0500224 /*
225 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400226 * the maximum needed size of the buffer, so we don't have to search.
227 */
228 buf = malloc(sz);
229 if (!buf)
230 return -ENOMEM;
231 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500232 /*
233 * We're safe to free the buffer here. The strings inside gr point
234 * inside buf, but we don't use any of them; this leaves the pointers
235 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
236 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400237 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700238 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400239 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700240 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400241 minijail_change_gid(j, pgr->gr_gid);
242 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Will Drewry6ac91122011-10-21 16:38:58 -0500245void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400246{
247 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400248}
249
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700250void API minijail_no_new_privs(struct minijail *j)
251{
252 j->flags.no_new_privs = 1;
253}
254
Will Drewry6ac91122011-10-21 16:38:58 -0500255void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400256{
257 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500258}
259
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700260void API minijail_log_seccomp_filter_failures(struct minijail *j)
261{
262 j->flags.log_seccomp_filter = 1;
263}
264
Will Drewry6ac91122011-10-21 16:38:58 -0500265void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400266{
267 j->caps = capmask;
268 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400269}
270
Will Drewry6ac91122011-10-21 16:38:58 -0500271void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400272{
273 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400274}
275
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700276void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
277{
278 int ns_fd = open(ns_path, O_RDONLY);
279 if (ns_fd < 0) {
280 pdie("failed to open namespace '%s'", ns_path);
281 }
282 j->mountns_fd = ns_fd;
283 j->flags.enter_vfs = 1;
284}
285
Will Drewry6ac91122011-10-21 16:38:58 -0500286void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400287{
Elly Jonese58176c2012-01-23 11:46:17 -0500288 j->flags.vfs = 1;
289 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400290 j->flags.pids = 1;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800291 j->flags.do_init = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400292}
293
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400294void API minijail_namespace_net(struct minijail *j)
295{
296 j->flags.net = 1;
297}
298
Will Drewry6ac91122011-10-21 16:38:58 -0500299void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400300{
301 j->flags.vfs = 1;
302 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400303}
304
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800305void API minijail_namespace_user(struct minijail *j)
306{
307 j->flags.userns = 1;
308}
309
310int API minijail_uidmap(struct minijail *j, const char *uidmap)
311{
312 j->uidmap = strdup(uidmap);
313 if (!j->uidmap)
314 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800315 char *ch;
316 for (ch = j->uidmap; *ch; ch++) {
317 if (*ch == ',')
318 *ch = '\n';
319 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800320 return 0;
321}
322
323int API minijail_gidmap(struct minijail *j, const char *gidmap)
324{
325 j->gidmap = strdup(gidmap);
326 if (!j->gidmap)
327 return -ENOMEM;
Yu-Hsi Chiang1912c5b2015-08-31 18:59:49 +0800328 char *ch;
329 for (ch = j->gidmap; *ch; ch++) {
330 if (*ch == ',')
331 *ch = '\n';
332 }
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800333 return 0;
334}
335
Will Drewry6ac91122011-10-21 16:38:58 -0500336void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400337{
338 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400339}
340
Will Drewry6ac91122011-10-21 16:38:58 -0500341void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400342{
343 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400344}
345
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800346void API minijail_run_as_init(struct minijail *j)
347{
348 /*
349 * Since the jailed program will become 'init' in the new PID namespace,
350 * Minijail does not need to fork an 'init' process.
351 */
352 j->flags.do_init = 0;
353}
354
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700355int API minijail_enter_chroot(struct minijail *j, const char *dir)
356{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400357 if (j->chrootdir)
358 return -EINVAL;
359 j->chrootdir = strdup(dir);
360 if (!j->chrootdir)
361 return -ENOMEM;
362 j->flags.chroot = 1;
363 return 0;
364}
365
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800366int API minijail_enter_pivot_root(struct minijail *j, const char *dir)
367{
368 if (j->chrootdir)
369 return -EINVAL;
370 j->chrootdir = strdup(dir);
371 if (!j->chrootdir)
372 return -ENOMEM;
373 j->flags.pivot_root = 1;
374 return 0;
375}
376
Lee Campbell11af0622014-05-22 12:36:04 -0700377void API minijail_mount_tmp(struct minijail *j)
378{
379 j->flags.mount_tmp = 1;
380}
381
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800382int API minijail_write_pid_file(struct minijail *j, const char *path)
383{
384 j->pid_file_path = strdup(path);
385 if (!j->pid_file_path)
386 return -ENOMEM;
387 j->flags.pid_file = 1;
388 return 0;
389}
390
Will Drewry6ac91122011-10-21 16:38:58 -0500391int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700392 int writeable)
393{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400394 struct binding *b;
395
396 if (*dest != '/')
397 return -EINVAL;
398 b = calloc(1, sizeof(*b));
399 if (!b)
400 return -ENOMEM;
401 b->dest = strdup(dest);
402 if (!b->dest)
403 goto error;
404 b->src = strdup(src);
405 if (!b->src)
406 goto error;
407 b->writeable = writeable;
408
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700409 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400410
Elly Jonesdd3e8512012-01-23 15:13:38 -0500411 /*
412 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400413 * containing vfs namespace.
414 */
415 minijail_namespace_vfs(j);
416
417 if (j->bindings_tail)
418 j->bindings_tail->next = b;
419 else
420 j->bindings_head = b;
421 j->bindings_tail = b;
422 j->binding_count++;
423
424 return 0;
425
426error:
427 free(b->src);
428 free(b->dest);
429 free(b);
430 return -ENOMEM;
431}
432
Will Drewry6ac91122011-10-21 16:38:58 -0500433void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400434{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700435 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
436 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
437 warn("not loading seccomp filter, seccomp not supported");
438 return;
439 }
440 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400441 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800442 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700443 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400444 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800445
446 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700447 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
448 die("failed to compile seccomp filter BPF program in '%s'",
449 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800450 }
451
452 j->filter_len = fprog->len;
453 j->filter_prog = fprog;
454
Elly Jonese1749eb2011-10-07 13:54:59 -0400455 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500456}
457
Will Drewryf89aef52011-09-16 16:48:57 -0500458struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400459 size_t available;
460 size_t total;
461 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500462};
463
Will Drewry6ac91122011-10-21 16:38:58 -0500464void marshal_state_init(struct marshal_state *state,
465 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400466{
467 state->available = available;
468 state->buf = buf;
469 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500470}
471
Will Drewry6ac91122011-10-21 16:38:58 -0500472void marshal_append(struct marshal_state *state,
473 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400474{
475 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500476
Elly Jonese1749eb2011-10-07 13:54:59 -0400477 /* Up to |available| will be written. */
478 if (copy_len) {
479 memcpy(state->buf, src, copy_len);
480 state->buf += copy_len;
481 state->available -= copy_len;
482 }
483 /* |total| will contain the expected length. */
484 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500485}
486
Will Drewry6ac91122011-10-21 16:38:58 -0500487void minijail_marshal_helper(struct marshal_state *state,
488 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400489{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400490 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400491 marshal_append(state, (char *)j, sizeof(*j));
492 if (j->user)
493 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400494 if (j->chrootdir)
495 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800496 if (j->flags.seccomp_filter && j->filter_prog) {
497 struct sock_fprog *fp = j->filter_prog;
498 marshal_append(state, (char *)fp->filter,
499 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400500 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400501 for (b = j->bindings_head; b; b = b->next) {
502 marshal_append(state, b->src, strlen(b->src) + 1);
503 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700504 marshal_append(state, (char *)&b->writeable,
505 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400506 }
Will Drewryf89aef52011-09-16 16:48:57 -0500507}
508
Will Drewry6ac91122011-10-21 16:38:58 -0500509size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400510{
511 struct marshal_state state;
512 marshal_state_init(&state, NULL, 0);
513 minijail_marshal_helper(&state, j);
514 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500515}
516
Elly Jonese1749eb2011-10-07 13:54:59 -0400517int minijail_marshal(const struct minijail *j, char *buf, size_t available)
518{
519 struct marshal_state state;
520 marshal_state_init(&state, buf, available);
521 minijail_marshal_helper(&state, j);
522 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500523}
524
Elly Jones51a5b6c2011-10-12 19:09:26 -0400525/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
526 * @length Number of bytes to consume
527 * @buf Buffer to consume from
528 * @buflength Size of @buf
529 *
530 * Returns a pointer to the base of the bytes, or NULL for errors.
531 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700532void *consumebytes(size_t length, char **buf, size_t *buflength)
533{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400534 char *p = *buf;
535 if (length > *buflength)
536 return NULL;
537 *buf += length;
538 *buflength -= length;
539 return p;
540}
541
542/* consumestr: consumes a C string from a buffer @buf of length @length
543 * @buf Buffer to consume
544 * @length Length of buffer
545 *
546 * Returns a pointer to the base of the string, or NULL for errors.
547 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700548char *consumestr(char **buf, size_t *buflength)
549{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400550 size_t len = strnlen(*buf, *buflength);
551 if (len == *buflength)
552 /* There's no null-terminator */
553 return NULL;
554 return consumebytes(len + 1, buf, buflength);
555}
556
Elly Jonese1749eb2011-10-07 13:54:59 -0400557int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
558{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400559 int i;
560 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500561 int ret = -EINVAL;
562
Elly Jonese1749eb2011-10-07 13:54:59 -0400563 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500564 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400565 memcpy((void *)j, serialized, sizeof(*j));
566 serialized += sizeof(*j);
567 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500568
Will Drewrybee7ba72011-10-21 20:47:01 -0500569 /* Potentially stale pointers not used as signals. */
570 j->bindings_head = NULL;
571 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800572 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500573
Elly Jonese1749eb2011-10-07 13:54:59 -0400574 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400575 char *user = consumestr(&serialized, &length);
576 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500577 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400578 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500579 if (!j->user)
580 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400581 }
Will Drewryf89aef52011-09-16 16:48:57 -0500582
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400583 if (j->chrootdir) { /* stale pointer */
584 char *chrootdir = consumestr(&serialized, &length);
585 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500586 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400587 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500588 if (!j->chrootdir)
589 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400590 }
591
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800592 if (j->flags.seccomp_filter && j->filter_len > 0) {
593 size_t ninstrs = j->filter_len;
594 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
595 ninstrs > USHRT_MAX)
596 goto bad_filters;
597
598 size_t program_len = ninstrs * sizeof(struct sock_filter);
599 void *program = consumebytes(program_len, &serialized, &length);
600 if (!program)
601 goto bad_filters;
602
603 j->filter_prog = malloc(sizeof(struct sock_fprog));
604 j->filter_prog->len = ninstrs;
605 j->filter_prog->filter = malloc(program_len);
606 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400607 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400608
609 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400610 j->binding_count = 0;
611 for (i = 0; i < count; ++i) {
612 int *writeable;
613 const char *dest;
614 const char *src = consumestr(&serialized, &length);
615 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500616 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400617 dest = consumestr(&serialized, &length);
618 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500619 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400620 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
621 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500622 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400623 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500624 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400625 }
626
Elly Jonese1749eb2011-10-07 13:54:59 -0400627 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500628
629bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800630 if (j->flags.seccomp_filter && j->filter_len > 0) {
631 free(j->filter_prog->filter);
632 free(j->filter_prog);
633 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500634bad_filters:
635 if (j->chrootdir)
636 free(j->chrootdir);
637bad_chrootdir:
638 if (j->user)
639 free(j->user);
640clear_pointers:
641 j->user = NULL;
642 j->chrootdir = NULL;
643out:
644 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500645}
646
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800647static void write_ugid_mappings(const struct minijail *j, int *pipe_fds)
648{
649 int fd, ret, len;
650 size_t sz;
651 char fname[32];
652 close(pipe_fds[0]);
653
654 sz = sizeof(fname);
655 if (j->uidmap) {
656 ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid);
657 if (ret < 0 || ret >= sz)
658 die("failed to write file name of uid_map");
659 fd = open(fname, O_WRONLY);
660 if (fd < 0)
661 pdie("failed to open '%s'", fname);
662 len = strlen(j->uidmap);
663 if (write(fd, j->uidmap, len) < len)
664 die("failed to set uid_map");
665 close(fd);
666 }
667 if (j->gidmap) {
668 ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid);
669 if (ret < 0 || ret >= sz)
670 die("failed to write file name of gid_map");
671 fd = open(fname, O_WRONLY);
672 if (fd < 0)
673 pdie("failed to open '%s'", fname);
674 len = strlen(j->gidmap);
675 if (write(fd, j->gidmap, len) < len)
676 die("failed to set gid_map");
677 close(fd);
678 }
679
680 close(pipe_fds[1]);
681}
682
683static void enter_user_namespace(const struct minijail *j, int *pipe_fds)
684{
685 char buf;
686
687 close(pipe_fds[1]);
688
689 /* Wait for parent to set up uid/gid mappings. */
690 if (read(pipe_fds[0], &buf, 1) != 0)
691 die("failed to sync with parent");
692 close(pipe_fds[0]);
693
694 if (j->uidmap && setresuid(0, 0, 0))
695 pdie("setresuid");
696 if (j->gidmap && setresgid(0, 0, 0))
697 pdie("setresgid");
698}
699
Elly Jones51a5b6c2011-10-12 19:09:26 -0400700/* bind_one: Applies bindings from @b for @j, recursing as needed.
701 * @j Minijail these bindings are for
702 * @b Head of list of bindings
703 *
704 * Returns 0 for success.
705 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700706int bind_one(const struct minijail *j, struct binding *b)
707{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400708 int ret = 0;
709 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400710 if (ret)
711 return ret;
712 /* dest has a leading "/" */
713 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
714 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500715 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400716 if (ret)
717 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500718 if (!b->writeable) {
719 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700720 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500721 if (ret)
722 pdie("bind ro: %s -> %s", b->src, dest);
723 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400724 free(dest);
725 if (b->next)
726 return bind_one(j, b->next);
727 return ret;
728}
729
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700730int enter_chroot(const struct minijail *j)
731{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400732 int ret;
733 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
734 return ret;
735
736 if (chroot(j->chrootdir))
737 return -errno;
738
739 if (chdir("/"))
740 return -errno;
741
742 return 0;
743}
744
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800745int enter_pivot_root(const struct minijail *j)
746{
747 int ret;
748 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
749 return ret;
750
751 /* To ensure chrootdir is the root of a file system, do a self bind mount. */
752 if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, ""))
753 pdie("failed to bind mount '%s'", j->chrootdir);
754 if (chdir(j->chrootdir))
755 return -errno;
756 if (mkdir(".minijail_pivot", 0755))
757 pdie("mkdir(.minijail_pivot)");
758 if (syscall(SYS_pivot_root, ".", ".minijail_pivot")) {
759 remove(".minijail_pivot");
760 pdie("pivot_root");
761 }
762 /* The old root might be busy, so use lazy unmount. */
763 if (umount2(".minijail_pivot", MNT_DETACH))
764 pdie("umount(.minijail_pivot");
765 if (chdir("/"))
766 return -errno;
767 if (chroot("/"))
768 return -errno;
769 if (remove(".minijail_pivot"))
770 return -errno;
771
772 return 0;
773}
774
Lee Campbell11af0622014-05-22 12:36:04 -0700775int mount_tmp(void)
776{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800777 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700778}
779
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800780int remount_readonly(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400781{
782 const char *kProcPath = "/proc";
783 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500784 /*
785 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400786 * /proc in our namespace, which means using MS_REMOUNT here would
787 * mutate our parent's mount as well, even though we're in a VFS
788 * namespace (!). Instead, remove their mount from our namespace
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800789 * and make our own. However, if we are in a new user namespace, /proc
790 * is not seen as mounted, so don't return error if umount() fails.
Elly Jonese1749eb2011-10-07 13:54:59 -0400791 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800792 if (umount(kProcPath) && !j->flags.userns)
Elly Jonese1749eb2011-10-07 13:54:59 -0400793 return -errno;
794 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
795 return -errno;
796 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400797}
798
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800799static void write_pid_file(const struct minijail *j)
800{
801 FILE *fp = fopen(j->pid_file_path, "w");
802
803 if (!fp)
804 pdie("failed to open '%s'", j->pid_file_path);
805 if (fprintf(fp, "%d\n", (int)j->initpid) < 0)
806 pdie("fprintf(%s)", j->pid_file_path);
807 if (fclose(fp))
808 pdie("fclose(%s)", j->pid_file_path);
809}
810
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700811void drop_ugid(const struct minijail *j)
812{
813 if (j->flags.usergroups) {
814 if (initgroups(j->user, j->usergid))
815 pdie("initgroups");
816 } else {
817 /* Only attempt to clear supplemental groups if we are changing
818 * users. */
819 if ((j->uid || j->gid) && setgroups(0, NULL))
820 pdie("setgroups");
821 }
822
823 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
824 pdie("setresgid");
825
826 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
827 pdie("setresuid");
828}
829
Mike Frysinger3adfef72013-05-09 17:19:08 -0400830/*
831 * We specifically do not use cap_valid() as that only tells us the last
832 * valid cap we were *compiled* against (i.e. what the version of kernel
833 * headers says). If we run on a different kernel version, then it's not
834 * uncommon for that to be less (if an older kernel) or more (if a newer
835 * kernel). So suck up the answer via /proc.
836 */
837static int run_cap_valid(unsigned int cap)
838{
839 static unsigned int last_cap;
840
841 if (!last_cap) {
842 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
843 FILE *fp = fopen(cap_file, "re");
844 if (fscanf(fp, "%u", &last_cap) != 1)
845 pdie("fscanf(%s)", cap_file);
846 fclose(fp);
847 }
848
849 return cap <= last_cap;
850}
851
Will Drewry6ac91122011-10-21 16:38:58 -0500852void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400853{
854 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800855 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800856 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400857 unsigned int i;
858 if (!caps)
859 die("can't get process caps");
860 if (cap_clear_flag(caps, CAP_INHERITABLE))
861 die("can't clear inheritable caps");
862 if (cap_clear_flag(caps, CAP_EFFECTIVE))
863 die("can't clear effective caps");
864 if (cap_clear_flag(caps, CAP_PERMITTED))
865 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400866 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800867 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800868 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400869 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800870 flag[0] = i;
871 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400872 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800873 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400874 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800875 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400876 die("can't add inheritable cap");
877 }
878 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800879 die("can't apply initial cleaned capset");
880
881 /*
882 * Instead of dropping bounding set first, do it here in case
883 * the caller had a more permissive bounding set which could
884 * have been used above to raise a capability that wasn't already
885 * present. This requires CAP_SETPCAP, so we raised/kept it above.
886 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400887 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800888 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400889 continue;
890 if (prctl(PR_CAPBSET_DROP, i))
891 pdie("prctl(PR_CAPBSET_DROP)");
892 }
Kees Cook323878a2013-02-05 15:35:24 -0800893
894 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800895 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800896 flag[0] = CAP_SETPCAP;
897 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
898 die("can't clear effective cap");
899 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
900 die("can't clear permitted cap");
901 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
902 die("can't clear inheritable cap");
903 }
904
905 if (cap_set_proc(caps))
906 die("can't apply final cleaned capset");
907
908 cap_free(caps);
Elly Jonescd7a9042011-07-22 13:56:51 -0400909}
910
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700911void set_seccomp_filter(const struct minijail *j)
912{
913 /*
914 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
915 * in the kernel source tree for an explanation of the parameters.
916 */
917 if (j->flags.no_new_privs) {
918 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
919 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
920 }
921
922 /*
923 * If we're logging seccomp filter failures,
924 * install the SIGSYS handler first.
925 */
926 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
927 if (install_sigsys_handler())
928 pdie("install SIGSYS handler");
929 warn("logging seccomp filter failures");
930 }
931
932 /*
933 * Install the syscall filter.
934 */
935 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700936 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
937 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
938 warn("seccomp not supported");
939 return;
940 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700941 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700942 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700943 }
944}
945
Will Drewry6ac91122011-10-21 16:38:58 -0500946void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400947{
948 if (j->flags.pids)
949 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700950 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400951
Elly Jonese1749eb2011-10-07 13:54:59 -0400952 if (j->flags.usergroups && !j->user)
953 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400954
Elly Jonesdd3e8512012-01-23 15:13:38 -0500955 /*
956 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400957 * so we don't even try. If any of our operations fail, we abort() the
958 * entire process.
959 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700960 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
961 pdie("setns(CLONE_NEWNS)");
962
Elly Jonese1749eb2011-10-07 13:54:59 -0400963 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400964 pdie("unshare(vfs)");
965
966 if (j->flags.net && unshare(CLONE_NEWNET))
967 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400968
Elly Jones51a5b6c2011-10-12 19:09:26 -0400969 if (j->flags.chroot && enter_chroot(j))
970 pdie("chroot");
971
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800972 if (j->flags.pivot_root && enter_pivot_root(j))
973 pdie("pivot_root");
974
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800975 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700976 pdie("mount_tmp");
977
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800978 if (j->flags.readonly && remount_readonly(j))
Elly Jonese1749eb2011-10-07 13:54:59 -0400979 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400980
Elly Jonese1749eb2011-10-07 13:54:59 -0400981 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500982 /*
983 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400984 * capability to change uids, our attempt to use setuid()
985 * below will fail. Hang on to root caps across setuid(), then
986 * lock securebits.
987 */
988 if (prctl(PR_SET_KEEPCAPS, 1))
989 pdie("prctl(PR_SET_KEEPCAPS)");
990 if (prctl
991 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
992 pdie("prctl(PR_SET_SECUREBITS)");
993 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400994
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700995 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700996 * If we're setting no_new_privs, we can drop privileges
997 * before setting seccomp filter. This way filter policies
998 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700999 */
1000 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001001 drop_ugid(j);
1002 if (j->flags.caps)
1003 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001004
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001005 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001006 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -07001007 /*
1008 * If we're not setting no_new_privs,
1009 * we need to set seccomp filter *before* dropping privileges.
1010 * WARNING: this means that filter policies *must* allow
1011 * setgroups()/setresgid()/setresuid() for dropping root and
1012 * capget()/capset()/prctl() for dropping caps.
1013 */
1014 set_seccomp_filter(j);
1015
1016 drop_ugid(j);
1017 if (j->flags.caps)
1018 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -04001019 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001020
Elly Jonesdd3e8512012-01-23 15:13:38 -05001021 /*
1022 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -04001023 * privilege-dropping syscalls :)
1024 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001025 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
1026 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
1027 warn("seccomp not supported");
1028 return;
1029 }
Elly Jonese1749eb2011-10-07 13:54:59 -04001030 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -07001031 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001032}
1033
Will Drewry6ac91122011-10-21 16:38:58 -05001034/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -04001035static int init_exitstatus = 0;
1036
Will Drewry6ac91122011-10-21 16:38:58 -05001037void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -04001038{
1039 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -04001040}
1041
Will Drewry6ac91122011-10-21 16:38:58 -05001042int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -04001043{
1044 pid_t pid;
1045 int status;
1046 /* so that we exit with the right status */
1047 signal(SIGTERM, init_term);
1048 /* TODO(wad) self jail with seccomp_filters here. */
1049 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001050 /*
1051 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -04001052 * left inside our pid namespace or we get a signal.
1053 */
1054 if (pid == rootpid)
1055 init_exitstatus = status;
1056 }
1057 if (!WIFEXITED(init_exitstatus))
1058 _exit(MINIJAIL_ERR_INIT);
1059 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -04001060}
1061
Will Drewry6ac91122011-10-21 16:38:58 -05001062int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001063{
1064 size_t sz = 0;
1065 size_t bytes = read(fd, &sz, sizeof(sz));
1066 char *buf;
1067 int r;
1068 if (sizeof(sz) != bytes)
1069 return -EINVAL;
1070 if (sz > USHRT_MAX) /* Arbitrary sanity check */
1071 return -E2BIG;
1072 buf = malloc(sz);
1073 if (!buf)
1074 return -ENOMEM;
1075 bytes = read(fd, buf, sz);
1076 if (bytes != sz) {
1077 free(buf);
1078 return -EINVAL;
1079 }
1080 r = minijail_unmarshal(j, buf, sz);
1081 free(buf);
1082 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001083}
1084
Will Drewry6ac91122011-10-21 16:38:58 -05001085int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -04001086{
1087 char *buf;
1088 size_t sz = minijail_size(j);
1089 ssize_t written;
1090 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -04001091
Elly Jonese1749eb2011-10-07 13:54:59 -04001092 if (!sz)
1093 return -EINVAL;
1094 buf = malloc(sz);
1095 r = minijail_marshal(j, buf, sz);
1096 if (r) {
1097 free(buf);
1098 return r;
1099 }
1100 /* Sends [size][minijail]. */
1101 written = write(fd, &sz, sizeof(sz));
1102 if (written != sizeof(sz)) {
1103 free(buf);
1104 return -EFAULT;
1105 }
1106 written = write(fd, buf, sz);
1107 if (written < 0 || (size_t) written != sz) {
1108 free(buf);
1109 return -EFAULT;
1110 }
1111 free(buf);
1112 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001113}
Elly Jonescd7a9042011-07-22 13:56:51 -04001114
Will Drewry6ac91122011-10-21 16:38:58 -05001115int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -04001116{
Daniel Erat5b7a3182015-08-19 16:06:22 -06001117#if defined(__ANDROID__)
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001118 /* Don't use LDPRELOAD on Brillo. */
1119 return 0;
1120#else
Elly Jonese1749eb2011-10-07 13:54:59 -04001121 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
1122 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
1123 if (!newenv)
1124 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -04001125
Elly Jonese1749eb2011-10-07 13:54:59 -04001126 /* Only insert a separating space if we have something to separate... */
1127 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
1128 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -04001129
Elly Jonese1749eb2011-10-07 13:54:59 -04001130 /* setenv() makes a copy of the string we give it */
1131 setenv(kLdPreloadEnvVar, newenv, 1);
1132 free(newenv);
1133 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -07001134#endif
Elly Jonescd7a9042011-07-22 13:56:51 -04001135}
1136
Will Drewry6ac91122011-10-21 16:38:58 -05001137int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -04001138{
1139 int r = pipe(fds);
1140 char fd_buf[11];
1141 if (r)
1142 return r;
1143 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
1144 if (r <= 0)
1145 return -EINVAL;
1146 setenv(kFdEnvVar, fd_buf, 1);
1147 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -05001148}
1149
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001150int setup_pipe_end(int fds[2], size_t index)
1151{
1152 if (index > 1)
1153 return -1;
1154
1155 close(fds[1 - index]);
1156 return fds[index];
1157}
1158
1159int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1160{
1161 if (index > 1)
1162 return -1;
1163
1164 close(fds[1 - index]);
1165 /* dup2(2) the corresponding end of the pipe into |fd|. */
1166 return dup2(fds[index], fd);
1167}
1168
Will Drewry6ac91122011-10-21 16:38:58 -05001169int API minijail_run(struct minijail *j, const char *filename,
1170 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001171{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001172 return minijail_run_pid_pipes(j, filename, argv,
1173 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001174}
1175
1176int API minijail_run_pid(struct minijail *j, const char *filename,
1177 char *const argv[], pid_t *pchild_pid)
1178{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001179 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
1180 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001181}
1182
1183int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001184 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001185{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001186 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
1187 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001188}
1189
1190int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001191 char *const argv[], pid_t *pchild_pid,
1192 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001193{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001194 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
1195 NULL, NULL);
1196}
1197
1198int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001199 char *const argv[], pid_t *pchild_pid,
1200 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001201{
Elly Jonese1749eb2011-10-07 13:54:59 -04001202 char *oldenv, *oldenv_copy = NULL;
1203 pid_t child_pid;
1204 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001205 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001206 int stdout_fds[2];
1207 int stderr_fds[2];
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001208 int userns_pipe_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001209 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001210 /* We need to remember this across the minijail_preexec() call. */
1211 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001212 int do_init = j->flags.do_init;
Ben Chan541c7e52011-08-26 14:55:53 -07001213
Elly Jonese1749eb2011-10-07 13:54:59 -04001214 oldenv = getenv(kLdPreloadEnvVar);
1215 if (oldenv) {
1216 oldenv_copy = strdup(oldenv);
1217 if (!oldenv_copy)
1218 return -ENOMEM;
1219 }
Will Drewryf89aef52011-09-16 16:48:57 -05001220
Elly Jonese1749eb2011-10-07 13:54:59 -04001221 if (setup_preload())
1222 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001223
Elly Jonesdd3e8512012-01-23 15:13:38 -05001224 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001225 * Make the process group ID of this process equal to its PID, so that
1226 * both the Minijail process and the jailed process can be killed
1227 * together.
1228 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1229 * the process is already a process group leader.
1230 */
1231 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1232 if (errno != EPERM) {
1233 pdie("setpgid(0, 0)");
1234 }
1235 }
1236
1237 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001238 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001239 * a pipe(2) to send the minijail configuration over.
1240 */
1241 if (setup_pipe(pipe_fds))
1242 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001243
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001244 /*
1245 * If we want to write to the child process' standard input,
1246 * create the pipe(2) now.
1247 */
1248 if (pstdin_fd) {
1249 if (pipe(stdin_fds))
1250 return -EFAULT;
1251 }
1252
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001253 /*
1254 * If we want to read from the child process' standard output,
1255 * create the pipe(2) now.
1256 */
1257 if (pstdout_fd) {
1258 if (pipe(stdout_fds))
1259 return -EFAULT;
1260 }
1261
1262 /*
1263 * If we want to read from the child process' standard error,
1264 * create the pipe(2) now.
1265 */
1266 if (pstderr_fd) {
1267 if (pipe(stderr_fds))
1268 return -EFAULT;
1269 }
1270
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001271 /*
1272 * If we want to set up a new uid/gid mapping in the user namespace,
1273 * create the pipe(2) to sync between parent and child.
1274 */
1275 if (j->flags.userns) {
1276 if (pipe(userns_pipe_fds))
1277 return -EFAULT;
1278 }
1279
Elly Jones761b7412012-06-13 15:49:52 -04001280 /* Use sys_clone() if and only if we're creating a pid namespace.
1281 *
1282 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1283 *
1284 * In multithreaded programs, there are a bunch of locks inside libc,
1285 * some of which may be held by other threads at the time that we call
1286 * minijail_run_pid(). If we call fork(), glibc does its level best to
1287 * ensure that we hold all of these locks before it calls clone()
1288 * internally and drop them after clone() returns, but when we call
1289 * sys_clone(2) directly, all that gets bypassed and we end up with a
1290 * child address space where some of libc's important locks are held by
1291 * other threads (which did not get cloned, and hence will never release
1292 * those locks). This is okay so long as we call exec() immediately
1293 * after, but a bunch of seemingly-innocent libc functions like setenv()
1294 * take locks.
1295 *
1296 * Hence, only call sys_clone() if we need to, in order to get at pid
1297 * namespacing. If we follow this path, the child's address space might
1298 * have broken locks; you may only call functions that do not acquire
1299 * any locks.
1300 *
1301 * Unfortunately, fork() acquires every lock it can get its hands on, as
1302 * previously detailed, so this function is highly likely to deadlock
1303 * later on (see "deadlock here") if we're multithreaded.
1304 *
1305 * We might hack around this by having the clone()d child (init of the
1306 * pid namespace) return directly, rather than leaving the clone()d
1307 * process hanging around to be init for the new namespace (and having
1308 * its fork()ed child return in turn), but that process would be crippled
1309 * with its libc locks potentially broken. We might try fork()ing in the
1310 * parent before we clone() to ensure that we own all the locks, but
1311 * then we have to have the forked child hanging around consuming
1312 * resources (and possibly having file descriptors / shared memory
1313 * regions / etc attached). We'd need to keep the child around to avoid
1314 * having its children get reparented to init.
1315 *
1316 * TODO(ellyjones): figure out if the "forked child hanging around"
1317 * problem is fixable or not. It would be nice if we worked in this
1318 * case.
1319 */
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001320 if (pid_namespace) {
1321 int clone_flags = CLONE_NEWPID | SIGCHLD;
1322 if (j->flags.userns)
1323 clone_flags |= CLONE_NEWUSER;
1324 child_pid = syscall(SYS_clone, clone_flags, NULL);
1325 }
Elly Jones761b7412012-06-13 15:49:52 -04001326 else
1327 child_pid = fork();
1328
Elly Jonese1749eb2011-10-07 13:54:59 -04001329 if (child_pid < 0) {
1330 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001331 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001332 }
Will Drewryf89aef52011-09-16 16:48:57 -05001333
Elly Jonese1749eb2011-10-07 13:54:59 -04001334 if (child_pid) {
1335 /* Restore parent's LD_PRELOAD. */
1336 if (oldenv_copy) {
1337 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1338 free(oldenv_copy);
1339 } else {
1340 unsetenv(kLdPreloadEnvVar);
1341 }
1342 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001343
Elly Jonese1749eb2011-10-07 13:54:59 -04001344 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001345
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001346 if (j->flags.pid_file)
1347 write_pid_file(j);
1348
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001349 if (j->flags.userns)
1350 write_ugid_mappings(j, userns_pipe_fds);
1351
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001352 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001353 close(pipe_fds[0]); /* read endpoint */
1354 ret = minijail_to_fd(j, pipe_fds[1]);
1355 close(pipe_fds[1]); /* write endpoint */
1356 if (ret) {
1357 kill(j->initpid, SIGKILL);
1358 die("failed to send marshalled minijail");
1359 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001360
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001361 if (pchild_pid)
1362 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001363
1364 /*
1365 * If we want to write to the child process' standard input,
1366 * set up the write end of the pipe.
1367 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001368 if (pstdin_fd)
1369 *pstdin_fd = setup_pipe_end(stdin_fds,
1370 1 /* write end */);
1371
1372 /*
1373 * If we want to read from the child process' standard output,
1374 * set up the read end of the pipe.
1375 */
1376 if (pstdout_fd)
1377 *pstdout_fd = setup_pipe_end(stdout_fds,
1378 0 /* read end */);
1379
1380 /*
1381 * If we want to read from the child process' standard error,
1382 * set up the read end of the pipe.
1383 */
1384 if (pstderr_fd)
1385 *pstderr_fd = setup_pipe_end(stderr_fds,
1386 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001387
Elly Jonese1749eb2011-10-07 13:54:59 -04001388 return 0;
1389 }
1390 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001391
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001392
1393 if (j->flags.userns)
1394 enter_user_namespace(j, userns_pipe_fds);
1395
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001396 /*
1397 * If we want to write to the jailed process' standard input,
1398 * set up the read end of the pipe.
1399 */
1400 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001401 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1402 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001403 die("failed to set up stdin pipe");
1404 }
1405
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001406 /*
1407 * If we want to read from the jailed process' standard output,
1408 * set up the write end of the pipe.
1409 */
1410 if (pstdout_fd) {
1411 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1412 STDOUT_FILENO) < 0)
1413 die("failed to set up stdout pipe");
1414 }
1415
1416 /*
1417 * If we want to read from the jailed process' standard error,
1418 * set up the write end of the pipe.
1419 */
1420 if (pstderr_fd) {
1421 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1422 STDERR_FILENO) < 0)
1423 die("failed to set up stderr pipe");
1424 }
1425
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001426 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001427 minijail_preexec(j);
1428 /* Jail this process and its descendants... */
1429 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001430
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001431 if (pid_namespace && do_init) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001432 /*
1433 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001434 * namespace. We don't want all programs we might exec to have
1435 * to know how to be init. Normally |do_init == 1| we fork off
1436 * a child to actually run the program. If |do_init == 0|, we
1437 * let the program keep pid 1 and be init.
Elly Jones761b7412012-06-13 15:49:52 -04001438 *
1439 * If we're multithreaded, we'll probably deadlock here. See
1440 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001441 */
1442 child_pid = fork();
1443 if (child_pid < 0)
1444 _exit(child_pid);
1445 else if (child_pid > 0)
1446 init(child_pid); /* never returns */
1447 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001448
Elly Jonesdd3e8512012-01-23 15:13:38 -05001449 /*
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001450 * If we aren't pid-namespaced, or jailed program asked to be init:
Elly Jonese1749eb2011-10-07 13:54:59 -04001451 * calling process
1452 * -> execve()-ing process
1453 * If we are:
1454 * calling process
1455 * -> init()-ing process
1456 * -> execve()-ing process
1457 */
1458 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001459}
1460
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001461int API minijail_run_static(struct minijail *j, const char *filename,
1462 char *const argv[])
1463{
1464 pid_t child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001465 int userns_pipe_fds[2];
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001466 int pid_namespace = j->flags.pids;
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001467 int do_init = j->flags.do_init;
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001468
1469 if (j->flags.caps)
1470 die("caps not supported with static targets");
1471
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001472 /*
1473 * If we want to set up a new uid/gid mapping in the user namespace,
1474 * create the pipe(2) to sync between parent and child.
1475 */
1476 if (j->flags.userns) {
1477 if (pipe(userns_pipe_fds))
1478 return -EFAULT;
1479 }
1480
1481 if (pid_namespace) {
1482 int clone_flags = CLONE_NEWPID | SIGCHLD;
1483 if (j->flags.userns)
1484 clone_flags |= CLONE_NEWUSER;
1485 child_pid = syscall(SYS_clone, clone_flags, NULL);
1486 }
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001487 else
1488 child_pid = fork();
1489
1490 if (child_pid < 0) {
1491 die("failed to fork child");
1492 }
1493 if (child_pid > 0 ) {
1494 j->initpid = child_pid;
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001495
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +08001496 if (j->flags.pid_file)
1497 write_pid_file(j);
1498
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001499 if (j->flags.userns)
1500 write_ugid_mappings(j, userns_pipe_fds);
1501
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001502 return 0;
1503 }
1504
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +08001505 if (j->flags.userns)
1506 enter_user_namespace(j, userns_pipe_fds);
1507
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001508 /*
1509 * We can now drop this child into the sandbox
1510 * then execve the target.
1511 */
1512
1513 j->flags.pids = 0;
1514 minijail_enter(j);
1515
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001516 if (pid_namespace && do_init) {
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001517 /*
1518 * pid namespace: this process will become init inside the new
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +08001519 * namespace. We don't want all programs we might exec to have
1520 * to know how to be init. Normally |do_init == 1| we fork off
1521 * a child to actually run the program. If |do_init == 0|, we
1522 * let the program keep pid 1 and be init.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001523 *
1524 * If we're multithreaded, we'll probably deadlock here. See
1525 * WARNING above.
1526 */
1527 child_pid = fork();
1528 if (child_pid < 0)
1529 _exit(child_pid);
1530 else if (child_pid > 0)
1531 init(child_pid); /* never returns */
1532 }
1533
1534 _exit(execve(filename, argv, environ));
1535}
1536
Will Drewry6ac91122011-10-21 16:38:58 -05001537int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001538{
1539 int st;
1540 if (kill(j->initpid, SIGTERM))
1541 return -errno;
1542 if (waitpid(j->initpid, &st, 0) < 0)
1543 return -errno;
1544 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001545}
1546
Will Drewry6ac91122011-10-21 16:38:58 -05001547int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001548{
1549 int st;
1550 if (waitpid(j->initpid, &st, 0) < 0)
1551 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001552
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001553 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001554 int error_status = st;
1555 if (WIFSIGNALED(st)) {
1556 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001557 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001558 j->initpid, signum);
1559 /*
1560 * We return MINIJAIL_ERR_JAIL if the process received
1561 * SIGSYS, which happens when a syscall is blocked by
1562 * seccomp filters.
1563 * If not, we do what bash(1) does:
1564 * $? = 128 + signum
1565 */
1566 if (signum == SIGSYS) {
1567 error_status = MINIJAIL_ERR_JAIL;
1568 } else {
1569 error_status = 128 + signum;
1570 }
1571 }
1572 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001573 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001574
1575 int exit_status = WEXITSTATUS(st);
1576 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001577 info("child process %d exited with status %d",
1578 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001579
1580 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001581}
1582
Will Drewry6ac91122011-10-21 16:38:58 -05001583void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001584{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001585 if (j->flags.seccomp_filter && j->filter_prog) {
1586 free(j->filter_prog->filter);
1587 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001588 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001589 while (j->bindings_head) {
1590 struct binding *b = j->bindings_head;
1591 j->bindings_head = j->bindings_head->next;
1592 free(b->dest);
1593 free(b->src);
1594 free(b);
1595 }
1596 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001597 if (j->user)
1598 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001599 if (j->chrootdir)
1600 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001601 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001602}