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