blob: 1c54a407138548c66bb596b016b8fe8d6c44bd5c [file] [log] [blame]
Jorge Lucangeli Obesd613ab22015-03-03 14:22:50 -08001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * 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
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07008
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08009#include <asm/unistd.h>
Will Drewry32ac9f52011-08-18 21:36:27 -050010#include <ctype.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040011#include <errno.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070012#include <fcntl.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040013#include <grp.h>
14#include <inttypes.h>
Will Drewryfe4a3722011-09-16 14:50:50 -050015#include <limits.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040016#include <linux/capability.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040017#include <pwd.h>
18#include <sched.h>
19#include <signal.h>
Will Drewry2f54b6a2011-09-16 13:45:31 -050020#include <stdarg.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080021#include <stddef.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <syscall.h>
26#include <sys/capability.h>
27#include <sys/mount.h>
Will Drewryf89aef52011-09-16 16:48:57 -050028#include <sys/param.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040029#include <sys/prctl.h>
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070030#include <sys/stat.h>
31#include <sys/types.h>
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080032#include <sys/user.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040033#include <sys/wait.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040034#include <unistd.h>
35
36#include "libminijail.h"
37#include "libminijail-private.h"
38
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070039#include "signal_handler.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080040#include "syscall_filter.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070041#include "util.h"
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080042
Lei Zhangeee31552012-10-17 21:27:10 -070043#ifdef HAVE_SECUREBITS_H
44#include <linux/securebits.h>
45#else
46#define SECURE_ALL_BITS 0x15
47#define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1)
48#endif
49
Will Drewry32ac9f52011-08-18 21:36:27 -050050/* Until these are reliably available in linux/prctl.h */
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080051#ifndef PR_SET_SECCOMP
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070052# define PR_SET_SECCOMP 22
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -080053#endif
54
55/* For seccomp_filter using BPF. */
56#ifndef PR_SET_NO_NEW_PRIVS
57# define PR_SET_NO_NEW_PRIVS 38
58#endif
59#ifndef SECCOMP_MODE_FILTER
60# define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */
Will Drewry32ac9f52011-08-18 21:36:27 -050061#endif
62
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -070063#ifdef USE_SECCOMP_SOFTFAIL
64# define SECCOMP_SOFTFAIL 1
65#else
66# define SECCOMP_SOFTFAIL 0
67#endif
68
Elly Jones51a5b6c2011-10-12 19:09:26 -040069struct binding {
70 char *src;
71 char *dest;
72 int writeable;
73 struct binding *next;
74};
75
Will Drewryf89aef52011-09-16 16:48:57 -050076struct minijail {
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070077 /*
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070078 * WARNING: if you add a flag here you need to make sure it's
79 * accounted for in minijail_pre{enter|exec}() below.
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -070080 */
Elly Jonese1749eb2011-10-07 13:54:59 -040081 struct {
82 int uid:1;
83 int gid:1;
84 int caps:1;
85 int vfs:1;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070086 int enter_vfs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040087 int pids:1;
Elly Fong-Jones6c086302013-03-20 17:15:28 -040088 int net:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040089 int seccomp:1;
90 int readonly:1;
91 int usergroups:1;
92 int ptrace:1;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070093 int no_new_privs:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040094 int seccomp_filter:1;
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070095 int log_seccomp_filter:1;
Elly Jones51a5b6c2011-10-12 19:09:26 -040096 int chroot:1;
Lee Campbell11af0622014-05-22 12:36:04 -070097 int mount_tmp:1;
Elly Jonese1749eb2011-10-07 13:54:59 -040098 } flags;
99 uid_t uid;
100 gid_t gid;
101 gid_t usergid;
102 char *user;
103 uint64_t caps;
104 pid_t initpid;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700105 int mountns_fd;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800106 int filter_len;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400107 int binding_count;
108 char *chrootdir;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800109 struct sock_fprog *filter_prog;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400110 struct binding *bindings_head;
111 struct binding *bindings_tail;
Will Drewryf89aef52011-09-16 16:48:57 -0500112};
113
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700114/*
115 * Strip out flags meant for the parent.
116 * We keep things that are not inherited across execve(2) (e.g. capabilities),
117 * or are easier to set after execve(2) (e.g. seccomp filters).
118 */
119void minijail_preenter(struct minijail *j)
120{
121 j->flags.vfs = 0;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700122 j->flags.enter_vfs = 0;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700123 j->flags.readonly = 0;
124 j->flags.pids = 0;
125}
126
127/*
128 * Strip out flags meant for the child.
129 * We keep things that are inherited across execve(2).
130 */
131void minijail_preexec(struct minijail *j)
132{
133 int vfs = j->flags.vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700134 int enter_vfs = j->flags.enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700135 int readonly = j->flags.readonly;
136 if (j->user)
137 free(j->user);
138 j->user = NULL;
139 memset(&j->flags, 0, sizeof(j->flags));
140 /* Now restore anything we meant to keep. */
141 j->flags.vfs = vfs;
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700142 j->flags.enter_vfs = enter_vfs;
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -0700143 j->flags.readonly = readonly;
144 /* Note, |pids| will already have been used before this call. */
145}
146
147/* Minijail API. */
148
Will Drewry6ac91122011-10-21 16:38:58 -0500149struct minijail API *minijail_new(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400150{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400151 return calloc(1, sizeof(struct minijail));
Elly Jonescd7a9042011-07-22 13:56:51 -0400152}
153
Will Drewry6ac91122011-10-21 16:38:58 -0500154void API minijail_change_uid(struct minijail *j, uid_t uid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400155{
156 if (uid == 0)
157 die("useless change to uid 0");
158 j->uid = uid;
159 j->flags.uid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400160}
161
Will Drewry6ac91122011-10-21 16:38:58 -0500162void API minijail_change_gid(struct minijail *j, gid_t gid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400163{
164 if (gid == 0)
165 die("useless change to gid 0");
166 j->gid = gid;
167 j->flags.gid = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400168}
169
Will Drewry6ac91122011-10-21 16:38:58 -0500170int API minijail_change_user(struct minijail *j, const char *user)
Elly Jonese1749eb2011-10-07 13:54:59 -0400171{
172 char *buf = NULL;
173 struct passwd pw;
174 struct passwd *ppw = NULL;
175 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
176 if (sz == -1)
177 sz = 65536; /* your guess is as good as mine... */
Elly Joneseb300c52011-09-22 14:35:43 -0400178
Elly Jonesdd3e8512012-01-23 15:13:38 -0500179 /*
180 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400181 * the maximum needed size of the buffer, so we don't have to search.
182 */
183 buf = malloc(sz);
184 if (!buf)
185 return -ENOMEM;
186 getpwnam_r(user, &pw, buf, sz, &ppw);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500187 /*
188 * We're safe to free the buffer here. The strings inside pw point
189 * inside buf, but we don't use any of them; this leaves the pointers
190 * dangling but it's safe. ppw points at pw if getpwnam_r succeeded.
191 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400192 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700193 /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */
Elly Jonese1749eb2011-10-07 13:54:59 -0400194 if (!ppw)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700195 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400196 minijail_change_uid(j, ppw->pw_uid);
197 j->user = strdup(user);
198 if (!j->user)
199 return -ENOMEM;
200 j->usergid = ppw->pw_gid;
201 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400202}
203
Will Drewry6ac91122011-10-21 16:38:58 -0500204int API minijail_change_group(struct minijail *j, const char *group)
Elly Jonese1749eb2011-10-07 13:54:59 -0400205{
Jorge Lucangeli Obes20ac2282015-07-18 17:53:12 +0000206 struct group *pgr = NULL;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700207
208#if defined(__BRILLO__)
209 /* Android does not implement getgrnam_r(). */
210 pgr = getgrnam(group);
211#else
212 struct group gr;
213 char *buf = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400214 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
215 if (sz == -1)
216 sz = 65536; /* and mine is as good as yours, really */
Elly Joneseb300c52011-09-22 14:35:43 -0400217
Elly Jonesdd3e8512012-01-23 15:13:38 -0500218 /*
219 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
Elly Jonese1749eb2011-10-07 13:54:59 -0400220 * the maximum needed size of the buffer, so we don't have to search.
221 */
222 buf = malloc(sz);
223 if (!buf)
224 return -ENOMEM;
225 getgrnam_r(group, &gr, buf, sz, &pgr);
Elly Jonesdd3e8512012-01-23 15:13:38 -0500226 /*
227 * We're safe to free the buffer here. The strings inside gr point
228 * inside buf, but we don't use any of them; this leaves the pointers
229 * dangling but it's safe. pgr points at gr if getgrnam_r succeeded.
230 */
Elly Jonese1749eb2011-10-07 13:54:59 -0400231 free(buf);
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700232 /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700233#endif
Elly Jonese1749eb2011-10-07 13:54:59 -0400234 if (!pgr)
Jorge Lucangeli Obes4e480652014-03-26 10:56:42 -0700235 return -1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400236 minijail_change_gid(j, pgr->gr_gid);
237 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400238}
239
Will Drewry6ac91122011-10-21 16:38:58 -0500240void API minijail_use_seccomp(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400241{
242 j->flags.seccomp = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400243}
244
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700245void API minijail_no_new_privs(struct minijail *j)
246{
247 j->flags.no_new_privs = 1;
248}
249
Will Drewry6ac91122011-10-21 16:38:58 -0500250void API minijail_use_seccomp_filter(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400251{
252 j->flags.seccomp_filter = 1;
Will Drewry32ac9f52011-08-18 21:36:27 -0500253}
254
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700255void API minijail_log_seccomp_filter_failures(struct minijail *j)
256{
257 j->flags.log_seccomp_filter = 1;
258}
259
Will Drewry6ac91122011-10-21 16:38:58 -0500260void API minijail_use_caps(struct minijail *j, uint64_t capmask)
Elly Jonese1749eb2011-10-07 13:54:59 -0400261{
262 j->caps = capmask;
263 j->flags.caps = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400264}
265
Will Drewry6ac91122011-10-21 16:38:58 -0500266void API minijail_namespace_vfs(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400267{
268 j->flags.vfs = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400269}
270
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700271void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path)
272{
273 int ns_fd = open(ns_path, O_RDONLY);
274 if (ns_fd < 0) {
275 pdie("failed to open namespace '%s'", ns_path);
276 }
277 j->mountns_fd = ns_fd;
278 j->flags.enter_vfs = 1;
279}
280
Will Drewry6ac91122011-10-21 16:38:58 -0500281void API minijail_namespace_pids(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400282{
Elly Jonese58176c2012-01-23 11:46:17 -0500283 j->flags.vfs = 1;
284 j->flags.readonly = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400285 j->flags.pids = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400286}
287
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400288void API minijail_namespace_net(struct minijail *j)
289{
290 j->flags.net = 1;
291}
292
Will Drewry6ac91122011-10-21 16:38:58 -0500293void API minijail_remount_readonly(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400294{
295 j->flags.vfs = 1;
296 j->flags.readonly = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400297}
298
Will Drewry6ac91122011-10-21 16:38:58 -0500299void API minijail_inherit_usergroups(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400300{
301 j->flags.usergroups = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400302}
303
Will Drewry6ac91122011-10-21 16:38:58 -0500304void API minijail_disable_ptrace(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400305{
306 j->flags.ptrace = 1;
Elly Jonescd7a9042011-07-22 13:56:51 -0400307}
308
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700309int API minijail_enter_chroot(struct minijail *j, const char *dir)
310{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400311 if (j->chrootdir)
312 return -EINVAL;
313 j->chrootdir = strdup(dir);
314 if (!j->chrootdir)
315 return -ENOMEM;
316 j->flags.chroot = 1;
317 return 0;
318}
319
Lee Campbell11af0622014-05-22 12:36:04 -0700320void API minijail_mount_tmp(struct minijail *j)
321{
322 j->flags.mount_tmp = 1;
323}
324
Will Drewry6ac91122011-10-21 16:38:58 -0500325int API minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700326 int writeable)
327{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400328 struct binding *b;
329
330 if (*dest != '/')
331 return -EINVAL;
332 b = calloc(1, sizeof(*b));
333 if (!b)
334 return -ENOMEM;
335 b->dest = strdup(dest);
336 if (!b->dest)
337 goto error;
338 b->src = strdup(src);
339 if (!b->src)
340 goto error;
341 b->writeable = writeable;
342
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700343 info("bind %s -> %s", src, dest);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400344
Elly Jonesdd3e8512012-01-23 15:13:38 -0500345 /*
346 * Force vfs namespacing so the bind mounts don't leak out into the
Elly Jones51a5b6c2011-10-12 19:09:26 -0400347 * containing vfs namespace.
348 */
349 minijail_namespace_vfs(j);
350
351 if (j->bindings_tail)
352 j->bindings_tail->next = b;
353 else
354 j->bindings_head = b;
355 j->bindings_tail = b;
356 j->binding_count++;
357
358 return 0;
359
360error:
361 free(b->src);
362 free(b->dest);
363 free(b);
364 return -ENOMEM;
365}
366
Will Drewry6ac91122011-10-21 16:38:58 -0500367void API minijail_parse_seccomp_filters(struct minijail *j, const char *path)
Elly Jonese1749eb2011-10-07 13:54:59 -0400368{
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700369 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) {
370 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
371 warn("not loading seccomp filter, seccomp not supported");
372 return;
373 }
374 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400375 FILE *file = fopen(path, "r");
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800376 if (!file) {
Jorge Lucangeli Obes224e4272012-08-02 14:31:39 -0700377 pdie("failed to open seccomp filter file '%s'", path);
Elly Jonese1749eb2011-10-07 13:54:59 -0400378 }
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800379
380 struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog));
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700381 if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) {
382 die("failed to compile seccomp filter BPF program in '%s'",
383 path);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800384 }
385
386 j->filter_len = fprog->len;
387 j->filter_prog = fprog;
388
Elly Jonese1749eb2011-10-07 13:54:59 -0400389 fclose(file);
Will Drewry32ac9f52011-08-18 21:36:27 -0500390}
391
Will Drewryf89aef52011-09-16 16:48:57 -0500392struct marshal_state {
Elly Jonese1749eb2011-10-07 13:54:59 -0400393 size_t available;
394 size_t total;
395 char *buf;
Will Drewryf89aef52011-09-16 16:48:57 -0500396};
397
Will Drewry6ac91122011-10-21 16:38:58 -0500398void marshal_state_init(struct marshal_state *state,
399 char *buf, size_t available)
Elly Jonese1749eb2011-10-07 13:54:59 -0400400{
401 state->available = available;
402 state->buf = buf;
403 state->total = 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500404}
405
Will Drewry6ac91122011-10-21 16:38:58 -0500406void marshal_append(struct marshal_state *state,
407 char *src, size_t length)
Elly Jonese1749eb2011-10-07 13:54:59 -0400408{
409 size_t copy_len = MIN(state->available, length);
Will Drewryf89aef52011-09-16 16:48:57 -0500410
Elly Jonese1749eb2011-10-07 13:54:59 -0400411 /* Up to |available| will be written. */
412 if (copy_len) {
413 memcpy(state->buf, src, copy_len);
414 state->buf += copy_len;
415 state->available -= copy_len;
416 }
417 /* |total| will contain the expected length. */
418 state->total += length;
Will Drewryf89aef52011-09-16 16:48:57 -0500419}
420
Will Drewry6ac91122011-10-21 16:38:58 -0500421void minijail_marshal_helper(struct marshal_state *state,
422 const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400423{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400424 struct binding *b = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -0400425 marshal_append(state, (char *)j, sizeof(*j));
426 if (j->user)
427 marshal_append(state, j->user, strlen(j->user) + 1);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400428 if (j->chrootdir)
429 marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1);
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800430 if (j->flags.seccomp_filter && j->filter_prog) {
431 struct sock_fprog *fp = j->filter_prog;
432 marshal_append(state, (char *)fp->filter,
433 fp->len * sizeof(struct sock_filter));
Elly Jonese1749eb2011-10-07 13:54:59 -0400434 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400435 for (b = j->bindings_head; b; b = b->next) {
436 marshal_append(state, b->src, strlen(b->src) + 1);
437 marshal_append(state, b->dest, strlen(b->dest) + 1);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700438 marshal_append(state, (char *)&b->writeable,
439 sizeof(b->writeable));
Elly Jones51a5b6c2011-10-12 19:09:26 -0400440 }
Will Drewryf89aef52011-09-16 16:48:57 -0500441}
442
Will Drewry6ac91122011-10-21 16:38:58 -0500443size_t API minijail_size(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400444{
445 struct marshal_state state;
446 marshal_state_init(&state, NULL, 0);
447 minijail_marshal_helper(&state, j);
448 return state.total;
Will Drewry2ddaad02011-09-16 11:36:08 -0500449}
450
Elly Jonese1749eb2011-10-07 13:54:59 -0400451int minijail_marshal(const struct minijail *j, char *buf, size_t available)
452{
453 struct marshal_state state;
454 marshal_state_init(&state, buf, available);
455 minijail_marshal_helper(&state, j);
456 return (state.total > available);
Will Drewry2ddaad02011-09-16 11:36:08 -0500457}
458
Elly Jones51a5b6c2011-10-12 19:09:26 -0400459/* consumebytes: consumes @length bytes from a buffer @buf of length @buflength
460 * @length Number of bytes to consume
461 * @buf Buffer to consume from
462 * @buflength Size of @buf
463 *
464 * Returns a pointer to the base of the bytes, or NULL for errors.
465 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700466void *consumebytes(size_t length, char **buf, size_t *buflength)
467{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400468 char *p = *buf;
469 if (length > *buflength)
470 return NULL;
471 *buf += length;
472 *buflength -= length;
473 return p;
474}
475
476/* consumestr: consumes a C string from a buffer @buf of length @length
477 * @buf Buffer to consume
478 * @length Length of buffer
479 *
480 * Returns a pointer to the base of the string, or NULL for errors.
481 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700482char *consumestr(char **buf, size_t *buflength)
483{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400484 size_t len = strnlen(*buf, *buflength);
485 if (len == *buflength)
486 /* There's no null-terminator */
487 return NULL;
488 return consumebytes(len + 1, buf, buflength);
489}
490
Elly Jonese1749eb2011-10-07 13:54:59 -0400491int minijail_unmarshal(struct minijail *j, char *serialized, size_t length)
492{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400493 int i;
494 int count;
Will Drewrybee7ba72011-10-21 20:47:01 -0500495 int ret = -EINVAL;
496
Elly Jonese1749eb2011-10-07 13:54:59 -0400497 if (length < sizeof(*j))
Will Drewrybee7ba72011-10-21 20:47:01 -0500498 goto out;
Elly Jonese1749eb2011-10-07 13:54:59 -0400499 memcpy((void *)j, serialized, sizeof(*j));
500 serialized += sizeof(*j);
501 length -= sizeof(*j);
Will Drewryf89aef52011-09-16 16:48:57 -0500502
Will Drewrybee7ba72011-10-21 20:47:01 -0500503 /* Potentially stale pointers not used as signals. */
504 j->bindings_head = NULL;
505 j->bindings_tail = NULL;
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800506 j->filter_prog = NULL;
Will Drewrybee7ba72011-10-21 20:47:01 -0500507
Elly Jonese1749eb2011-10-07 13:54:59 -0400508 if (j->user) { /* stale pointer */
Elly Jones51a5b6c2011-10-12 19:09:26 -0400509 char *user = consumestr(&serialized, &length);
510 if (!user)
Will Drewrybee7ba72011-10-21 20:47:01 -0500511 goto clear_pointers;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400512 j->user = strdup(user);
Will Drewrybee7ba72011-10-21 20:47:01 -0500513 if (!j->user)
514 goto clear_pointers;
Elly Jonese1749eb2011-10-07 13:54:59 -0400515 }
Will Drewryf89aef52011-09-16 16:48:57 -0500516
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400517 if (j->chrootdir) { /* stale pointer */
518 char *chrootdir = consumestr(&serialized, &length);
519 if (!chrootdir)
Will Drewrybee7ba72011-10-21 20:47:01 -0500520 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400521 j->chrootdir = strdup(chrootdir);
Will Drewrybee7ba72011-10-21 20:47:01 -0500522 if (!j->chrootdir)
523 goto bad_chrootdir;
Elly Jonesa8d1e1b2011-10-21 15:38:00 -0400524 }
525
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800526 if (j->flags.seccomp_filter && j->filter_len > 0) {
527 size_t ninstrs = j->filter_len;
528 if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) ||
529 ninstrs > USHRT_MAX)
530 goto bad_filters;
531
532 size_t program_len = ninstrs * sizeof(struct sock_filter);
533 void *program = consumebytes(program_len, &serialized, &length);
534 if (!program)
535 goto bad_filters;
536
537 j->filter_prog = malloc(sizeof(struct sock_fprog));
538 j->filter_prog->len = ninstrs;
539 j->filter_prog->filter = malloc(program_len);
540 memcpy(j->filter_prog->filter, program, program_len);
Elly Jonese1749eb2011-10-07 13:54:59 -0400541 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400542
543 count = j->binding_count;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400544 j->binding_count = 0;
545 for (i = 0; i < count; ++i) {
546 int *writeable;
547 const char *dest;
548 const char *src = consumestr(&serialized, &length);
549 if (!src)
Will Drewrybee7ba72011-10-21 20:47:01 -0500550 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400551 dest = consumestr(&serialized, &length);
552 if (!dest)
Will Drewrybee7ba72011-10-21 20:47:01 -0500553 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400554 writeable = consumebytes(sizeof(*writeable), &serialized, &length);
555 if (!writeable)
Will Drewrybee7ba72011-10-21 20:47:01 -0500556 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400557 if (minijail_bind(j, src, dest, *writeable))
Will Drewrybee7ba72011-10-21 20:47:01 -0500558 goto bad_bindings;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400559 }
560
Elly Jonese1749eb2011-10-07 13:54:59 -0400561 return 0;
Will Drewrybee7ba72011-10-21 20:47:01 -0500562
563bad_bindings:
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -0800564 if (j->flags.seccomp_filter && j->filter_len > 0) {
565 free(j->filter_prog->filter);
566 free(j->filter_prog);
567 }
Will Drewrybee7ba72011-10-21 20:47:01 -0500568bad_filters:
569 if (j->chrootdir)
570 free(j->chrootdir);
571bad_chrootdir:
572 if (j->user)
573 free(j->user);
574clear_pointers:
575 j->user = NULL;
576 j->chrootdir = NULL;
577out:
578 return ret;
Will Drewry2ddaad02011-09-16 11:36:08 -0500579}
580
Elly Jones51a5b6c2011-10-12 19:09:26 -0400581/* bind_one: Applies bindings from @b for @j, recursing as needed.
582 * @j Minijail these bindings are for
583 * @b Head of list of bindings
584 *
585 * Returns 0 for success.
586 */
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700587int bind_one(const struct minijail *j, struct binding *b)
588{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400589 int ret = 0;
590 char *dest = NULL;
Elly Jones51a5b6c2011-10-12 19:09:26 -0400591 if (ret)
592 return ret;
593 /* dest has a leading "/" */
594 if (asprintf(&dest, "%s%s", j->chrootdir, b->dest) < 0)
595 return -ENOMEM;
Elly Jonesa1059632011-12-15 15:17:07 -0500596 ret = mount(b->src, dest, NULL, MS_BIND, NULL);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400597 if (ret)
598 pdie("bind: %s -> %s", b->src, dest);
Elly Jonesa1059632011-12-15 15:17:07 -0500599 if (!b->writeable) {
600 ret = mount(b->src, dest, NULL,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700601 MS_BIND | MS_REMOUNT | MS_RDONLY, NULL);
Elly Jonesa1059632011-12-15 15:17:07 -0500602 if (ret)
603 pdie("bind ro: %s -> %s", b->src, dest);
604 }
Elly Jones51a5b6c2011-10-12 19:09:26 -0400605 free(dest);
606 if (b->next)
607 return bind_one(j, b->next);
608 return ret;
609}
610
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700611int enter_chroot(const struct minijail *j)
612{
Elly Jones51a5b6c2011-10-12 19:09:26 -0400613 int ret;
614 if (j->bindings_head && (ret = bind_one(j, j->bindings_head)))
615 return ret;
616
617 if (chroot(j->chrootdir))
618 return -errno;
619
620 if (chdir("/"))
621 return -errno;
622
623 return 0;
624}
625
Lee Campbell11af0622014-05-22 12:36:04 -0700626int mount_tmp(void)
627{
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800628 return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777");
Lee Campbell11af0622014-05-22 12:36:04 -0700629}
630
Will Drewry6ac91122011-10-21 16:38:58 -0500631int remount_readonly(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400632{
633 const char *kProcPath = "/proc";
634 const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
Elly Jonesdd3e8512012-01-23 15:13:38 -0500635 /*
636 * Right now, we're holding a reference to our parent's old mount of
Elly Jonese1749eb2011-10-07 13:54:59 -0400637 * /proc in our namespace, which means using MS_REMOUNT here would
638 * mutate our parent's mount as well, even though we're in a VFS
639 * namespace (!). Instead, remove their mount from our namespace
640 * and make our own.
641 */
642 if (umount(kProcPath))
643 return -errno;
644 if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, ""))
645 return -errno;
646 return 0;
Elly Jonescd7a9042011-07-22 13:56:51 -0400647}
648
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700649void drop_ugid(const struct minijail *j)
650{
651 if (j->flags.usergroups) {
652 if (initgroups(j->user, j->usergid))
653 pdie("initgroups");
654 } else {
655 /* Only attempt to clear supplemental groups if we are changing
656 * users. */
657 if ((j->uid || j->gid) && setgroups(0, NULL))
658 pdie("setgroups");
659 }
660
661 if (j->flags.gid && setresgid(j->gid, j->gid, j->gid))
662 pdie("setresgid");
663
664 if (j->flags.uid && setresuid(j->uid, j->uid, j->uid))
665 pdie("setresuid");
666}
667
Mike Frysinger3adfef72013-05-09 17:19:08 -0400668/*
669 * We specifically do not use cap_valid() as that only tells us the last
670 * valid cap we were *compiled* against (i.e. what the version of kernel
671 * headers says). If we run on a different kernel version, then it's not
672 * uncommon for that to be less (if an older kernel) or more (if a newer
673 * kernel). So suck up the answer via /proc.
674 */
675static int run_cap_valid(unsigned int cap)
676{
677 static unsigned int last_cap;
678
679 if (!last_cap) {
680 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
681 FILE *fp = fopen(cap_file, "re");
682 if (fscanf(fp, "%u", &last_cap) != 1)
683 pdie("fscanf(%s)", cap_file);
684 fclose(fp);
685 }
686
687 return cap <= last_cap;
688}
689
Will Drewry6ac91122011-10-21 16:38:58 -0500690void drop_caps(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400691{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700692#if defined(__BRILLO__)
693 /*
694 * Temporarily disable capabilities support until Minijail can use
695 * libcap-ng.
696 */
697 (void) j;
698#else
Elly Jonese1749eb2011-10-07 13:54:59 -0400699 cap_t caps = cap_get_proc();
Kees Cook323878a2013-02-05 15:35:24 -0800700 cap_value_t flag[1];
Kees Cooke5609ac2013-02-06 14:12:41 -0800701 const uint64_t one = 1;
Elly Jonese1749eb2011-10-07 13:54:59 -0400702 unsigned int i;
703 if (!caps)
704 die("can't get process caps");
705 if (cap_clear_flag(caps, CAP_INHERITABLE))
706 die("can't clear inheritable caps");
707 if (cap_clear_flag(caps, CAP_EFFECTIVE))
708 die("can't clear effective caps");
709 if (cap_clear_flag(caps, CAP_PERMITTED))
710 die("can't clear permitted caps");
Mike Frysinger3adfef72013-05-09 17:19:08 -0400711 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cook323878a2013-02-05 15:35:24 -0800712 /* Keep CAP_SETPCAP for dropping bounding set bits. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800713 if (i != CAP_SETPCAP && !(j->caps & (one << i)))
Elly Jonese1749eb2011-10-07 13:54:59 -0400714 continue;
Kees Cook323878a2013-02-05 15:35:24 -0800715 flag[0] = i;
716 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400717 die("can't add effective cap");
Kees Cook323878a2013-02-05 15:35:24 -0800718 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400719 die("can't add permitted cap");
Kees Cook323878a2013-02-05 15:35:24 -0800720 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET))
Elly Jonese1749eb2011-10-07 13:54:59 -0400721 die("can't add inheritable cap");
722 }
723 if (cap_set_proc(caps))
Kees Cook323878a2013-02-05 15:35:24 -0800724 die("can't apply initial cleaned capset");
725
726 /*
727 * Instead of dropping bounding set first, do it here in case
728 * the caller had a more permissive bounding set which could
729 * have been used above to raise a capability that wasn't already
730 * present. This requires CAP_SETPCAP, so we raised/kept it above.
731 */
Mike Frysinger3adfef72013-05-09 17:19:08 -0400732 for (i = 0; i < sizeof(j->caps) * 8 && run_cap_valid(i); ++i) {
Kees Cooke5609ac2013-02-06 14:12:41 -0800733 if (j->caps & (one << i))
Elly Jonese1749eb2011-10-07 13:54:59 -0400734 continue;
735 if (prctl(PR_CAPBSET_DROP, i))
736 pdie("prctl(PR_CAPBSET_DROP)");
737 }
Kees Cook323878a2013-02-05 15:35:24 -0800738
739 /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */
Kees Cooke5609ac2013-02-06 14:12:41 -0800740 if ((j->caps & (one << CAP_SETPCAP)) == 0) {
Kees Cook323878a2013-02-05 15:35:24 -0800741 flag[0] = CAP_SETPCAP;
742 if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR))
743 die("can't clear effective cap");
744 if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR))
745 die("can't clear permitted cap");
746 if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR))
747 die("can't clear inheritable cap");
748 }
749
750 if (cap_set_proc(caps))
751 die("can't apply final cleaned capset");
752
753 cap_free(caps);
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700754#endif
Elly Jonescd7a9042011-07-22 13:56:51 -0400755}
756
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700757void set_seccomp_filter(const struct minijail *j)
758{
759 /*
760 * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c>
761 * in the kernel source tree for an explanation of the parameters.
762 */
763 if (j->flags.no_new_privs) {
764 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
765 pdie("prctl(PR_SET_NO_NEW_PRIVS)");
766 }
767
768 /*
769 * If we're logging seccomp filter failures,
770 * install the SIGSYS handler first.
771 */
772 if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) {
773 if (install_sigsys_handler())
774 pdie("install SIGSYS handler");
775 warn("logging seccomp filter failures");
776 }
777
778 /*
779 * Install the syscall filter.
780 */
781 if (j->flags.seccomp_filter) {
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700782 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) {
783 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
784 warn("seccomp not supported");
785 return;
786 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700787 pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700788 }
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700789 }
790}
791
Will Drewry6ac91122011-10-21 16:38:58 -0500792void API minijail_enter(const struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400793{
794 if (j->flags.pids)
795 die("tried to enter a pid-namespaced jail;"
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700796 " try minijail_run()?");
Elly Jonescd7a9042011-07-22 13:56:51 -0400797
Elly Jonese1749eb2011-10-07 13:54:59 -0400798 if (j->flags.usergroups && !j->user)
799 die("usergroup inheritance without username");
Elly Jonescd7a9042011-07-22 13:56:51 -0400800
Elly Jonesdd3e8512012-01-23 15:13:38 -0500801 /*
802 * We can't recover from failures if we've dropped privileges partially,
Elly Jonese1749eb2011-10-07 13:54:59 -0400803 * so we don't even try. If any of our operations fail, we abort() the
804 * entire process.
805 */
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700806 if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS))
807 pdie("setns(CLONE_NEWNS)");
808
Elly Jonese1749eb2011-10-07 13:54:59 -0400809 if (j->flags.vfs && unshare(CLONE_NEWNS))
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400810 pdie("unshare(vfs)");
811
812 if (j->flags.net && unshare(CLONE_NEWNET))
813 pdie("unshare(net)");
Elly Jonescd7a9042011-07-22 13:56:51 -0400814
Elly Jones51a5b6c2011-10-12 19:09:26 -0400815 if (j->flags.chroot && enter_chroot(j))
816 pdie("chroot");
817
Jorge Lucangeli Obes3901da62015-03-03 13:55:11 -0800818 if (j->flags.mount_tmp && mount_tmp())
Lee Campbell11af0622014-05-22 12:36:04 -0700819 pdie("mount_tmp");
820
Elly Jonese1749eb2011-10-07 13:54:59 -0400821 if (j->flags.readonly && remount_readonly())
822 pdie("remount");
Elly Jonescd7a9042011-07-22 13:56:51 -0400823
Elly Jonese1749eb2011-10-07 13:54:59 -0400824 if (j->flags.caps) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500825 /*
826 * POSIX capabilities are a bit tricky. If we drop our
Elly Jonese1749eb2011-10-07 13:54:59 -0400827 * capability to change uids, our attempt to use setuid()
828 * below will fail. Hang on to root caps across setuid(), then
829 * lock securebits.
830 */
831 if (prctl(PR_SET_KEEPCAPS, 1))
832 pdie("prctl(PR_SET_KEEPCAPS)");
833 if (prctl
834 (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS))
835 pdie("prctl(PR_SET_SECUREBITS)");
836 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400837
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700838 /*
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700839 * If we're setting no_new_privs, we can drop privileges
840 * before setting seccomp filter. This way filter policies
841 * don't need to allow privilege-dropping syscalls.
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700842 */
843 if (j->flags.no_new_privs) {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700844 drop_ugid(j);
845 if (j->flags.caps)
846 drop_caps(j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700847
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700848 set_seccomp_filter(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400849 } else {
Jorge Lucangeli Obes6201cf52012-08-23 11:42:27 -0700850 /*
851 * If we're not setting no_new_privs,
852 * we need to set seccomp filter *before* dropping privileges.
853 * WARNING: this means that filter policies *must* allow
854 * setgroups()/setresgid()/setresuid() for dropping root and
855 * capget()/capset()/prctl() for dropping caps.
856 */
857 set_seccomp_filter(j);
858
859 drop_ugid(j);
860 if (j->flags.caps)
861 drop_caps(j);
Elly Jonese1749eb2011-10-07 13:54:59 -0400862 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400863
Elly Jonesdd3e8512012-01-23 15:13:38 -0500864 /*
865 * seccomp has to come last since it cuts off all the other
Elly Jonese1749eb2011-10-07 13:54:59 -0400866 * privilege-dropping syscalls :)
867 */
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700868 if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) {
869 if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) {
870 warn("seccomp not supported");
871 return;
872 }
Elly Jonese1749eb2011-10-07 13:54:59 -0400873 pdie("prctl(PR_SET_SECCOMP)");
Utkarsh Sanghi0ef8a662014-08-18 15:50:11 -0700874 }
Elly Jonescd7a9042011-07-22 13:56:51 -0400875}
876
Will Drewry6ac91122011-10-21 16:38:58 -0500877/* TODO(wad) will visibility affect this variable? */
Elly Jonescd7a9042011-07-22 13:56:51 -0400878static int init_exitstatus = 0;
879
Will Drewry6ac91122011-10-21 16:38:58 -0500880void init_term(int __attribute__ ((unused)) sig)
Elly Jonese1749eb2011-10-07 13:54:59 -0400881{
882 _exit(init_exitstatus);
Elly Jonescd7a9042011-07-22 13:56:51 -0400883}
884
Will Drewry6ac91122011-10-21 16:38:58 -0500885int init(pid_t rootpid)
Elly Jonese1749eb2011-10-07 13:54:59 -0400886{
887 pid_t pid;
888 int status;
889 /* so that we exit with the right status */
890 signal(SIGTERM, init_term);
891 /* TODO(wad) self jail with seccomp_filters here. */
892 while ((pid = wait(&status)) > 0) {
Elly Jonesdd3e8512012-01-23 15:13:38 -0500893 /*
894 * This loop will only end when either there are no processes
Elly Jonese1749eb2011-10-07 13:54:59 -0400895 * left inside our pid namespace or we get a signal.
896 */
897 if (pid == rootpid)
898 init_exitstatus = status;
899 }
900 if (!WIFEXITED(init_exitstatus))
901 _exit(MINIJAIL_ERR_INIT);
902 _exit(WEXITSTATUS(init_exitstatus));
Elly Jonescd7a9042011-07-22 13:56:51 -0400903}
904
Will Drewry6ac91122011-10-21 16:38:58 -0500905int API minijail_from_fd(int fd, struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -0400906{
907 size_t sz = 0;
908 size_t bytes = read(fd, &sz, sizeof(sz));
909 char *buf;
910 int r;
911 if (sizeof(sz) != bytes)
912 return -EINVAL;
913 if (sz > USHRT_MAX) /* Arbitrary sanity check */
914 return -E2BIG;
915 buf = malloc(sz);
916 if (!buf)
917 return -ENOMEM;
918 bytes = read(fd, buf, sz);
919 if (bytes != sz) {
920 free(buf);
921 return -EINVAL;
922 }
923 r = minijail_unmarshal(j, buf, sz);
924 free(buf);
925 return r;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500926}
927
Will Drewry6ac91122011-10-21 16:38:58 -0500928int API minijail_to_fd(struct minijail *j, int fd)
Elly Jonese1749eb2011-10-07 13:54:59 -0400929{
930 char *buf;
931 size_t sz = minijail_size(j);
932 ssize_t written;
933 int r;
Elly Jonescd7a9042011-07-22 13:56:51 -0400934
Elly Jonese1749eb2011-10-07 13:54:59 -0400935 if (!sz)
936 return -EINVAL;
937 buf = malloc(sz);
938 r = minijail_marshal(j, buf, sz);
939 if (r) {
940 free(buf);
941 return r;
942 }
943 /* Sends [size][minijail]. */
944 written = write(fd, &sz, sizeof(sz));
945 if (written != sizeof(sz)) {
946 free(buf);
947 return -EFAULT;
948 }
949 written = write(fd, buf, sz);
950 if (written < 0 || (size_t) written != sz) {
951 free(buf);
952 return -EFAULT;
953 }
954 free(buf);
955 return 0;
Will Drewry2f54b6a2011-09-16 13:45:31 -0500956}
Elly Jonescd7a9042011-07-22 13:56:51 -0400957
Will Drewry6ac91122011-10-21 16:38:58 -0500958int setup_preload(void)
Elly Jonese1749eb2011-10-07 13:54:59 -0400959{
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700960#if defined(__BRILLO__)
961 /* Don't use LDPRELOAD on Brillo. */
962 return 0;
963#else
Elly Jonese1749eb2011-10-07 13:54:59 -0400964 char *oldenv = getenv(kLdPreloadEnvVar) ? : "";
965 char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH));
966 if (!newenv)
967 return -ENOMEM;
Elly Jonescd7a9042011-07-22 13:56:51 -0400968
Elly Jonese1749eb2011-10-07 13:54:59 -0400969 /* Only insert a separating space if we have something to separate... */
970 sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "",
971 PRELOADPATH);
Elly Jonescd7a9042011-07-22 13:56:51 -0400972
Elly Jonese1749eb2011-10-07 13:54:59 -0400973 /* setenv() makes a copy of the string we give it */
974 setenv(kLdPreloadEnvVar, newenv, 1);
975 free(newenv);
976 return 0;
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -0700977#endif
Elly Jonescd7a9042011-07-22 13:56:51 -0400978}
979
Will Drewry6ac91122011-10-21 16:38:58 -0500980int setup_pipe(int fds[2])
Elly Jonese1749eb2011-10-07 13:54:59 -0400981{
982 int r = pipe(fds);
983 char fd_buf[11];
984 if (r)
985 return r;
986 r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]);
987 if (r <= 0)
988 return -EINVAL;
989 setenv(kFdEnvVar, fd_buf, 1);
990 return 0;
Will Drewryf89aef52011-09-16 16:48:57 -0500991}
992
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800993int setup_pipe_end(int fds[2], size_t index)
994{
995 if (index > 1)
996 return -1;
997
998 close(fds[1 - index]);
999 return fds[index];
1000}
1001
1002int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd)
1003{
1004 if (index > 1)
1005 return -1;
1006
1007 close(fds[1 - index]);
1008 /* dup2(2) the corresponding end of the pipe into |fd|. */
1009 return dup2(fds[index], fd);
1010}
1011
Will Drewry6ac91122011-10-21 16:38:58 -05001012int API minijail_run(struct minijail *j, const char *filename,
1013 char *const argv[])
Elly Jonese1749eb2011-10-07 13:54:59 -04001014{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001015 return minijail_run_pid_pipes(j, filename, argv,
1016 NULL, NULL, NULL, NULL);
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001017}
1018
1019int API minijail_run_pid(struct minijail *j, const char *filename,
1020 char *const argv[], pid_t *pchild_pid)
1021{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001022 return minijail_run_pid_pipes(j, filename, argv, pchild_pid,
1023 NULL, NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001024}
1025
1026int API minijail_run_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001027 char *const argv[], int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001028{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001029 return minijail_run_pid_pipes(j, filename, argv, NULL, pstdin_fd,
1030 NULL, NULL);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001031}
1032
1033int API minijail_run_pid_pipe(struct minijail *j, const char *filename,
Jorge Lucangeli Obes6537a562012-09-05 10:39:40 -07001034 char *const argv[], pid_t *pchild_pid,
1035 int *pstdin_fd)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001036{
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001037 return minijail_run_pid_pipes(j, filename, argv, pchild_pid, pstdin_fd,
1038 NULL, NULL);
1039}
1040
1041int API minijail_run_pid_pipes(struct minijail *j, const char *filename,
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001042 char *const argv[], pid_t *pchild_pid,
1043 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd)
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001044{
Elly Jonese1749eb2011-10-07 13:54:59 -04001045 char *oldenv, *oldenv_copy = NULL;
1046 pid_t child_pid;
1047 int pipe_fds[2];
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001048 int stdin_fds[2];
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001049 int stdout_fds[2];
1050 int stderr_fds[2];
Elly Jonese1749eb2011-10-07 13:54:59 -04001051 int ret;
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001052 /* We need to remember this across the minijail_preexec() call. */
1053 int pid_namespace = j->flags.pids;
Ben Chan541c7e52011-08-26 14:55:53 -07001054
Elly Jonese1749eb2011-10-07 13:54:59 -04001055 oldenv = getenv(kLdPreloadEnvVar);
1056 if (oldenv) {
1057 oldenv_copy = strdup(oldenv);
1058 if (!oldenv_copy)
1059 return -ENOMEM;
1060 }
Will Drewryf89aef52011-09-16 16:48:57 -05001061
Elly Jonese1749eb2011-10-07 13:54:59 -04001062 if (setup_preload())
1063 return -EFAULT;
Will Drewry2f54b6a2011-09-16 13:45:31 -05001064
Elly Jonesdd3e8512012-01-23 15:13:38 -05001065 /*
Jorge Lucangeli Obes3c84df12015-05-14 17:37:58 -07001066 * Make the process group ID of this process equal to its PID, so that
1067 * both the Minijail process and the jailed process can be killed
1068 * together.
1069 * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when
1070 * the process is already a process group leader.
1071 */
1072 if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) {
1073 if (errno != EPERM) {
1074 pdie("setpgid(0, 0)");
1075 }
1076 }
1077
1078 /*
Elly Jonesdd3e8512012-01-23 15:13:38 -05001079 * Before we fork(2) and execve(2) the child process, we need to open
Elly Jonese1749eb2011-10-07 13:54:59 -04001080 * a pipe(2) to send the minijail configuration over.
1081 */
1082 if (setup_pipe(pipe_fds))
1083 return -EFAULT;
Elly Jonescd7a9042011-07-22 13:56:51 -04001084
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001085 /*
1086 * If we want to write to the child process' standard input,
1087 * create the pipe(2) now.
1088 */
1089 if (pstdin_fd) {
1090 if (pipe(stdin_fds))
1091 return -EFAULT;
1092 }
1093
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001094 /*
1095 * If we want to read from the child process' standard output,
1096 * create the pipe(2) now.
1097 */
1098 if (pstdout_fd) {
1099 if (pipe(stdout_fds))
1100 return -EFAULT;
1101 }
1102
1103 /*
1104 * If we want to read from the child process' standard error,
1105 * create the pipe(2) now.
1106 */
1107 if (pstderr_fd) {
1108 if (pipe(stderr_fds))
1109 return -EFAULT;
1110 }
1111
Elly Jones761b7412012-06-13 15:49:52 -04001112 /* Use sys_clone() if and only if we're creating a pid namespace.
1113 *
1114 * tl;dr: WARNING: do not mix pid namespaces and multithreading.
1115 *
1116 * In multithreaded programs, there are a bunch of locks inside libc,
1117 * some of which may be held by other threads at the time that we call
1118 * minijail_run_pid(). If we call fork(), glibc does its level best to
1119 * ensure that we hold all of these locks before it calls clone()
1120 * internally and drop them after clone() returns, but when we call
1121 * sys_clone(2) directly, all that gets bypassed and we end up with a
1122 * child address space where some of libc's important locks are held by
1123 * other threads (which did not get cloned, and hence will never release
1124 * those locks). This is okay so long as we call exec() immediately
1125 * after, but a bunch of seemingly-innocent libc functions like setenv()
1126 * take locks.
1127 *
1128 * Hence, only call sys_clone() if we need to, in order to get at pid
1129 * namespacing. If we follow this path, the child's address space might
1130 * have broken locks; you may only call functions that do not acquire
1131 * any locks.
1132 *
1133 * Unfortunately, fork() acquires every lock it can get its hands on, as
1134 * previously detailed, so this function is highly likely to deadlock
1135 * later on (see "deadlock here") if we're multithreaded.
1136 *
1137 * We might hack around this by having the clone()d child (init of the
1138 * pid namespace) return directly, rather than leaving the clone()d
1139 * process hanging around to be init for the new namespace (and having
1140 * its fork()ed child return in turn), but that process would be crippled
1141 * with its libc locks potentially broken. We might try fork()ing in the
1142 * parent before we clone() to ensure that we own all the locks, but
1143 * then we have to have the forked child hanging around consuming
1144 * resources (and possibly having file descriptors / shared memory
1145 * regions / etc attached). We'd need to keep the child around to avoid
1146 * having its children get reparented to init.
1147 *
1148 * TODO(ellyjones): figure out if the "forked child hanging around"
1149 * problem is fixable or not. It would be nice if we worked in this
1150 * case.
1151 */
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001152 if (pid_namespace)
Elly Jones761b7412012-06-13 15:49:52 -04001153 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1154 else
1155 child_pid = fork();
1156
Elly Jonese1749eb2011-10-07 13:54:59 -04001157 if (child_pid < 0) {
1158 free(oldenv_copy);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001159 die("failed to fork child");
Elly Jonese1749eb2011-10-07 13:54:59 -04001160 }
Will Drewryf89aef52011-09-16 16:48:57 -05001161
Elly Jonese1749eb2011-10-07 13:54:59 -04001162 if (child_pid) {
1163 /* Restore parent's LD_PRELOAD. */
1164 if (oldenv_copy) {
1165 setenv(kLdPreloadEnvVar, oldenv_copy, 1);
1166 free(oldenv_copy);
1167 } else {
1168 unsetenv(kLdPreloadEnvVar);
1169 }
1170 unsetenv(kFdEnvVar);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001171
Elly Jonese1749eb2011-10-07 13:54:59 -04001172 j->initpid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001173
1174 /* Send marshalled minijail. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001175 close(pipe_fds[0]); /* read endpoint */
1176 ret = minijail_to_fd(j, pipe_fds[1]);
1177 close(pipe_fds[1]); /* write endpoint */
1178 if (ret) {
1179 kill(j->initpid, SIGKILL);
1180 die("failed to send marshalled minijail");
1181 }
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001182
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -07001183 if (pchild_pid)
1184 *pchild_pid = child_pid;
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001185
1186 /*
1187 * If we want to write to the child process' standard input,
1188 * set up the write end of the pipe.
1189 */
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001190 if (pstdin_fd)
1191 *pstdin_fd = setup_pipe_end(stdin_fds,
1192 1 /* write end */);
1193
1194 /*
1195 * If we want to read from the child process' standard output,
1196 * set up the read end of the pipe.
1197 */
1198 if (pstdout_fd)
1199 *pstdout_fd = setup_pipe_end(stdout_fds,
1200 0 /* read end */);
1201
1202 /*
1203 * If we want to read from the child process' standard error,
1204 * set up the read end of the pipe.
1205 */
1206 if (pstderr_fd)
1207 *pstderr_fd = setup_pipe_end(stderr_fds,
1208 0 /* read end */);
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001209
Elly Jonese1749eb2011-10-07 13:54:59 -04001210 return 0;
1211 }
1212 free(oldenv_copy);
Ben Chan541c7e52011-08-26 14:55:53 -07001213
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001214 /*
1215 * If we want to write to the jailed process' standard input,
1216 * set up the read end of the pipe.
1217 */
1218 if (pstdin_fd) {
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001219 if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */,
1220 STDIN_FILENO) < 0)
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -07001221 die("failed to set up stdin pipe");
1222 }
1223
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -08001224 /*
1225 * If we want to read from the jailed process' standard output,
1226 * set up the write end of the pipe.
1227 */
1228 if (pstdout_fd) {
1229 if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */,
1230 STDOUT_FILENO) < 0)
1231 die("failed to set up stdout pipe");
1232 }
1233
1234 /*
1235 * If we want to read from the jailed process' standard error,
1236 * set up the write end of the pipe.
1237 */
1238 if (pstderr_fd) {
1239 if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */,
1240 STDERR_FILENO) < 0)
1241 die("failed to set up stderr pipe");
1242 }
1243
Jorge Lucangeli Obes4ae30cc2014-04-10 15:35:33 -07001244 /* Strip out flags that cannot be inherited across execve. */
Elly Jonese1749eb2011-10-07 13:54:59 -04001245 minijail_preexec(j);
1246 /* Jail this process and its descendants... */
1247 minijail_enter(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001248
Elly Jonesa05d7bb2012-06-14 14:09:27 -04001249 if (pid_namespace) {
Elly Jonesdd3e8512012-01-23 15:13:38 -05001250 /*
1251 * pid namespace: this process will become init inside the new
Elly Jonese1749eb2011-10-07 13:54:59 -04001252 * namespace, so fork off a child to actually run the program
1253 * (we don't want all programs we might exec to have to know
1254 * how to be init).
Elly Jones761b7412012-06-13 15:49:52 -04001255 *
1256 * If we're multithreaded, we'll probably deadlock here. See
1257 * WARNING above.
Elly Jonese1749eb2011-10-07 13:54:59 -04001258 */
1259 child_pid = fork();
1260 if (child_pid < 0)
1261 _exit(child_pid);
1262 else if (child_pid > 0)
1263 init(child_pid); /* never returns */
1264 }
Elly Jonescd7a9042011-07-22 13:56:51 -04001265
Elly Jonesdd3e8512012-01-23 15:13:38 -05001266 /*
1267 * If we aren't pid-namespaced:
Elly Jonese1749eb2011-10-07 13:54:59 -04001268 * calling process
1269 * -> execve()-ing process
1270 * If we are:
1271 * calling process
1272 * -> init()-ing process
1273 * -> execve()-ing process
1274 */
1275 _exit(execve(filename, argv, environ));
Elly Jonescd7a9042011-07-22 13:56:51 -04001276}
1277
Lee Campbell1e4fc6a2014-06-06 17:40:02 -07001278int API minijail_run_static(struct minijail *j, const char *filename,
1279 char *const argv[])
1280{
1281 pid_t child_pid;
1282 int pid_namespace = j->flags.pids;
1283
1284 if (j->flags.caps)
1285 die("caps not supported with static targets");
1286
1287 if (pid_namespace)
1288 child_pid = syscall(SYS_clone, CLONE_NEWPID | SIGCHLD, NULL);
1289 else
1290 child_pid = fork();
1291
1292 if (child_pid < 0) {
1293 die("failed to fork child");
1294 }
1295 if (child_pid > 0 ) {
1296 j->initpid = child_pid;
1297 return 0;
1298 }
1299
1300 /*
1301 * We can now drop this child into the sandbox
1302 * then execve the target.
1303 */
1304
1305 j->flags.pids = 0;
1306 minijail_enter(j);
1307
1308 if (pid_namespace) {
1309 /*
1310 * pid namespace: this process will become init inside the new
1311 * namespace, so fork off a child to actually run the program
1312 * (we don't want all programs we might exec to have to know
1313 * how to be init).
1314 *
1315 * If we're multithreaded, we'll probably deadlock here. See
1316 * WARNING above.
1317 */
1318 child_pid = fork();
1319 if (child_pid < 0)
1320 _exit(child_pid);
1321 else if (child_pid > 0)
1322 init(child_pid); /* never returns */
1323 }
1324
1325 _exit(execve(filename, argv, environ));
1326}
1327
Will Drewry6ac91122011-10-21 16:38:58 -05001328int API minijail_kill(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001329{
1330 int st;
1331 if (kill(j->initpid, SIGTERM))
1332 return -errno;
1333 if (waitpid(j->initpid, &st, 0) < 0)
1334 return -errno;
1335 return st;
Elly Jonescd7a9042011-07-22 13:56:51 -04001336}
1337
Will Drewry6ac91122011-10-21 16:38:58 -05001338int API minijail_wait(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001339{
1340 int st;
1341 if (waitpid(j->initpid, &st, 0) < 0)
1342 return -errno;
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001343
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001344 if (!WIFEXITED(st)) {
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001345 int error_status = st;
1346 if (WIFSIGNALED(st)) {
1347 int signum = WTERMSIG(st);
mukesh agrawalc420a262013-06-11 17:22:42 -07001348 warn("child process %d received signal %d",
Jorge Lucangeli Obes18d1eba2014-04-18 13:58:20 -07001349 j->initpid, signum);
1350 /*
1351 * We return MINIJAIL_ERR_JAIL if the process received
1352 * SIGSYS, which happens when a syscall is blocked by
1353 * seccomp filters.
1354 * If not, we do what bash(1) does:
1355 * $? = 128 + signum
1356 */
1357 if (signum == SIGSYS) {
1358 error_status = MINIJAIL_ERR_JAIL;
1359 } else {
1360 error_status = 128 + signum;
1361 }
1362 }
1363 return error_status;
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -07001364 }
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001365
1366 int exit_status = WEXITSTATUS(st);
1367 if (exit_status != 0)
mukesh agrawalc420a262013-06-11 17:22:42 -07001368 info("child process %d exited with status %d",
1369 j->initpid, exit_status);
Jorge Lucangeli Obes1530b742012-12-11 14:08:09 -08001370
1371 return exit_status;
Elly Jonescd7a9042011-07-22 13:56:51 -04001372}
1373
Will Drewry6ac91122011-10-21 16:38:58 -05001374void API minijail_destroy(struct minijail *j)
Elly Jonese1749eb2011-10-07 13:54:59 -04001375{
Jorge Lucangeli Obes524c0402012-01-17 11:30:23 -08001376 if (j->flags.seccomp_filter && j->filter_prog) {
1377 free(j->filter_prog->filter);
1378 free(j->filter_prog);
Elly Jonese1749eb2011-10-07 13:54:59 -04001379 }
Elly Jones51a5b6c2011-10-12 19:09:26 -04001380 while (j->bindings_head) {
1381 struct binding *b = j->bindings_head;
1382 j->bindings_head = j->bindings_head->next;
1383 free(b->dest);
1384 free(b->src);
1385 free(b);
1386 }
1387 j->bindings_tail = NULL;
Elly Jonese1749eb2011-10-07 13:54:59 -04001388 if (j->user)
1389 free(j->user);
Will Drewrybee7ba72011-10-21 20:47:01 -05001390 if (j->chrootdir)
1391 free(j->chrootdir);
Elly Jonese1749eb2011-10-07 13:54:59 -04001392 free(j);
Elly Jonescd7a9042011-07-22 13:56:51 -04001393}