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