blob: 0a7df3481db259016f3a7d16808455a28deda825 [file] [log] [blame]
Elly Jonescd7a9042011-07-22 13:56:51 -04001/* 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 Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
6#define _BSD_SOURCE
7#define _GNU_SOURCE
Will Drewry32ac9f52011-08-18 21:36:27 -05008#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -04009#include <errno.h>
10#include <grp.h>
11#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050012#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <linux/capability.h>
14#include <linux/securebits.h>
15#include <pwd.h>
16#include <sched.h>
17#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050018#include <stdarg.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040019#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 Drewryf89aef52011-09-16 16:48:57 -050025#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040026#include <sys/prctl.h>
27#include <sys/wait.h>
28#include <syslog.h>
29#include <unistd.h>
30
31#include "libminijail.h"
Will Drewry32ac9f52011-08-18 21:36:27 -050032#include "libsyscalls.h"
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include "libminijail-private.h"
34
Will Drewry32ac9f52011-08-18 21:36:27 -050035/* Until these are reliably available in linux/prctl.h */
36#ifndef PR_SET_SECCOMP_FILTER
Elly Jonese1749eb2011-10-07 13:54:59 -040037# 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 Drewry32ac9f52011-08-18 21:36:27 -050042#endif
43
Will Drewry32ac9f52011-08-18 21:36:27 -050044#define die(_msg, ...) do { \
Elly Jonese1749eb2011-10-07 13:54:59 -040045 syslog(LOG_ERR, "libminijail: " _msg, ## __VA_ARGS__); \
46 abort(); \
Will Drewry32ac9f52011-08-18 21:36:27 -050047} while (0)
Elly Jonescd7a9042011-07-22 13:56:51 -040048
Will Drewry32ac9f52011-08-18 21:36:27 -050049#define pdie(_msg, ...) \
Elly Jonese1749eb2011-10-07 13:54:59 -040050 die(_msg ": %s", ## __VA_ARGS__, strerror(errno))
Will Drewry32ac9f52011-08-18 21:36:27 -050051
52#define warn(_msg, ...) \
Elly Jonese1749eb2011-10-07 13:54:59 -040053 syslog(LOG_WARNING, "libminijail: " _msg, ## __VA_ARGS__)
Elly Jonescd7a9042011-07-22 13:56:51 -040054
Will Drewryf89aef52011-09-16 16:48:57 -050055struct seccomp_filter {
Elly Jonese1749eb2011-10-07 13:54:59 -040056 int nr;
57 char *filter;
58 struct seccomp_filter *next, *prev;
Will Drewryf89aef52011-09-16 16:48:57 -050059};
60
Elly Jones51a5b6c2011-10-12 19:09:26 -040061struct binding {
62 char *src;
63 char *dest;
64 int writeable;
65 struct binding *next;
66};
67
Will Drewryf89aef52011-09-16 16:48:57 -050068struct minijail {
Elly Jonese1749eb2011-10-07 13:54:59 -040069 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 Jones51a5b6c2011-10-12 19:09:26 -040080 int chroot:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040081 } 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 Jones51a5b6c2011-10-12 19:09:26 -040089 int binding_count;
90 char *chrootdir;
Elly Jonese1749eb2011-10-07 13:54:59 -040091 struct seccomp_filter *filters;
Elly Jones51a5b6c2011-10-12 19:09:26 -040092 struct binding *bindings_head;
93 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -050094};
95
Elly Jonese1749eb2011-10-07 13:54:59 -040096struct minijail *minijail_new(void)
97{
Elly Jones51a5b6c2011-10-12 19:09:26 -040098 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -040099}
100
Elly Jonese1749eb2011-10-07 13:54:59 -0400101void minijail_change_uid(struct minijail *j, uid_t uid)
102{
103 if (uid == 0)
104 die("useless change to uid 0");
105 j->uid = uid;
106 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400107}
108
Elly Jonese1749eb2011-10-07 13:54:59 -0400109void minijail_change_gid(struct minijail *j, gid_t gid)
110{
111 if (gid == 0)
112 die("useless change to gid 0");
113 j->gid = gid;
114 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400115}
116
Elly Jonese1749eb2011-10-07 13:54:59 -0400117int minijail_change_user(struct minijail *j, const char *user)
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 Joneseb300c52011-09-22 14:35:43 -0400125
Elly Jonese1749eb2011-10-07 13:54:59 -0400126 /* 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 Jonescd7a9042011-07-22 13:56:51 -0400142}
143
Elly Jonese1749eb2011-10-07 13:54:59 -0400144int minijail_change_group(struct minijail *j, const char *group)
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 Joneseb300c52011-09-22 14:35:43 -0400152
Elly Jonese1749eb2011-10-07 13:54:59 -0400153 /* 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 Jonescd7a9042011-07-22 13:56:51 -0400165}
166
Elly Jonese1749eb2011-10-07 13:54:59 -0400167void minijail_use_seccomp(struct minijail *j)
168{
169 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400170}
171
Elly Jonese1749eb2011-10-07 13:54:59 -0400172void minijail_use_seccomp_filter(struct minijail *j)
173{
174 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500175}
176
Elly Jonese1749eb2011-10-07 13:54:59 -0400177void minijail_use_caps(struct minijail *j, uint64_t capmask)
178{
179 j->caps = capmask;
180 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400181}
182
Elly Jonese1749eb2011-10-07 13:54:59 -0400183void minijail_namespace_vfs(struct minijail *j)
184{
185 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400186}
187
Elly Jonese1749eb2011-10-07 13:54:59 -0400188void minijail_namespace_pids(struct minijail *j)
189{
190 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400191}
192
Elly Jonese1749eb2011-10-07 13:54:59 -0400193void minijail_remount_readonly(struct minijail *j)
194{
195 j->flags.vfs = 1;
196 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400197}
198
Elly Jonese1749eb2011-10-07 13:54:59 -0400199void minijail_inherit_usergroups(struct minijail *j)
200{
201 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400202}
203
Elly Jonese1749eb2011-10-07 13:54:59 -0400204void minijail_disable_ptrace(struct minijail *j)
205{
206 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400207}
208
Elly Jones51a5b6c2011-10-12 19:09:26 -0400209int minijail_enter_chroot(struct minijail *j, const char *dir) {
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
219int minijail_bind(struct minijail *j, const char *src, const char *dest,
220 int writeable) {
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
252error:
253 free(b->src);
254 free(b->dest);
255 free(b);
256 return -ENOMEM;
257}
258
Elly Jonese1749eb2011-10-07 13:54:59 -0400259int minijail_add_seccomp_filter(struct minijail *j, int nr, const char *filter)
260{
261 struct seccomp_filter *sf;
262 if (!filter || nr < 0)
263 return -EINVAL;
Will Drewry32ac9f52011-08-18 21:36:27 -0500264
Elly Jonese1749eb2011-10-07 13:54:59 -0400265 sf = malloc(sizeof(*sf));
266 if (!sf)
267 return -ENOMEM;
268 sf->nr = nr;
269 sf->filter = strndup(filter, MINIJAIL_MAX_SECCOMP_FILTER_LINE);
270 if (!sf->filter) {
271 free(sf);
272 return -ENOMEM;
273 }
Will Drewry32ac9f52011-08-18 21:36:27 -0500274
Elly Jonese1749eb2011-10-07 13:54:59 -0400275 j->filter_count++;
Will Drewryf89aef52011-09-16 16:48:57 -0500276
Elly Jonese1749eb2011-10-07 13:54:59 -0400277 if (!j->filters) {
278 j->filters = sf;
279 sf->next = sf;
280 sf->prev = sf;
281 return 0;
282 }
283 sf->next = j->filters;
284 sf->prev = j->filters->prev;
285 sf->prev->next = sf;
286 j->filters->prev = sf;
287 return 0;
Will Drewry32ac9f52011-08-18 21:36:27 -0500288}
289
Elly Jonese1749eb2011-10-07 13:54:59 -0400290int minijail_lookup_syscall(const char *name)
291{
292 const struct syscall_entry *entry = syscall_table;
293 for (; entry->name && entry->nr >= 0; ++entry)
294 if (!strcmp(entry->name, name))
295 return entry->nr;
296 return -1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500297}
298
Elly Jonese1749eb2011-10-07 13:54:59 -0400299static char *strip(char *s)
300{
301 char *end;
302 while (*s && isblank(*s))
303 s++;
304 end = s + strlen(s) - 1;
305 while (*end && (isblank(*end) || *end == '\n'))
306 end--;
307 *(end + 1) = '\0';
308 return s;
Will Drewry32ac9f52011-08-18 21:36:27 -0500309}
310
Elly Jonese1749eb2011-10-07 13:54:59 -0400311void minijail_parse_seccomp_filters(struct minijail *j, const char *path)
312{
313 FILE *file = fopen(path, "r");
314 char line[MINIJAIL_MAX_SECCOMP_FILTER_LINE];
Ben Chan1d697932011-10-14 10:53:32 -0700315 int count = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -0400316 if (!file)
317 pdie("failed to open seccomp filters file");
Will Drewry32ac9f52011-08-18 21:36:27 -0500318
Elly Jonese1749eb2011-10-07 13:54:59 -0400319 /* Format is simple:
320 * syscall_name<COLON><FILTER STRING>[\n|EOF]
321 * #...comment...
322 * <empty line?
323 */
324 while (fgets(line, sizeof(line), file)) {
325 char *filter = line;
326 char *name = strsep(&filter, ":");
327 char *name_end = NULL;
328 int nr = -1;
Ben Chan1d697932011-10-14 10:53:32 -0700329 count++;
Will Drewry32ac9f52011-08-18 21:36:27 -0500330
Ben Chan1d697932011-10-14 10:53:32 -0700331 /* Allow comment lines */
332 if (*name == '#')
333 continue;
Will Drewry32ac9f52011-08-18 21:36:27 -0500334
Elly Jonese1749eb2011-10-07 13:54:59 -0400335 name = strip(name);
Will Drewry32ac9f52011-08-18 21:36:27 -0500336
Elly Jonese1749eb2011-10-07 13:54:59 -0400337 if (!filter) {
338 if (strlen(name))
339 die("invalid filter on line %d", count);
340 /* Allow empty lines */
341 continue;
342 }
Will Drewry32ac9f52011-08-18 21:36:27 -0500343
Elly Jonese1749eb2011-10-07 13:54:59 -0400344 filter = strip(filter);
Will Drewry32ac9f52011-08-18 21:36:27 -0500345
Elly Jonese1749eb2011-10-07 13:54:59 -0400346 /* Take direct syscall numbers */
347 nr = strtol(name, &name_end, 0);
348 /* Or fail-over to using names */
349 if (*name_end != '\0')
350 nr = minijail_lookup_syscall(name);
351 if (nr < 0)
352 die("syscall '%s' unknown", name);
Will Drewry32ac9f52011-08-18 21:36:27 -0500353
Elly Jonese1749eb2011-10-07 13:54:59 -0400354 if (minijail_add_seccomp_filter(j, nr, filter))
355 pdie("failed to add filter for syscall '%s'", name);
356 }
357 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500358}
359
Will Drewryf89aef52011-09-16 16:48:57 -0500360struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400361 size_t available;
362 size_t total;
363 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500364};
365
366static void marshal_state_init(struct marshal_state *state,
Elly Jonese1749eb2011-10-07 13:54:59 -0400367 char *buf, size_t available)
368{
369 state->available = available;
370 state->buf = buf;
371 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500372}
373
374static void marshal_append(struct marshal_state *state,
Elly Jonese1749eb2011-10-07 13:54:59 -0400375 char *src, size_t length)
376{
377 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500378
Elly Jonese1749eb2011-10-07 13:54:59 -0400379 /* Up to |available| will be written. */
380 if (copy_len) {
381 memcpy(state->buf, src, copy_len);
382 state->buf += copy_len;
383 state->available -= copy_len;
384 }
385 /* |total| will contain the expected length. */
386 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500387}
388
389static void minijail_marshal_helper(struct marshal_state *state,
Elly Jonese1749eb2011-10-07 13:54:59 -0400390 const struct minijail *j)
391{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400392 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400393 marshal_append(state, (char *)j, sizeof(*j));
394 if (j->user)
395 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400396 if (j->chrootdir)
397 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Elly Jonese1749eb2011-10-07 13:54:59 -0400398 if (j->flags.seccomp_filter && j->filters) {
399 struct seccomp_filter *f = j->filters;
400 do {
401 marshal_append(state, (char *)&f->nr, sizeof(f->nr));
402 marshal_append(state, f->filter, strlen(f->filter) + 1);
403 f = f->next;
404 } while (f != j->filters);
405 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400406 for (b = j->bindings_head; b; b = b->next) {
407 marshal_append(state, b->src, strlen(b->src) + 1);
408 marshal_append(state, b->dest, strlen(b->dest) + 1);
409 marshal_append(state, (char *)&b->writeable, sizeof(b->writeable));
410 }
Will Drewryf89aef52011-09-16 16:48:57 -0500411}
412
Elly Jonese1749eb2011-10-07 13:54:59 -0400413size_t minijail_size(const struct minijail *j)
414{
415 struct marshal_state state;
416 marshal_state_init(&state, NULL, 0);
417 minijail_marshal_helper(&state, j);
418 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500419}
420
Elly Jonese1749eb2011-10-07 13:54:59 -0400421int minijail_marshal(const struct minijail *j, char *buf, size_t available)
422{
423 struct marshal_state state;
424 marshal_state_init(&state, buf, available);
425 minijail_marshal_helper(&state, j);
426 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500427}
428
Elly Jones51a5b6c2011-10-12 19:09:26 -0400429/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
430 * @length Number of bytes to consume
431 * @buf Buffer to consume from
432 * @buflength Size of @buf
433 *
434 * Returns a pointer to the base of the bytes, or NULL for errors.
435 */
436static void *consumebytes(size_t length, char **buf, size_t *buflength) {
437 char *p = *buf;
438 if (length > *buflength)
439 return NULL;
440 *buf += length;
441 *buflength -= length;
442 return p;
443}
444
445/* consumestr: consumes a C string from a buffer @buf of length @length
446 * @buf Buffer to consume
447 * @length Length of buffer
448 *
449 * Returns a pointer to the base of the string, or NULL for errors.
450 */
451static char *consumestr(char **buf, size_t *buflength) {
452 size_t len = strnlen(*buf, *buflength);
453 if (len == *buflength)
454 /* There's no null-terminator */
455 return NULL;
456 return consumebytes(len + 1, buf, buflength);
457}
458
Elly Jonese1749eb2011-10-07 13:54:59 -0400459int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
460{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400461 int i;
462 int count;
Elly Jonese1749eb2011-10-07 13:54:59 -0400463 if (length < sizeof(*j))
464 return -EINVAL;
465 memcpy((void *)j, serialized, sizeof(*j));
466 serialized += sizeof(*j);
467 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500468
Elly Jonese1749eb2011-10-07 13:54:59 -0400469 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400470 char *user = consumestr(&serialized, &length);
471 if (!user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400472 return -EINVAL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400473 j->user = strdup(user);
Elly Jonese1749eb2011-10-07 13:54:59 -0400474 }
Will Drewryf89aef52011-09-16 16:48:57 -0500475
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400476 if (j->chrootdir) { /* stale pointer */
477 char *chrootdir = consumestr(&serialized, &length);
478 if (!chrootdir)
479 return -EINVAL;
480 j->chrootdir = strdup(chrootdir);
481 }
482
Elly Jonese1749eb2011-10-07 13:54:59 -0400483 if (j->flags.seccomp_filter && j->filter_count) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400484 count = j->filter_count;
Elly Jonese1749eb2011-10-07 13:54:59 -0400485 /* Let add_seccomp_filter recompute the value. */
486 j->filter_count = 0;
487 j->filters = NULL; /* Don't follow the stale pointer. */
488 for (; count > 0; --count) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400489 int *nr = (int *)consumebytes(sizeof(*nr), &serialized,
490 &length);
Elly Jonese1749eb2011-10-07 13:54:59 -0400491 char *filter;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400492 if (!nr)
Elly Jonese1749eb2011-10-07 13:54:59 -0400493 return -EINVAL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400494 filter = consumestr(&serialized, &length);
495 if (!filter)
Elly Jonese1749eb2011-10-07 13:54:59 -0400496 return -EINVAL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400497 if (minijail_add_seccomp_filter(j, *nr, filter))
498 return -EINVAL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400499 }
500 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400501
502 count = j->binding_count;
503 j->bindings_head = NULL;
504 j->bindings_tail = NULL;
505 j->binding_count = 0;
506 for (i = 0; i < count; ++i) {
507 int *writeable;
508 const char *dest;
509 const char *src = consumestr(&serialized, &length);
510 if (!src)
511 return -EINVAL;
512 dest = consumestr(&serialized, &length);
513 if (!dest)
514 return -EINVAL;
515 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
516 if (!writeable)
517 return -EINVAL;
518 if (minijail_bind(j, src, dest, *writeable))
519 return -EINVAL;
520 }
521
Elly Jonese1749eb2011-10-07 13:54:59 -0400522 return 0;
Will Drewry2ddaad02011-09-16 11:36:08 -0500523}
524
Elly Jonese1749eb2011-10-07 13:54:59 -0400525void minijail_preenter(struct minijail *j)
526{
527 /* Strip out options which are minijail_run() only. */
528 j->flags.vfs = 0;
529 j->flags.readonly = 0;
530 j->flags.pids = 0;
Will Drewryfe4a3722011-09-16 14:50:50 -0500531}
532
Elly Jonese1749eb2011-10-07 13:54:59 -0400533void minijail_preexec(struct minijail *j)
534{
535 int vfs = j->flags.vfs;
536 int readonly = j->flags.readonly;
537 if (j->user)
538 free(j->user);
539 j->user = NULL;
540 memset(&j->flags, 0, sizeof(j->flags));
541 /* Now restore anything we meant to keep. */
542 j->flags.vfs = vfs;
543 j->flags.readonly = readonly;
544 /* Note, pidns will already have been used before this call. */
Will Drewry2ddaad02011-09-16 11:36:08 -0500545}
546
Elly Jones51a5b6c2011-10-12 19:09:26 -0400547/* bind_one: Applies bindings from @b for @j, recursing as needed.
548 * @j Minijail these bindings are for
549 * @b Head of list of bindings
550 *
551 * Returns 0 for success.
552 */
553static int bind_one(const struct minijail *j, struct binding *b) {
554 int ret = 0;
555 char *dest = NULL;
556 int mflags = MS_BIND | (b->writeable ? 0 : MS_RDONLY);
557 if (ret)
558 return ret;
559 /* dest has a leading "/" */
560 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
561 return -ENOMEM;
562 ret = mount(b->src, dest, NULL, mflags, NULL);
563 if (ret)
564 pdie("bind: %s -> %s", b->src, dest);
565 free(dest);
566 if (b->next)
567 return bind_one(j, b->next);
568 return ret;
569}
570
571static int enter_chroot(const struct minijail *j) {
572 int ret;
573 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
574 return ret;
575
576 if (chroot(j->chrootdir))
577 return -errno;
578
579 if (chdir("/"))
580 return -errno;
581
582 return 0;
583}
584
585
586
Elly Jonese1749eb2011-10-07 13:54:59 -0400587static int remount_readonly(void)
588{
589 const char *kProcPath = "/proc";
590 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
591 /* Right now, we're holding a reference to our parent's old mount of
592 * /proc in our namespace, which means using MS_REMOUNT here would
593 * mutate our parent's mount as well, even though we're in a VFS
594 * namespace (!). Instead, remove their mount from our namespace
595 * and make our own.
596 */
597 if (umount(kProcPath))
598 return -errno;
599 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
600 return -errno;
601 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400602}
603
Elly Jonese1749eb2011-10-07 13:54:59 -0400604static void drop_caps(const struct minijail *j)
605{
606 cap_t caps = cap_get_proc();
607 cap_value_t raise_flag[1];
608 unsigned int i;
609 if (!caps)
610 die("can't get process caps");
611 if (cap_clear_flag(caps, CAP_INHERITABLE))
612 die("can't clear inheritable caps");
613 if (cap_clear_flag(caps, CAP_EFFECTIVE))
614 die("can't clear effective caps");
615 if (cap_clear_flag(caps, CAP_PERMITTED))
616 die("can't clear permitted caps");
617 for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) {
618 if (i != CAP_SETPCAP && !(j->caps & (1 << i)))
619 continue;
620 raise_flag[0] = i;
621 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, raise_flag, CAP_SET))
622 die("can't add effective cap");
623 if (cap_set_flag(caps, CAP_PERMITTED, 1, raise_flag, CAP_SET))
624 die("can't add permitted cap");
625 if (cap_set_flag(caps, CAP_INHERITABLE, 1, raise_flag, CAP_SET))
626 die("can't add inheritable cap");
627 }
628 if (cap_set_proc(caps))
629 die("can't apply cleaned capset");
630 cap_free(caps);
631 for (i = 0; i < sizeof(j->caps) * 8 && cap_valid((int)i); ++i) {
632 if (j->caps & (1 << i))
633 continue;
634 if (prctl(PR_CAPBSET_DROP, i))
635 pdie("prctl(PR_CAPBSET_DROP)");
636 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400637}
638
Elly Jonese1749eb2011-10-07 13:54:59 -0400639static int setup_seccomp_filters(const struct minijail *j)
640{
641 const struct seccomp_filter *sf = j->filters;
642 int ret = 0;
643 int broaden = 0;
Will Drewry32ac9f52011-08-18 21:36:27 -0500644
Elly Jonese1749eb2011-10-07 13:54:59 -0400645 /* No filters installed isn't necessarily an error. */
646 if (!sf)
647 return ret;
Will Drewry32ac9f52011-08-18 21:36:27 -0500648
Elly Jonese1749eb2011-10-07 13:54:59 -0400649 do {
650 errno = 0;
651 ret = prctl(PR_SET_SECCOMP_FILTER, PR_SECCOMP_FILTER_SYSCALL,
652 sf->nr, broaden ? "1" : sf->filter);
653 if (ret) {
654 switch (errno) {
655 case ENOSYS:
656 /* TODO(wad) make this a config option */
657 if (broaden)
658 die("CONFIG_SECCOMP_FILTER is not"
659 "supported by your kernel");
660 warn("missing CONFIG_FTRACE_SYSCALLS; relaxing"
661 "the filter for %d", sf->nr);
662 broaden = 1;
663 continue;
664 case E2BIG:
665 warn("seccomp filter too long: %d", sf->nr);
666 pdie("filter too long");
667 case ENOSPC:
668 pdie("too many seccomp filters");
669 case EPERM:
670 warn("syscall filter disallowed for %d",
671 sf->nr);
672 pdie("failed to install seccomp filter");
673 case EINVAL:
674 warn("seccomp filter or call method is"
675 " invalid. %d:'%s'", sf->nr, sf->filter);
676 default:
677 pdie("failed to install seccomp filter");
678 }
679 }
680 sf = sf->next;
681 broaden = 0;
682 } while (sf != j->filters);
683 return ret;
Will Drewry32ac9f52011-08-18 21:36:27 -0500684}
685
Elly Jonese1749eb2011-10-07 13:54:59 -0400686void minijail_enter(const struct minijail *j)
687{
688 if (j->flags.pids)
689 die("tried to enter a pid-namespaced jail;"
690 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400691
Elly Jonese1749eb2011-10-07 13:54:59 -0400692 if (j->flags.seccomp_filter && setup_seccomp_filters(j))
693 pdie("failed to configure seccomp filters");
Will Drewry32ac9f52011-08-18 21:36:27 -0500694
Elly Jonese1749eb2011-10-07 13:54:59 -0400695 if (j->flags.usergroups && !j->user)
696 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400697
Elly Jonese1749eb2011-10-07 13:54:59 -0400698 /* We can't recover from failures if we've dropped privileges partially,
699 * so we don't even try. If any of our operations fail, we abort() the
700 * entire process.
701 */
702 if (j->flags.vfs && unshare(CLONE_NEWNS))
703 pdie("unshare");
Elly Jonescd7a9042011-07-22 13:56:51 -0400704
Elly Jones51a5b6c2011-10-12 19:09:26 -0400705 if (j->flags.chroot && enter_chroot(j))
706 pdie("chroot");
707
Elly Jonese1749eb2011-10-07 13:54:59 -0400708 if (j->flags.readonly && remount_readonly())
709 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400710
Elly Jonese1749eb2011-10-07 13:54:59 -0400711 if (j->flags.caps) {
712 /* POSIX capabilities are a bit tricky. If we drop our
713 * capability to change uids, our attempt to use setuid()
714 * below will fail. Hang on to root caps across setuid(), then
715 * lock securebits.
716 */
717 if (prctl(PR_SET_KEEPCAPS, 1))
718 pdie("prctl(PR_SET_KEEPCAPS)");
719 if (prctl
720 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
721 pdie("prctl(PR_SET_SECUREBITS)");
722 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400723
Elly Jonese1749eb2011-10-07 13:54:59 -0400724 if (j->flags.usergroups) {
725 if (initgroups(j->user, j->usergid))
726 pdie("initgroups");
727 } else {
728 /* Only attempt to clear supplemental groups if we are changing
729 * users. */
730 if ((j->uid || j->gid) && setgroups(0, NULL))
731 pdie("setgroups");
732 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400733
Elly Jonese1749eb2011-10-07 13:54:59 -0400734 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
735 pdie("setresgid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400736
Elly Jonese1749eb2011-10-07 13:54:59 -0400737 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
738 pdie("setresuid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400739
Elly Jonese1749eb2011-10-07 13:54:59 -0400740 if (j->flags.caps)
741 drop_caps(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400742
Elly Jonese1749eb2011-10-07 13:54:59 -0400743 /* seccomp has to come last since it cuts off all the other
744 * privilege-dropping syscalls :)
745 */
746 if (j->flags.seccomp_filter && prctl(PR_SET_SECCOMP, 13))
747 pdie("prctl(PR_SET_SECCOMP, 13)");
Will Drewry32ac9f52011-08-18 21:36:27 -0500748
Elly Jonese1749eb2011-10-07 13:54:59 -0400749 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
750 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400751}
752
753static int init_exitstatus = 0;
754
Elly Jonese1749eb2011-10-07 13:54:59 -0400755static void init_term(int __attribute__ ((unused)) sig)
756{
757 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400758}
759
Elly Jonese1749eb2011-10-07 13:54:59 -0400760static int init(pid_t rootpid)
761{
762 pid_t pid;
763 int status;
764 /* so that we exit with the right status */
765 signal(SIGTERM, init_term);
766 /* TODO(wad) self jail with seccomp_filters here. */
767 while ((pid = wait(&status)) > 0) {
768 /* This loop will only end when either there are no processes
769 * left inside our pid namespace or we get a signal.
770 */
771 if (pid == rootpid)
772 init_exitstatus = status;
773 }
774 if (!WIFEXITED(init_exitstatus))
775 _exit(MINIJAIL_ERR_INIT);
776 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400777}
778
Elly Jonese1749eb2011-10-07 13:54:59 -0400779int minijail_from_fd(int fd, struct minijail *j)
780{
781 size_t sz = 0;
782 size_t bytes = read(fd, &sz, sizeof(sz));
783 char *buf;
784 int r;
785 if (sizeof(sz) != bytes)
786 return -EINVAL;
787 if (sz > USHRT_MAX) /* Arbitrary sanity check */
788 return -E2BIG;
789 buf = malloc(sz);
790 if (!buf)
791 return -ENOMEM;
792 bytes = read(fd, buf, sz);
793 if (bytes != sz) {
794 free(buf);
795 return -EINVAL;
796 }
797 r = minijail_unmarshal(j, buf, sz);
798 free(buf);
799 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500800}
801
Elly Jonese1749eb2011-10-07 13:54:59 -0400802int minijail_to_fd(struct minijail *j, int fd)
803{
804 char *buf;
805 size_t sz = minijail_size(j);
806 ssize_t written;
807 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400808
Elly Jonese1749eb2011-10-07 13:54:59 -0400809 if (!sz)
810 return -EINVAL;
811 buf = malloc(sz);
812 r = minijail_marshal(j, buf, sz);
813 if (r) {
814 free(buf);
815 return r;
816 }
817 /* Sends [size][minijail]. */
818 written = write(fd, &sz, sizeof(sz));
819 if (written != sizeof(sz)) {
820 free(buf);
821 return -EFAULT;
822 }
823 written = write(fd, buf, sz);
824 if (written < 0 || (size_t) written != sz) {
825 free(buf);
826 return -EFAULT;
827 }
828 free(buf);
829 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500830}
Elly Jonescd7a9042011-07-22 13:56:51 -0400831
Elly Jonese1749eb2011-10-07 13:54:59 -0400832static int setup_preload(void)
833{
834 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
835 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
836 if (!newenv)
837 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400838
Elly Jonese1749eb2011-10-07 13:54:59 -0400839 /* Only insert a separating space if we have something to separate... */
840 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
841 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400842
Elly Jonese1749eb2011-10-07 13:54:59 -0400843 /* setenv() makes a copy of the string we give it */
844 setenv(kLdPreloadEnvVar, newenv, 1);
845 free(newenv);
846 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400847}
848
Elly Jonese1749eb2011-10-07 13:54:59 -0400849static int setup_pipe(int fds[2])
850{
851 int r = pipe(fds);
852 char fd_buf[11];
853 if (r)
854 return r;
855 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
856 if (r <= 0)
857 return -EINVAL;
858 setenv(kFdEnvVar, fd_buf, 1);
859 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500860}
861
Elly Jonese1749eb2011-10-07 13:54:59 -0400862int minijail_run(struct minijail *j, const char *filename, char *const argv[])
863{
864 unsigned int pidns = j->flags.pids ? CLONE_NEWPID : 0;
865 char *oldenv, *oldenv_copy = NULL;
866 pid_t child_pid;
867 int pipe_fds[2];
868 int ret;
Ben Chan541c7e52011-08-26 14:55:53 -0700869
Elly Jonese1749eb2011-10-07 13:54:59 -0400870 oldenv = getenv(kLdPreloadEnvVar);
871 if (oldenv) {
872 oldenv_copy = strdup(oldenv);
873 if (!oldenv_copy)
874 return -ENOMEM;
875 }
Will Drewryf89aef52011-09-16 16:48:57 -0500876
Elly Jonese1749eb2011-10-07 13:54:59 -0400877 if (setup_preload())
878 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500879
Elly Jonese1749eb2011-10-07 13:54:59 -0400880 /* Before we fork(2) and execve(2) the child process, we need to open
881 * a pipe(2) to send the minijail configuration over.
882 */
883 if (setup_pipe(pipe_fds))
884 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -0400885
Elly Jonese1749eb2011-10-07 13:54:59 -0400886 child_pid = syscall(SYS_clone, pidns | SIGCHLD, NULL);
887 if (child_pid < 0) {
888 free(oldenv_copy);
889 return child_pid;
890 }
Will Drewryf89aef52011-09-16 16:48:57 -0500891
Elly Jonese1749eb2011-10-07 13:54:59 -0400892 if (child_pid) {
893 /* Restore parent's LD_PRELOAD. */
894 if (oldenv_copy) {
895 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
896 free(oldenv_copy);
897 } else {
898 unsetenv(kLdPreloadEnvVar);
899 }
900 unsetenv(kFdEnvVar);
901 j->initpid = child_pid;
902 close(pipe_fds[0]); /* read endpoint */
903 ret = minijail_to_fd(j, pipe_fds[1]);
904 close(pipe_fds[1]); /* write endpoint */
905 if (ret) {
906 kill(j->initpid, SIGKILL);
907 die("failed to send marshalled minijail");
908 }
909 return 0;
910 }
911 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -0700912
Elly Jonese1749eb2011-10-07 13:54:59 -0400913 /* Drop everything that cannot be inherited across execve. */
914 minijail_preexec(j);
915 /* Jail this process and its descendants... */
916 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400917
Elly Jonese1749eb2011-10-07 13:54:59 -0400918 if (pidns) {
919 /* pid namespace: this process will become init inside the new
920 * namespace, so fork off a child to actually run the program
921 * (we don't want all programs we might exec to have to know
922 * how to be init).
923 */
924 child_pid = fork();
925 if (child_pid < 0)
926 _exit(child_pid);
927 else if (child_pid > 0)
928 init(child_pid); /* never returns */
929 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400930
Elly Jonese1749eb2011-10-07 13:54:59 -0400931 /* If we aren't pid-namespaced:
932 * calling process
933 * -> execve()-ing process
934 * If we are:
935 * calling process
936 * -> init()-ing process
937 * -> execve()-ing process
938 */
939 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -0400940}
941
Elly Jonese1749eb2011-10-07 13:54:59 -0400942int minijail_kill(struct minijail *j)
943{
944 int st;
945 if (kill(j->initpid, SIGTERM))
946 return -errno;
947 if (waitpid(j->initpid, &st, 0) < 0)
948 return -errno;
949 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -0400950}
951
Elly Jonese1749eb2011-10-07 13:54:59 -0400952int minijail_wait(struct minijail *j)
953{
954 int st;
955 if (waitpid(j->initpid, &st, 0) < 0)
956 return -errno;
957 if (!WIFEXITED(st))
958 return MINIJAIL_ERR_JAIL;
959 return WEXITSTATUS(st);
Elly Jonescd7a9042011-07-22 13:56:51 -0400960}
961
Elly Jonese1749eb2011-10-07 13:54:59 -0400962void minijail_destroy(struct minijail *j)
963{
964 struct seccomp_filter *f = j->filters;
965 /* Unlink the tail and head */
966 if (f)
967 f->prev->next = NULL;
968 while (f) {
969 struct seccomp_filter *next = f->next;
970 free(f->filter);
971 free(f);
972 f = next;
973 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400974 while (j->bindings_head) {
975 struct binding *b = j->bindings_head;
976 j->bindings_head = j->bindings_head->next;
977 free(b->dest);
978 free(b->src);
979 free(b);
980 }
981 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400982 if (j->user)
983 free(j->user);
984 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400985}