blob: 5d6d7c442ecf799681d4f9fa727348bdf59b5d20 [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
Will Drewry6ac91122011-10-21 16:38:58 -050096struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -040097{
Elly Jones51a5b6c2011-10-12 19:09:26 -040098 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -040099}
100
Will Drewry6ac91122011-10-21 16:38:58 -0500101void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400102{
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
Will Drewry6ac91122011-10-21 16:38:58 -0500109void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400110{
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
Will Drewry6ac91122011-10-21 16:38:58 -0500117int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400118{
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
Will Drewry6ac91122011-10-21 16:38:58 -0500144int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400145{
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
Will Drewry6ac91122011-10-21 16:38:58 -0500167void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400168{
169 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400170}
171
Will Drewry6ac91122011-10-21 16:38:58 -0500172void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400173{
174 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500175}
176
Will Drewry6ac91122011-10-21 16:38:58 -0500177void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400178{
179 j->caps = capmask;
180 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400181}
182
Will Drewry6ac91122011-10-21 16:38:58 -0500183void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400184{
185 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400186}
187
Will Drewry6ac91122011-10-21 16:38:58 -0500188void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400189{
190 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400191}
192
Will Drewry6ac91122011-10-21 16:38:58 -0500193void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400194{
195 j->flags.vfs = 1;
196 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400197}
198
Will Drewry6ac91122011-10-21 16:38:58 -0500199void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400200{
201 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400202}
203
Will Drewry6ac91122011-10-21 16:38:58 -0500204void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400205{
206 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400207}
208
Will Drewry6ac91122011-10-21 16:38:58 -0500209int API minijail_enter_chroot(struct minijail *j, const char *dir) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400210 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 Drewry6ac91122011-10-21 16:38:58 -0500219int API minijail_bind(struct minijail *j, const char *src, const char *dest,
220 int writeable) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400221 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
Will Drewry6ac91122011-10-21 16:38:58 -0500259int API minijail_add_seccomp_filter(struct minijail *j, int nr,
260 const char *filter)
Elly Jonese1749eb2011-10-07 13:54:59 -0400261{
262 struct seccomp_filter *sf;
263 if (!filter || nr < 0)
264 return -EINVAL;
Will Drewry32ac9f52011-08-18 21:36:27 -0500265
Elly Jonese1749eb2011-10-07 13:54:59 -0400266 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 Drewry32ac9f52011-08-18 21:36:27 -0500275
Elly Jonese1749eb2011-10-07 13:54:59 -0400276 j->filter_count++;
Will Drewryf89aef52011-09-16 16:48:57 -0500277
Elly Jonese1749eb2011-10-07 13:54:59 -0400278 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 Drewry32ac9f52011-08-18 21:36:27 -0500289}
290
Will Drewry6ac91122011-10-21 16:38:58 -0500291int API minijail_lookup_syscall(const char *name)
Elly Jonese1749eb2011-10-07 13:54:59 -0400292{
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 Drewry32ac9f52011-08-18 21:36:27 -0500298}
299
Will Drewry6ac91122011-10-21 16:38:58 -0500300char *strip(char *s)
Elly Jonese1749eb2011-10-07 13:54:59 -0400301{
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 Drewry32ac9f52011-08-18 21:36:27 -0500310}
311
Will Drewry6ac91122011-10-21 16:38:58 -0500312void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400313{
314 FILE *file = fopen(path, "r");
315 char line[MINIJAIL_MAX_SECCOMP_FILTER_LINE];
Ben Chan1d697932011-10-14 10:53:32 -0700316 int count = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -0400317 if (!file)
318 pdie("failed to open seccomp filters file");
Will Drewry32ac9f52011-08-18 21:36:27 -0500319
Elly Jonese1749eb2011-10-07 13:54:59 -0400320 /* 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 Chan1d697932011-10-14 10:53:32 -0700330 count++;
Will Drewry32ac9f52011-08-18 21:36:27 -0500331
Ben Chan1d697932011-10-14 10:53:32 -0700332 /* Allow comment lines */
333 if (*name == '#')
334 continue;
Will Drewry32ac9f52011-08-18 21:36:27 -0500335
Elly Jonese1749eb2011-10-07 13:54:59 -0400336 name = strip(name);
Will Drewry32ac9f52011-08-18 21:36:27 -0500337
Elly Jonese1749eb2011-10-07 13:54:59 -0400338 if (!filter) {
339 if (strlen(name))
340 die("invalid filter on line %d", count);
341 /* Allow empty lines */
342 continue;
343 }
Will Drewry32ac9f52011-08-18 21:36:27 -0500344
Elly Jonese1749eb2011-10-07 13:54:59 -0400345 filter = strip(filter);
Will Drewry32ac9f52011-08-18 21:36:27 -0500346
Elly Jonese1749eb2011-10-07 13:54:59 -0400347 /* 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 Drewry32ac9f52011-08-18 21:36:27 -0500354
Elly Jonese1749eb2011-10-07 13:54:59 -0400355 if (minijail_add_seccomp_filter(j, nr, filter))
356 pdie("failed to add filter for syscall '%s'", name);
357 }
358 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500359}
360
Will Drewryf89aef52011-09-16 16:48:57 -0500361struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400362 size_t available;
363 size_t total;
364 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500365};
366
Will Drewry6ac91122011-10-21 16:38:58 -0500367void marshal_state_init(struct marshal_state *state,
368 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400369{
370 state->available = available;
371 state->buf = buf;
372 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500373}
374
Will Drewry6ac91122011-10-21 16:38:58 -0500375void marshal_append(struct marshal_state *state,
376 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400377{
378 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500379
Elly Jonese1749eb2011-10-07 13:54:59 -0400380 /* 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 Drewryf89aef52011-09-16 16:48:57 -0500388}
389
Will Drewry6ac91122011-10-21 16:38:58 -0500390void minijail_marshal_helper(struct marshal_state *state,
391 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400392{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400393 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400394 marshal_append(state, (char *)j, sizeof(*j));
395 if (j->user)
396 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400397 if (j->chrootdir)
398 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Elly Jonese1749eb2011-10-07 13:54:59 -0400399 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 Jones51a5b6c2011-10-12 19:09:26 -0400407 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 Drewryf89aef52011-09-16 16:48:57 -0500412}
413
Will Drewry6ac91122011-10-21 16:38:58 -0500414size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400415{
416 struct marshal_state state;
417 marshal_state_init(&state, NULL, 0);
418 minijail_marshal_helper(&state, j);
419 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500420}
421
Elly Jonese1749eb2011-10-07 13:54:59 -0400422int 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 Drewry2ddaad02011-09-16 11:36:08 -0500428}
429
Elly Jones51a5b6c2011-10-12 19:09:26 -0400430/* 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 Drewry6ac91122011-10-21 16:38:58 -0500437void *consumebytes(size_t length, char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400438 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 Drewry6ac91122011-10-21 16:38:58 -0500452char *consumestr(char **buf, size_t *buflength) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400453 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 Jonese1749eb2011-10-07 13:54:59 -0400460int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
461{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400462 int i;
463 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500464 int ret = -EINVAL;
465
Elly Jonese1749eb2011-10-07 13:54:59 -0400466 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500467 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400468 memcpy((void *)j, serialized, sizeof(*j));
469 serialized += sizeof(*j);
470 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500471
Will Drewrybee7ba72011-10-21 20:47:01 -0500472 /* Potentially stale pointers not used as signals. */
473 j->bindings_head = NULL;
474 j->bindings_tail = NULL;
475 j->filters = NULL;
476
Elly Jonese1749eb2011-10-07 13:54:59 -0400477 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400478 char *user = consumestr(&serialized, &length);
479 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500480 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400481 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500482 if (!j->user)
483 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400484 }
Will Drewryf89aef52011-09-16 16:48:57 -0500485
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400486 if (j->chrootdir) { /* stale pointer */
487 char *chrootdir = consumestr(&serialized, &length);
488 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500489 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400490 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500491 if (!j->chrootdir)
492 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400493 }
494
Elly Jonese1749eb2011-10-07 13:54:59 -0400495 if (j->flags.seccomp_filter && j->filter_count) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400496 count = j->filter_count;
Elly Jonese1749eb2011-10-07 13:54:59 -0400497 /* Let add_seccomp_filter recompute the value. */
498 j->filter_count = 0;
Elly Jonese1749eb2011-10-07 13:54:59 -0400499 for (; count > 0; --count) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400500 int *nr = (int *)consumebytes(sizeof(*nr), &serialized,
501 &length);
Elly Jonese1749eb2011-10-07 13:54:59 -0400502 char *filter;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400503 if (!nr)
Will Drewrybee7ba72011-10-21 20:47:01 -0500504 goto bad_filters;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400505 filter = consumestr(&serialized, &length);
506 if (!filter)
Will Drewrybee7ba72011-10-21 20:47:01 -0500507 goto bad_filters;
Elly Jonese1749eb2011-10-07 13:54:59 -0400508 if (minijail_add_seccomp_filter(j, *nr, filter))
Will Drewrybee7ba72011-10-21 20:47:01 -0500509 goto bad_filters;
Elly Jonese1749eb2011-10-07 13:54:59 -0400510 }
511 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512
513 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400514 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 Drewrybee7ba72011-10-21 20:47:01 -0500520 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400521 dest = consumestr(&serialized, &length);
522 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500523 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400524 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
525 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500526 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400527 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500528 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400529 }
530
Elly Jonese1749eb2011-10-07 13:54:59 -0400531 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500532
533bad_bindings:
534bad_filters:
535 if (j->chrootdir)
536 free(j->chrootdir);
537bad_chrootdir:
538 if (j->user)
539 free(j->user);
540clear_pointers:
541 j->user = NULL;
542 j->chrootdir = NULL;
543out:
544 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500545}
546
Elly Jonese1749eb2011-10-07 13:54:59 -0400547void 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 Drewryfe4a3722011-09-16 14:50:50 -0500553}
554
Elly Jonese1749eb2011-10-07 13:54:59 -0400555void 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 Drewry2ddaad02011-09-16 11:36:08 -0500567}
568
Elly Jones51a5b6c2011-10-12 19:09:26 -0400569/* 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 Drewry6ac91122011-10-21 16:38:58 -0500575int bind_one(const struct minijail *j, struct binding *b) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400576 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 Drewry6ac91122011-10-21 16:38:58 -0500593int enter_chroot(const struct minijail *j) {
Elly Jones51a5b6c2011-10-12 19:09:26 -0400594 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 Drewry6ac91122011-10-21 16:38:58 -0500607int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400608{
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 Jonescd7a9042011-07-22 13:56:51 -0400622}
623
Will Drewry6ac91122011-10-21 16:38:58 -0500624void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400625{
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 Jonescd7a9042011-07-22 13:56:51 -0400657}
658
Will Drewry6ac91122011-10-21 16:38:58 -0500659int setup_seccomp_filters(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400660{
661 const struct seccomp_filter *sf = j->filters;
662 int ret = 0;
663 int broaden = 0;
Will Drewry32ac9f52011-08-18 21:36:27 -0500664
Elly Jonese1749eb2011-10-07 13:54:59 -0400665 /* No filters installed isn't necessarily an error. */
666 if (!sf)
667 return ret;
Will Drewry32ac9f52011-08-18 21:36:27 -0500668
Elly Jonese1749eb2011-10-07 13:54:59 -0400669 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 Drewry32ac9f52011-08-18 21:36:27 -0500704}
705
Will Drewry6ac91122011-10-21 16:38:58 -0500706void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400707{
708 if (j->flags.pids)
709 die("tried to enter a pid-namespaced jail;"
710 "try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400711
Elly Jonese1749eb2011-10-07 13:54:59 -0400712 if (j->flags.seccomp_filter && setup_seccomp_filters(j))
713 pdie("failed to configure seccomp filters");
Will Drewry32ac9f52011-08-18 21:36:27 -0500714
Elly Jonese1749eb2011-10-07 13:54:59 -0400715 if (j->flags.usergroups && !j->user)
716 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400717
Elly Jonese1749eb2011-10-07 13:54:59 -0400718 /* 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 Jonescd7a9042011-07-22 13:56:51 -0400724
Elly Jones51a5b6c2011-10-12 19:09:26 -0400725 if (j->flags.chroot && enter_chroot(j))
726 pdie("chroot");
727
Elly Jonese1749eb2011-10-07 13:54:59 -0400728 if (j->flags.readonly && remount_readonly())
729 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400730
Elly Jonese1749eb2011-10-07 13:54:59 -0400731 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 Jonescd7a9042011-07-22 13:56:51 -0400743
Elly Jonese1749eb2011-10-07 13:54:59 -0400744 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 Jonescd7a9042011-07-22 13:56:51 -0400753
Elly Jonese1749eb2011-10-07 13:54:59 -0400754 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
755 pdie("setresgid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400756
Elly Jonese1749eb2011-10-07 13:54:59 -0400757 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
758 pdie("setresuid");
Elly Jonescd7a9042011-07-22 13:56:51 -0400759
Elly Jonese1749eb2011-10-07 13:54:59 -0400760 if (j->flags.caps)
761 drop_caps(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400762
Elly Jonese1749eb2011-10-07 13:54:59 -0400763 /* 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 Drewry32ac9f52011-08-18 21:36:27 -0500768
Elly Jonese1749eb2011-10-07 13:54:59 -0400769 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1))
770 pdie("prctl(PR_SET_SECCOMP)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400771}
772
Will Drewry6ac91122011-10-21 16:38:58 -0500773/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400774static int init_exitstatus = 0;
775
Will Drewry6ac91122011-10-21 16:38:58 -0500776void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400777{
778 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400779}
780
Will Drewry6ac91122011-10-21 16:38:58 -0500781int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400782{
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 Jonescd7a9042011-07-22 13:56:51 -0400798}
799
Will Drewry6ac91122011-10-21 16:38:58 -0500800int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400801{
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 Drewry2f54b6a2011-09-16 13:45:31 -0500821}
822
Will Drewry6ac91122011-10-21 16:38:58 -0500823int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400824{
825 char *buf;
826 size_t sz = minijail_size(j);
827 ssize_t written;
828 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400829
Elly Jonese1749eb2011-10-07 13:54:59 -0400830 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 Drewry2f54b6a2011-09-16 13:45:31 -0500851}
Elly Jonescd7a9042011-07-22 13:56:51 -0400852
Will Drewry6ac91122011-10-21 16:38:58 -0500853int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400854{
855 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
856 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
857 if (!newenv)
858 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400859
Elly Jonese1749eb2011-10-07 13:54:59 -0400860 /* Only insert a separating space if we have something to separate... */
861 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
862 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400863
Elly Jonese1749eb2011-10-07 13:54:59 -0400864 /* setenv() makes a copy of the string we give it */
865 setenv(kLdPreloadEnvVar, newenv, 1);
866 free(newenv);
867 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400868}
869
Will Drewry6ac91122011-10-21 16:38:58 -0500870int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400871{
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 Drewryf89aef52011-09-16 16:48:57 -0500881}
882
Will Drewry6ac91122011-10-21 16:38:58 -0500883int API minijail_run(struct minijail *j, const char *filename,
884 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -0400885{
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 Chan541c7e52011-08-26 14:55:53 -0700891
Elly Jonese1749eb2011-10-07 13:54:59 -0400892 oldenv = getenv(kLdPreloadEnvVar);
893 if (oldenv) {
894 oldenv_copy = strdup(oldenv);
895 if (!oldenv_copy)
896 return -ENOMEM;
897 }
Will Drewryf89aef52011-09-16 16:48:57 -0500898
Elly Jonese1749eb2011-10-07 13:54:59 -0400899 if (setup_preload())
900 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500901
Elly Jonese1749eb2011-10-07 13:54:59 -0400902 /* 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 Jonescd7a9042011-07-22 13:56:51 -0400907
Elly Jonese1749eb2011-10-07 13:54:59 -0400908 child_pid = syscall(SYS_clone, pidns | SIGCHLD, NULL);
909 if (child_pid < 0) {
910 free(oldenv_copy);
911 return child_pid;
912 }
Will Drewryf89aef52011-09-16 16:48:57 -0500913
Elly Jonese1749eb2011-10-07 13:54:59 -0400914 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 Chan541c7e52011-08-26 14:55:53 -0700934
Elly Jonese1749eb2011-10-07 13:54:59 -0400935 /* Drop everything that cannot be inherited across execve. */
936 minijail_preexec(j);
937 /* Jail this process and its descendants... */
938 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400939
Elly Jonese1749eb2011-10-07 13:54:59 -0400940 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 Jonescd7a9042011-07-22 13:56:51 -0400952
Elly Jonese1749eb2011-10-07 13:54:59 -0400953 /* 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 Jonescd7a9042011-07-22 13:56:51 -0400962}
963
Will Drewry6ac91122011-10-21 16:38:58 -0500964int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400965{
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 Jonescd7a9042011-07-22 13:56:51 -0400972}
973
Will Drewry6ac91122011-10-21 16:38:58 -0500974int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400975{
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 Jonescd7a9042011-07-22 13:56:51 -0400982}
983
Will Drewry6ac91122011-10-21 16:38:58 -0500984void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400985{
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 Jones51a5b6c2011-10-12 19:09:26 -0400996 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 Jonese1749eb2011-10-07 13:54:59 -04001004 if (j->user)
1005 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001006 if (j->chrootdir)
1007 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001008 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001009}