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