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