Jorge Lucangeli Obes | d613ab2 | 2015-03-03 14:22:50 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 3 | * found in the LICENSE file. |
| 4 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 5 | |
| 6 | #define _BSD_SOURCE |
| 7 | #define _GNU_SOURCE |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 8 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 9 | #include <asm/unistd.h> |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 10 | #include <ctype.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 11 | #include <errno.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 12 | #include <fcntl.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 13 | #include <grp.h> |
| 14 | #include <inttypes.h> |
Will Drewry | fe4a372 | 2011-09-16 14:50:50 -0500 | [diff] [blame] | 15 | #include <limits.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 16 | #include <linux/capability.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 17 | #include <pwd.h> |
| 18 | #include <sched.h> |
| 19 | #include <signal.h> |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 20 | #include <stdarg.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 21 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 22 | #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 Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 28 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 29 | #include <sys/prctl.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 32 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 33 | #include <sys/wait.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "libminijail.h" |
| 37 | #include "libminijail-private.h" |
| 38 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 39 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 40 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 41 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 42 | |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 43 | #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 Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 50 | /* Until these are reliably available in linux/prctl.h */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 51 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 52 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 53 | #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 Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 61 | #endif |
| 62 | |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 63 | #ifdef USE_SECCOMP_SOFTFAIL |
| 64 | # define SECCOMP_SOFTFAIL 1 |
| 65 | #else |
| 66 | # define SECCOMP_SOFTFAIL 0 |
| 67 | #endif |
| 68 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 69 | struct binding { |
| 70 | char *src; |
| 71 | char *dest; |
| 72 | int writeable; |
| 73 | struct binding *next; |
| 74 | }; |
| 75 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 76 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 77 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 78 | * 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 Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 80 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 81 | struct { |
| 82 | int uid:1; |
| 83 | int gid:1; |
| 84 | int caps:1; |
| 85 | int vfs:1; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 86 | int enter_vfs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 87 | int pids:1; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 88 | int net:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 89 | int seccomp:1; |
| 90 | int readonly:1; |
| 91 | int usergroups:1; |
| 92 | int ptrace:1; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 93 | int no_new_privs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 94 | int seccomp_filter:1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 95 | int log_seccomp_filter:1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 96 | int chroot:1; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 97 | int mount_tmp:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 98 | } flags; |
| 99 | uid_t uid; |
| 100 | gid_t gid; |
| 101 | gid_t usergid; |
| 102 | char *user; |
| 103 | uint64_t caps; |
| 104 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 105 | int mountns_fd; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 106 | int filter_len; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 107 | int binding_count; |
| 108 | char *chrootdir; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 109 | struct sock_fprog *filter_prog; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 110 | struct binding *bindings_head; |
| 111 | struct binding *bindings_tail; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 112 | }; |
| 113 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Strip out flags meant for the parent. |
| 116 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 117 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 118 | */ |
| 119 | void minijail_preenter(struct minijail *j) |
| 120 | { |
| 121 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 122 | j->flags.enter_vfs = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 123 | j->flags.readonly = 0; |
| 124 | j->flags.pids = 0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Strip out flags meant for the child. |
| 129 | * We keep things that are inherited across execve(2). |
| 130 | */ |
| 131 | void minijail_preexec(struct minijail *j) |
| 132 | { |
| 133 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 134 | int enter_vfs = j->flags.enter_vfs; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 135 | int readonly = j->flags.readonly; |
| 136 | if (j->user) |
| 137 | free(j->user); |
| 138 | j->user = NULL; |
| 139 | memset(&j->flags, 0, sizeof(j->flags)); |
| 140 | /* Now restore anything we meant to keep. */ |
| 141 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 142 | j->flags.enter_vfs = enter_vfs; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 143 | j->flags.readonly = readonly; |
| 144 | /* Note, |pids| will already have been used before this call. */ |
| 145 | } |
| 146 | |
| 147 | /* Minijail API. */ |
| 148 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 149 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 150 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 151 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 154 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 155 | { |
| 156 | if (uid == 0) |
| 157 | die("useless change to uid 0"); |
| 158 | j->uid = uid; |
| 159 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 162 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 163 | { |
| 164 | if (gid == 0) |
| 165 | die("useless change to gid 0"); |
| 166 | j->gid = gid; |
| 167 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 170 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 171 | { |
| 172 | char *buf = NULL; |
| 173 | struct passwd pw; |
| 174 | struct passwd *ppw = NULL; |
| 175 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 176 | if (sz == -1) |
| 177 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 178 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 179 | /* |
| 180 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 181 | * the maximum needed size of the buffer, so we don't have to search. |
| 182 | */ |
| 183 | buf = malloc(sz); |
| 184 | if (!buf) |
| 185 | return -ENOMEM; |
| 186 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 187 | /* |
| 188 | * We're safe to free the buffer here. The strings inside pw point |
| 189 | * inside buf, but we don't use any of them; this leaves the pointers |
| 190 | * dangling but it's safe. ppw points at pw if getpwnam_r succeeded. |
| 191 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 192 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 193 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 194 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 195 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 196 | minijail_change_uid(j, ppw->pw_uid); |
| 197 | j->user = strdup(user); |
| 198 | if (!j->user) |
| 199 | return -ENOMEM; |
| 200 | j->usergid = ppw->pw_gid; |
| 201 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 204 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 205 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 206 | char *buf = NULL; |
Yabin Cui | 1b21c8f | 2015-07-22 10:34:45 -0700 | [diff] [blame^] | 207 | struct group gr; |
| 208 | struct group *pgr = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 209 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 210 | if (sz == -1) |
| 211 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 212 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 213 | /* |
| 214 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 215 | * the maximum needed size of the buffer, so we don't have to search. |
| 216 | */ |
| 217 | buf = malloc(sz); |
| 218 | if (!buf) |
| 219 | return -ENOMEM; |
| 220 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 221 | /* |
| 222 | * We're safe to free the buffer here. The strings inside gr point |
| 223 | * inside buf, but we don't use any of them; this leaves the pointers |
| 224 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 225 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 226 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 227 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 228 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 229 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 230 | minijail_change_gid(j, pgr->gr_gid); |
| 231 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 234 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 235 | { |
| 236 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 239 | void API minijail_no_new_privs(struct minijail *j) |
| 240 | { |
| 241 | j->flags.no_new_privs = 1; |
| 242 | } |
| 243 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 244 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 245 | { |
| 246 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 249 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 250 | { |
| 251 | j->flags.log_seccomp_filter = 1; |
| 252 | } |
| 253 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 254 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 255 | { |
| 256 | j->caps = capmask; |
| 257 | j->flags.caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 260 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 261 | { |
| 262 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 265 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 266 | { |
| 267 | int ns_fd = open(ns_path, O_RDONLY); |
| 268 | if (ns_fd < 0) { |
| 269 | pdie("failed to open namespace '%s'", ns_path); |
| 270 | } |
| 271 | j->mountns_fd = ns_fd; |
| 272 | j->flags.enter_vfs = 1; |
| 273 | } |
| 274 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 275 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 276 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 277 | j->flags.vfs = 1; |
| 278 | j->flags.readonly = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 279 | j->flags.pids = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 282 | void API minijail_namespace_net(struct minijail *j) |
| 283 | { |
| 284 | j->flags.net = 1; |
| 285 | } |
| 286 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 287 | void API minijail_remount_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 288 | { |
| 289 | j->flags.vfs = 1; |
| 290 | j->flags.readonly = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 293 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 294 | { |
| 295 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 298 | void API minijail_disable_ptrace(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 299 | { |
| 300 | j->flags.ptrace = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 303 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 304 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 305 | if (j->chrootdir) |
| 306 | return -EINVAL; |
| 307 | j->chrootdir = strdup(dir); |
| 308 | if (!j->chrootdir) |
| 309 | return -ENOMEM; |
| 310 | j->flags.chroot = 1; |
| 311 | return 0; |
| 312 | } |
| 313 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 314 | void API minijail_mount_tmp(struct minijail *j) |
| 315 | { |
| 316 | j->flags.mount_tmp = 1; |
| 317 | } |
| 318 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 319 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 320 | int writeable) |
| 321 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 322 | struct binding *b; |
| 323 | |
| 324 | if (*dest != '/') |
| 325 | return -EINVAL; |
| 326 | b = calloc(1, sizeof(*b)); |
| 327 | if (!b) |
| 328 | return -ENOMEM; |
| 329 | b->dest = strdup(dest); |
| 330 | if (!b->dest) |
| 331 | goto error; |
| 332 | b->src = strdup(src); |
| 333 | if (!b->src) |
| 334 | goto error; |
| 335 | b->writeable = writeable; |
| 336 | |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 337 | info("bind %s -> %s", src, dest); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 338 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 339 | /* |
| 340 | * Force vfs namespacing so the bind mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 341 | * containing vfs namespace. |
| 342 | */ |
| 343 | minijail_namespace_vfs(j); |
| 344 | |
| 345 | if (j->bindings_tail) |
| 346 | j->bindings_tail->next = b; |
| 347 | else |
| 348 | j->bindings_head = b; |
| 349 | j->bindings_tail = b; |
| 350 | j->binding_count++; |
| 351 | |
| 352 | return 0; |
| 353 | |
| 354 | error: |
| 355 | free(b->src); |
| 356 | free(b->dest); |
| 357 | free(b); |
| 358 | return -ENOMEM; |
| 359 | } |
| 360 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 361 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 362 | { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 363 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) { |
| 364 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 365 | warn("not loading seccomp filter, seccomp not supported"); |
| 366 | return; |
| 367 | } |
| 368 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 369 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 370 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 371 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 372 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 373 | |
| 374 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 375 | if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) { |
| 376 | die("failed to compile seccomp filter BPF program in '%s'", |
| 377 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | j->filter_len = fprog->len; |
| 381 | j->filter_prog = fprog; |
| 382 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 383 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 384 | } |
| 385 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 386 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 387 | size_t available; |
| 388 | size_t total; |
| 389 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 390 | }; |
| 391 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 392 | void marshal_state_init(struct marshal_state *state, |
| 393 | char *buf, size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 394 | { |
| 395 | state->available = available; |
| 396 | state->buf = buf; |
| 397 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 400 | void marshal_append(struct marshal_state *state, |
| 401 | char *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 402 | { |
| 403 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 404 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 405 | /* Up to |available| will be written. */ |
| 406 | if (copy_len) { |
| 407 | memcpy(state->buf, src, copy_len); |
| 408 | state->buf += copy_len; |
| 409 | state->available -= copy_len; |
| 410 | } |
| 411 | /* |total| will contain the expected length. */ |
| 412 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 415 | void minijail_marshal_helper(struct marshal_state *state, |
| 416 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 417 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 418 | struct binding *b = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 419 | marshal_append(state, (char *)j, sizeof(*j)); |
| 420 | if (j->user) |
| 421 | marshal_append(state, j->user, strlen(j->user) + 1); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 422 | if (j->chrootdir) |
| 423 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 424 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 425 | struct sock_fprog *fp = j->filter_prog; |
| 426 | marshal_append(state, (char *)fp->filter, |
| 427 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 428 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 429 | for (b = j->bindings_head; b; b = b->next) { |
| 430 | marshal_append(state, b->src, strlen(b->src) + 1); |
| 431 | marshal_append(state, b->dest, strlen(b->dest) + 1); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 432 | marshal_append(state, (char *)&b->writeable, |
| 433 | sizeof(b->writeable)); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 434 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 437 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 438 | { |
| 439 | struct marshal_state state; |
| 440 | marshal_state_init(&state, NULL, 0); |
| 441 | minijail_marshal_helper(&state, j); |
| 442 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 445 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 446 | { |
| 447 | struct marshal_state state; |
| 448 | marshal_state_init(&state, buf, available); |
| 449 | minijail_marshal_helper(&state, j); |
| 450 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 451 | } |
| 452 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 453 | /* consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
| 454 | * @length Number of bytes to consume |
| 455 | * @buf Buffer to consume from |
| 456 | * @buflength Size of @buf |
| 457 | * |
| 458 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 459 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 460 | void *consumebytes(size_t length, char **buf, size_t *buflength) |
| 461 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 462 | char *p = *buf; |
| 463 | if (length > *buflength) |
| 464 | return NULL; |
| 465 | *buf += length; |
| 466 | *buflength -= length; |
| 467 | return p; |
| 468 | } |
| 469 | |
| 470 | /* consumestr: consumes a C string from a buffer @buf of length @length |
| 471 | * @buf Buffer to consume |
| 472 | * @length Length of buffer |
| 473 | * |
| 474 | * Returns a pointer to the base of the string, or NULL for errors. |
| 475 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 476 | char *consumestr(char **buf, size_t *buflength) |
| 477 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 478 | size_t len = strnlen(*buf, *buflength); |
| 479 | if (len == *buflength) |
| 480 | /* There's no null-terminator */ |
| 481 | return NULL; |
| 482 | return consumebytes(len + 1, buf, buflength); |
| 483 | } |
| 484 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 485 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 486 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 487 | int i; |
| 488 | int count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 489 | int ret = -EINVAL; |
| 490 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 491 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 492 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 493 | memcpy((void *)j, serialized, sizeof(*j)); |
| 494 | serialized += sizeof(*j); |
| 495 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 496 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 497 | /* Potentially stale pointers not used as signals. */ |
| 498 | j->bindings_head = NULL; |
| 499 | j->bindings_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 500 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 501 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 502 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 503 | char *user = consumestr(&serialized, &length); |
| 504 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 505 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 506 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 507 | if (!j->user) |
| 508 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 509 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 510 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 511 | if (j->chrootdir) { /* stale pointer */ |
| 512 | char *chrootdir = consumestr(&serialized, &length); |
| 513 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 514 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 515 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 516 | if (!j->chrootdir) |
| 517 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 520 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 521 | size_t ninstrs = j->filter_len; |
| 522 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 523 | ninstrs > USHRT_MAX) |
| 524 | goto bad_filters; |
| 525 | |
| 526 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 527 | void *program = consumebytes(program_len, &serialized, &length); |
| 528 | if (!program) |
| 529 | goto bad_filters; |
| 530 | |
| 531 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
| 532 | j->filter_prog->len = ninstrs; |
| 533 | j->filter_prog->filter = malloc(program_len); |
| 534 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 535 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 536 | |
| 537 | count = j->binding_count; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 538 | j->binding_count = 0; |
| 539 | for (i = 0; i < count; ++i) { |
| 540 | int *writeable; |
| 541 | const char *dest; |
| 542 | const char *src = consumestr(&serialized, &length); |
| 543 | if (!src) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 544 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 545 | dest = consumestr(&serialized, &length); |
| 546 | if (!dest) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 547 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 548 | writeable = consumebytes(sizeof(*writeable), &serialized, &length); |
| 549 | if (!writeable) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 550 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 551 | if (minijail_bind(j, src, dest, *writeable)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 552 | goto bad_bindings; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 553 | } |
| 554 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 555 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 556 | |
| 557 | bad_bindings: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 558 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 559 | free(j->filter_prog->filter); |
| 560 | free(j->filter_prog); |
| 561 | } |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 562 | bad_filters: |
| 563 | if (j->chrootdir) |
| 564 | free(j->chrootdir); |
| 565 | bad_chrootdir: |
| 566 | if (j->user) |
| 567 | free(j->user); |
| 568 | clear_pointers: |
| 569 | j->user = NULL; |
| 570 | j->chrootdir = NULL; |
| 571 | out: |
| 572 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 573 | } |
| 574 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 575 | /* bind_one: Applies bindings from @b for @j, recursing as needed. |
| 576 | * @j Minijail these bindings are for |
| 577 | * @b Head of list of bindings |
| 578 | * |
| 579 | * Returns 0 for success. |
| 580 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 581 | int bind_one(const struct minijail *j, struct binding *b) |
| 582 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 583 | int ret = 0; |
| 584 | char *dest = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 585 | if (ret) |
| 586 | return ret; |
| 587 | /* dest has a leading "/" */ |
| 588 | if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0) |
| 589 | return -ENOMEM; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 590 | ret = mount(b->src, dest, NULL, MS_BIND, NULL); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 591 | if (ret) |
| 592 | pdie("bind: %s -> %s", b->src, dest); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 593 | if (!b->writeable) { |
| 594 | ret = mount(b->src, dest, NULL, |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 595 | MS_BIND | MS_REMOUNT | MS_RDONLY, NULL); |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 596 | if (ret) |
| 597 | pdie("bind ro: %s -> %s", b->src, dest); |
| 598 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 599 | free(dest); |
| 600 | if (b->next) |
| 601 | return bind_one(j, b->next); |
| 602 | return ret; |
| 603 | } |
| 604 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 605 | int enter_chroot(const struct minijail *j) |
| 606 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 607 | int ret; |
| 608 | if (j->bindings_head && (ret = bind_one(j, j->bindings_head))) |
| 609 | return ret; |
| 610 | |
| 611 | if (chroot(j->chrootdir)) |
| 612 | return -errno; |
| 613 | |
| 614 | if (chdir("/")) |
| 615 | return -errno; |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 620 | int mount_tmp(void) |
| 621 | { |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 622 | return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 625 | int remount_readonly(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 626 | { |
| 627 | const char *kProcPath = "/proc"; |
| 628 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 629 | /* |
| 630 | * Right now, we're holding a reference to our parent's old mount of |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 631 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 632 | * mutate our parent's mount as well, even though we're in a VFS |
| 633 | * namespace (!). Instead, remove their mount from our namespace |
| 634 | * and make our own. |
| 635 | */ |
| 636 | if (umount(kProcPath)) |
| 637 | return -errno; |
| 638 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 639 | return -errno; |
| 640 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 641 | } |
| 642 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 643 | void drop_ugid(const struct minijail *j) |
| 644 | { |
| 645 | if (j->flags.usergroups) { |
| 646 | if (initgroups(j->user, j->usergid)) |
| 647 | pdie("initgroups"); |
| 648 | } else { |
| 649 | /* Only attempt to clear supplemental groups if we are changing |
| 650 | * users. */ |
| 651 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 652 | pdie("setgroups"); |
| 653 | } |
| 654 | |
| 655 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 656 | pdie("setresgid"); |
| 657 | |
| 658 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 659 | pdie("setresuid"); |
| 660 | } |
| 661 | |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 662 | /* |
| 663 | * We specifically do not use cap_valid() as that only tells us the last |
| 664 | * valid cap we were *compiled* against (i.e. what the version of kernel |
| 665 | * headers says). If we run on a different kernel version, then it's not |
| 666 | * uncommon for that to be less (if an older kernel) or more (if a newer |
| 667 | * kernel). So suck up the answer via /proc. |
| 668 | */ |
| 669 | static int run_cap_valid(unsigned int cap) |
| 670 | { |
| 671 | static unsigned int last_cap; |
| 672 | |
| 673 | if (!last_cap) { |
| 674 | const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
| 675 | FILE *fp = fopen(cap_file, "re"); |
| 676 | if (fscanf(fp, "%u", &last_cap) != 1) |
| 677 | pdie("fscanf(%s)", cap_file); |
| 678 | fclose(fp); |
| 679 | } |
| 680 | |
| 681 | return cap <= last_cap; |
| 682 | } |
| 683 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 684 | void drop_caps(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 685 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 686 | #if defined(__BRILLO__) |
| 687 | /* |
| 688 | * Temporarily disable capabilities support until Minijail can use |
| 689 | * libcap-ng. |
| 690 | */ |
| 691 | (void) j; |
| 692 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 693 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 694 | cap_value_t flag[1]; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 695 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 696 | unsigned int i; |
| 697 | if (!caps) |
| 698 | die("can't get process caps"); |
| 699 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 700 | die("can't clear inheritable caps"); |
| 701 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 702 | die("can't clear effective caps"); |
| 703 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 704 | die("can't clear permitted caps"); |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 705 | for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 706 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 707 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 708 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 709 | flag[0] = i; |
| 710 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 711 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 712 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 713 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 714 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 715 | die("can't add inheritable cap"); |
| 716 | } |
| 717 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 718 | die("can't apply initial cleaned capset"); |
| 719 | |
| 720 | /* |
| 721 | * Instead of dropping bounding set first, do it here in case |
| 722 | * the caller had a more permissive bounding set which could |
| 723 | * have been used above to raise a capability that wasn't already |
| 724 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 725 | */ |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 726 | for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) { |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 727 | if (j->caps & (one << i)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 728 | continue; |
| 729 | if (prctl(PR_CAPBSET_DROP, i)) |
| 730 | pdie("prctl(PR_CAPBSET_DROP)"); |
| 731 | } |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 732 | |
| 733 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 734 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 735 | flag[0] = CAP_SETPCAP; |
| 736 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 737 | die("can't clear effective cap"); |
| 738 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 739 | die("can't clear permitted cap"); |
| 740 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 741 | die("can't clear inheritable cap"); |
| 742 | } |
| 743 | |
| 744 | if (cap_set_proc(caps)) |
| 745 | die("can't apply final cleaned capset"); |
| 746 | |
| 747 | cap_free(caps); |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 748 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 749 | } |
| 750 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 751 | void set_seccomp_filter(const struct minijail *j) |
| 752 | { |
| 753 | /* |
| 754 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 755 | * in the kernel source tree for an explanation of the parameters. |
| 756 | */ |
| 757 | if (j->flags.no_new_privs) { |
| 758 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 759 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * If we're logging seccomp filter failures, |
| 764 | * install the SIGSYS handler first. |
| 765 | */ |
| 766 | if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) { |
| 767 | if (install_sigsys_handler()) |
| 768 | pdie("install SIGSYS handler"); |
| 769 | warn("logging seccomp filter failures"); |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * Install the syscall filter. |
| 774 | */ |
| 775 | if (j->flags.seccomp_filter) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 776 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) { |
| 777 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 778 | warn("seccomp not supported"); |
| 779 | return; |
| 780 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 781 | pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 782 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 786 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 787 | { |
| 788 | if (j->flags.pids) |
| 789 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 790 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 791 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 792 | if (j->flags.usergroups && !j->user) |
| 793 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 794 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 795 | /* |
| 796 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 797 | * so we don't even try. If any of our operations fail, we abort() the |
| 798 | * entire process. |
| 799 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 800 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
| 801 | pdie("setns(CLONE_NEWNS)"); |
| 802 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 803 | if (j->flags.vfs && unshare(CLONE_NEWNS)) |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 804 | pdie("unshare(vfs)"); |
| 805 | |
| 806 | if (j->flags.net && unshare(CLONE_NEWNET)) |
| 807 | pdie("unshare(net)"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 808 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 809 | if (j->flags.chroot && enter_chroot(j)) |
| 810 | pdie("chroot"); |
| 811 | |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 812 | if (j->flags.mount_tmp && mount_tmp()) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 813 | pdie("mount_tmp"); |
| 814 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 815 | if (j->flags.readonly && remount_readonly()) |
| 816 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 817 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 818 | if (j->flags.caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 819 | /* |
| 820 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 821 | * capability to change uids, our attempt to use setuid() |
| 822 | * below will fail. Hang on to root caps across setuid(), then |
| 823 | * lock securebits. |
| 824 | */ |
| 825 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 826 | pdie("prctl(PR_SET_KEEPCAPS)"); |
| 827 | if (prctl |
| 828 | (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS)) |
| 829 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 830 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 831 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 832 | /* |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 833 | * If we're setting no_new_privs, we can drop privileges |
| 834 | * before setting seccomp filter. This way filter policies |
| 835 | * don't need to allow privilege-dropping syscalls. |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 836 | */ |
| 837 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 838 | drop_ugid(j); |
| 839 | if (j->flags.caps) |
| 840 | drop_caps(j); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 841 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 842 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 843 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 844 | /* |
| 845 | * If we're not setting no_new_privs, |
| 846 | * we need to set seccomp filter *before* dropping privileges. |
| 847 | * WARNING: this means that filter policies *must* allow |
| 848 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 849 | * capget()/capset()/prctl() for dropping caps. |
| 850 | */ |
| 851 | set_seccomp_filter(j); |
| 852 | |
| 853 | drop_ugid(j); |
| 854 | if (j->flags.caps) |
| 855 | drop_caps(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 856 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 857 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 858 | /* |
| 859 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 860 | * privilege-dropping syscalls :) |
| 861 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 862 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
| 863 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 864 | warn("seccomp not supported"); |
| 865 | return; |
| 866 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 867 | pdie("prctl(PR_SET_SECCOMP)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 868 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 869 | } |
| 870 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 871 | /* TODO(wad) will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 872 | static int init_exitstatus = 0; |
| 873 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 874 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 875 | { |
| 876 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 879 | int init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 880 | { |
| 881 | pid_t pid; |
| 882 | int status; |
| 883 | /* so that we exit with the right status */ |
| 884 | signal(SIGTERM, init_term); |
| 885 | /* TODO(wad) self jail with seccomp_filters here. */ |
| 886 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 887 | /* |
| 888 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 889 | * left inside our pid namespace or we get a signal. |
| 890 | */ |
| 891 | if (pid == rootpid) |
| 892 | init_exitstatus = status; |
| 893 | } |
| 894 | if (!WIFEXITED(init_exitstatus)) |
| 895 | _exit(MINIJAIL_ERR_INIT); |
| 896 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 897 | } |
| 898 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 899 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 900 | { |
| 901 | size_t sz = 0; |
| 902 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 903 | char *buf; |
| 904 | int r; |
| 905 | if (sizeof(sz) != bytes) |
| 906 | return -EINVAL; |
| 907 | if (sz > USHRT_MAX) /* Arbitrary sanity check */ |
| 908 | return -E2BIG; |
| 909 | buf = malloc(sz); |
| 910 | if (!buf) |
| 911 | return -ENOMEM; |
| 912 | bytes = read(fd, buf, sz); |
| 913 | if (bytes != sz) { |
| 914 | free(buf); |
| 915 | return -EINVAL; |
| 916 | } |
| 917 | r = minijail_unmarshal(j, buf, sz); |
| 918 | free(buf); |
| 919 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 920 | } |
| 921 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 922 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 923 | { |
| 924 | char *buf; |
| 925 | size_t sz = minijail_size(j); |
| 926 | ssize_t written; |
| 927 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 928 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 929 | if (!sz) |
| 930 | return -EINVAL; |
| 931 | buf = malloc(sz); |
| 932 | r = minijail_marshal(j, buf, sz); |
| 933 | if (r) { |
| 934 | free(buf); |
| 935 | return r; |
| 936 | } |
| 937 | /* Sends [size][minijail]. */ |
| 938 | written = write(fd, &sz, sizeof(sz)); |
| 939 | if (written != sizeof(sz)) { |
| 940 | free(buf); |
| 941 | return -EFAULT; |
| 942 | } |
| 943 | written = write(fd, buf, sz); |
| 944 | if (written < 0 || (size_t) written != sz) { |
| 945 | free(buf); |
| 946 | return -EFAULT; |
| 947 | } |
| 948 | free(buf); |
| 949 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 950 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 951 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 952 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 953 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 954 | #if defined(__BRILLO__) |
| 955 | /* Don't use LDPRELOAD on Brillo. */ |
| 956 | return 0; |
| 957 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 958 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 959 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 960 | if (!newenv) |
| 961 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 962 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 963 | /* Only insert a separating space if we have something to separate... */ |
| 964 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 965 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 966 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 967 | /* setenv() makes a copy of the string we give it */ |
| 968 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 969 | free(newenv); |
| 970 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 971 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 972 | } |
| 973 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 974 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 975 | { |
| 976 | int r = pipe(fds); |
| 977 | char fd_buf[11]; |
| 978 | if (r) |
| 979 | return r; |
| 980 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 981 | if (r <= 0) |
| 982 | return -EINVAL; |
| 983 | setenv(kFdEnvVar, fd_buf, 1); |
| 984 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 985 | } |
| 986 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 987 | int setup_pipe_end(int fds[2], size_t index) |
| 988 | { |
| 989 | if (index > 1) |
| 990 | return -1; |
| 991 | |
| 992 | close(fds[1 - index]); |
| 993 | return fds[index]; |
| 994 | } |
| 995 | |
| 996 | int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd) |
| 997 | { |
| 998 | if (index > 1) |
| 999 | return -1; |
| 1000 | |
| 1001 | close(fds[1 - index]); |
| 1002 | /* dup2(2) the corresponding end of the pipe into |fd|. */ |
| 1003 | return dup2(fds[index], fd); |
| 1004 | } |
| 1005 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1006 | int API minijail_run(struct minijail *j, const char *filename, |
| 1007 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1008 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1009 | return minijail_run_pid_pipes(j, filename, argv, |
| 1010 | NULL, NULL, NULL, NULL); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 1014 | char *const argv[], pid_t *pchild_pid) |
| 1015 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1016 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, |
| 1017 | NULL, NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1021 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1022 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1023 | return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd, |
| 1024 | NULL, NULL); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | int API minijail_run_pid_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1028 | char *const argv[], pid_t *pchild_pid, |
| 1029 | int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1030 | { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1031 | return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd, |
| 1032 | NULL, NULL); |
| 1033 | } |
| 1034 | |
| 1035 | int API minijail_run_pid_pipes(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 1036 | char *const argv[], pid_t *pchild_pid, |
| 1037 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1038 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1039 | char *oldenv, *oldenv_copy = NULL; |
| 1040 | pid_t child_pid; |
| 1041 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1042 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1043 | int stdout_fds[2]; |
| 1044 | int stderr_fds[2]; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1045 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1046 | /* We need to remember this across the minijail_preexec() call. */ |
| 1047 | int pid_namespace = j->flags.pids; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1048 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1049 | oldenv = getenv(kLdPreloadEnvVar); |
| 1050 | if (oldenv) { |
| 1051 | oldenv_copy = strdup(oldenv); |
| 1052 | if (!oldenv_copy) |
| 1053 | return -ENOMEM; |
| 1054 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1055 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1056 | if (setup_preload()) |
| 1057 | return -EFAULT; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1058 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1059 | /* |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 1060 | * Make the process group ID of this process equal to its PID, so that |
| 1061 | * both the Minijail process and the jailed process can be killed |
| 1062 | * together. |
| 1063 | * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when |
| 1064 | * the process is already a process group leader. |
| 1065 | */ |
| 1066 | if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) { |
| 1067 | if (errno != EPERM) { |
| 1068 | pdie("setpgid(0, 0)"); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1073 | * Before we fork(2) and execve(2) the child process, we need to open |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1074 | * a pipe(2) to send the minijail configuration over. |
| 1075 | */ |
| 1076 | if (setup_pipe(pipe_fds)) |
| 1077 | return -EFAULT; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1078 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1079 | /* |
| 1080 | * If we want to write to the child process' standard input, |
| 1081 | * create the pipe(2) now. |
| 1082 | */ |
| 1083 | if (pstdin_fd) { |
| 1084 | if (pipe(stdin_fds)) |
| 1085 | return -EFAULT; |
| 1086 | } |
| 1087 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1088 | /* |
| 1089 | * If we want to read from the child process' standard output, |
| 1090 | * create the pipe(2) now. |
| 1091 | */ |
| 1092 | if (pstdout_fd) { |
| 1093 | if (pipe(stdout_fds)) |
| 1094 | return -EFAULT; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * If we want to read from the child process' standard error, |
| 1099 | * create the pipe(2) now. |
| 1100 | */ |
| 1101 | if (pstderr_fd) { |
| 1102 | if (pipe(stderr_fds)) |
| 1103 | return -EFAULT; |
| 1104 | } |
| 1105 | |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1106 | /* Use sys_clone() if and only if we're creating a pid namespace. |
| 1107 | * |
| 1108 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 1109 | * |
| 1110 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 1111 | * some of which may be held by other threads at the time that we call |
| 1112 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 1113 | * ensure that we hold all of these locks before it calls clone() |
| 1114 | * internally and drop them after clone() returns, but when we call |
| 1115 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 1116 | * child address space where some of libc's important locks are held by |
| 1117 | * other threads (which did not get cloned, and hence will never release |
| 1118 | * those locks). This is okay so long as we call exec() immediately |
| 1119 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 1120 | * take locks. |
| 1121 | * |
| 1122 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 1123 | * namespacing. If we follow this path, the child's address space might |
| 1124 | * have broken locks; you may only call functions that do not acquire |
| 1125 | * any locks. |
| 1126 | * |
| 1127 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 1128 | * previously detailed, so this function is highly likely to deadlock |
| 1129 | * later on (see "deadlock here") if we're multithreaded. |
| 1130 | * |
| 1131 | * We might hack around this by having the clone()d child (init of the |
| 1132 | * pid namespace) return directly, rather than leaving the clone()d |
| 1133 | * process hanging around to be init for the new namespace (and having |
| 1134 | * its fork()ed child return in turn), but that process would be crippled |
| 1135 | * with its libc locks potentially broken. We might try fork()ing in the |
| 1136 | * parent before we clone() to ensure that we own all the locks, but |
| 1137 | * then we have to have the forked child hanging around consuming |
| 1138 | * resources (and possibly having file descriptors / shared memory |
| 1139 | * regions / etc attached). We'd need to keep the child around to avoid |
| 1140 | * having its children get reparented to init. |
| 1141 | * |
| 1142 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 1143 | * problem is fixable or not. It would be nice if we worked in this |
| 1144 | * case. |
| 1145 | */ |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1146 | if (pid_namespace) |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1147 | child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL); |
| 1148 | else |
| 1149 | child_pid = fork(); |
| 1150 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1151 | if (child_pid < 0) { |
| 1152 | free(oldenv_copy); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1153 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1154 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1155 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1156 | if (child_pid) { |
| 1157 | /* Restore parent's LD_PRELOAD. */ |
| 1158 | if (oldenv_copy) { |
| 1159 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 1160 | free(oldenv_copy); |
| 1161 | } else { |
| 1162 | unsetenv(kLdPreloadEnvVar); |
| 1163 | } |
| 1164 | unsetenv(kFdEnvVar); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1165 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1166 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1167 | |
| 1168 | /* Send marshalled minijail. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1169 | close(pipe_fds[0]); /* read endpoint */ |
| 1170 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 1171 | close(pipe_fds[1]); /* write endpoint */ |
| 1172 | if (ret) { |
| 1173 | kill(j->initpid, SIGKILL); |
| 1174 | die("failed to send marshalled minijail"); |
| 1175 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1176 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1177 | if (pchild_pid) |
| 1178 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1179 | |
| 1180 | /* |
| 1181 | * If we want to write to the child process' standard input, |
| 1182 | * set up the write end of the pipe. |
| 1183 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1184 | if (pstdin_fd) |
| 1185 | *pstdin_fd = setup_pipe_end(stdin_fds, |
| 1186 | 1 /* write end */); |
| 1187 | |
| 1188 | /* |
| 1189 | * If we want to read from the child process' standard output, |
| 1190 | * set up the read end of the pipe. |
| 1191 | */ |
| 1192 | if (pstdout_fd) |
| 1193 | *pstdout_fd = setup_pipe_end(stdout_fds, |
| 1194 | 0 /* read end */); |
| 1195 | |
| 1196 | /* |
| 1197 | * If we want to read from the child process' standard error, |
| 1198 | * set up the read end of the pipe. |
| 1199 | */ |
| 1200 | if (pstderr_fd) |
| 1201 | *pstderr_fd = setup_pipe_end(stderr_fds, |
| 1202 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1203 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1207 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1208 | /* |
| 1209 | * If we want to write to the jailed process' standard input, |
| 1210 | * set up the read end of the pipe. |
| 1211 | */ |
| 1212 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1213 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 1214 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1215 | die("failed to set up stdin pipe"); |
| 1216 | } |
| 1217 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1218 | /* |
| 1219 | * If we want to read from the jailed process' standard output, |
| 1220 | * set up the write end of the pipe. |
| 1221 | */ |
| 1222 | if (pstdout_fd) { |
| 1223 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 1224 | STDOUT_FILENO) < 0) |
| 1225 | die("failed to set up stdout pipe"); |
| 1226 | } |
| 1227 | |
| 1228 | /* |
| 1229 | * If we want to read from the jailed process' standard error, |
| 1230 | * set up the write end of the pipe. |
| 1231 | */ |
| 1232 | if (pstderr_fd) { |
| 1233 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 1234 | STDERR_FILENO) < 0) |
| 1235 | die("failed to set up stderr pipe"); |
| 1236 | } |
| 1237 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 1238 | /* Strip out flags that cannot be inherited across execve. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1239 | minijail_preexec(j); |
| 1240 | /* Jail this process and its descendants... */ |
| 1241 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1242 | |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1243 | if (pid_namespace) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1244 | /* |
| 1245 | * pid namespace: this process will become init inside the new |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1246 | * namespace, so fork off a child to actually run the program |
| 1247 | * (we don't want all programs we might exec to have to know |
| 1248 | * how to be init). |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1249 | * |
| 1250 | * If we're multithreaded, we'll probably deadlock here. See |
| 1251 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1252 | */ |
| 1253 | child_pid = fork(); |
| 1254 | if (child_pid < 0) |
| 1255 | _exit(child_pid); |
| 1256 | else if (child_pid > 0) |
| 1257 | init(child_pid); /* never returns */ |
| 1258 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1259 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1260 | /* |
| 1261 | * If we aren't pid-namespaced: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1262 | * calling process |
| 1263 | * -> execve()-ing process |
| 1264 | * If we are: |
| 1265 | * calling process |
| 1266 | * -> init()-ing process |
| 1267 | * -> execve()-ing process |
| 1268 | */ |
| 1269 | _exit(execve(filename, argv, environ)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1270 | } |
| 1271 | |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1272 | int API minijail_run_static(struct minijail *j, const char *filename, |
| 1273 | char *const argv[]) |
| 1274 | { |
| 1275 | pid_t child_pid; |
| 1276 | int pid_namespace = j->flags.pids; |
| 1277 | |
| 1278 | if (j->flags.caps) |
| 1279 | die("caps not supported with static targets"); |
| 1280 | |
| 1281 | if (pid_namespace) |
| 1282 | child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL); |
| 1283 | else |
| 1284 | child_pid = fork(); |
| 1285 | |
| 1286 | if (child_pid < 0) { |
| 1287 | die("failed to fork child"); |
| 1288 | } |
| 1289 | if (child_pid > 0 ) { |
| 1290 | j->initpid = child_pid; |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * We can now drop this child into the sandbox |
| 1296 | * then execve the target. |
| 1297 | */ |
| 1298 | |
| 1299 | j->flags.pids = 0; |
| 1300 | minijail_enter(j); |
| 1301 | |
| 1302 | if (pid_namespace) { |
| 1303 | /* |
| 1304 | * pid namespace: this process will become init inside the new |
| 1305 | * namespace, so fork off a child to actually run the program |
| 1306 | * (we don't want all programs we might exec to have to know |
| 1307 | * how to be init). |
| 1308 | * |
| 1309 | * If we're multithreaded, we'll probably deadlock here. See |
| 1310 | * WARNING above. |
| 1311 | */ |
| 1312 | child_pid = fork(); |
| 1313 | if (child_pid < 0) |
| 1314 | _exit(child_pid); |
| 1315 | else if (child_pid > 0) |
| 1316 | init(child_pid); /* never returns */ |
| 1317 | } |
| 1318 | |
| 1319 | _exit(execve(filename, argv, environ)); |
| 1320 | } |
| 1321 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1322 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1323 | { |
| 1324 | int st; |
| 1325 | if (kill(j->initpid, SIGTERM)) |
| 1326 | return -errno; |
| 1327 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1328 | return -errno; |
| 1329 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1330 | } |
| 1331 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1332 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1333 | { |
| 1334 | int st; |
| 1335 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1336 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1337 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1338 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1339 | int error_status = st; |
| 1340 | if (WIFSIGNALED(st)) { |
| 1341 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1342 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1343 | j->initpid, signum); |
| 1344 | /* |
| 1345 | * We return MINIJAIL_ERR_JAIL if the process received |
| 1346 | * SIGSYS, which happens when a syscall is blocked by |
| 1347 | * seccomp filters. |
| 1348 | * If not, we do what bash(1) does: |
| 1349 | * $? = 128 + signum |
| 1350 | */ |
| 1351 | if (signum == SIGSYS) { |
| 1352 | error_status = MINIJAIL_ERR_JAIL; |
| 1353 | } else { |
| 1354 | error_status = 128 + signum; |
| 1355 | } |
| 1356 | } |
| 1357 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1358 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1359 | |
| 1360 | int exit_status = WEXITSTATUS(st); |
| 1361 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1362 | info("child process %d exited with status %d", |
| 1363 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1364 | |
| 1365 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1366 | } |
| 1367 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1368 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1369 | { |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1370 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 1371 | free(j->filter_prog->filter); |
| 1372 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1373 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1374 | while (j->bindings_head) { |
| 1375 | struct binding *b = j->bindings_head; |
| 1376 | j->bindings_head = j->bindings_head->next; |
| 1377 | free(b->dest); |
| 1378 | free(b->src); |
| 1379 | free(b); |
| 1380 | } |
| 1381 | j->bindings_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1382 | if (j->user) |
| 1383 | free(j->user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1384 | if (j->chrootdir) |
| 1385 | free(j->chrootdir); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1386 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1387 | } |