Jorge Lucangeli Obes | d613ab2 | 2015-03-03 14:22:50 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 3 | * found in the LICENSE file. |
| 4 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 5 | |
| 6 | #define _BSD_SOURCE |
| 7 | #define _GNU_SOURCE |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 8 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 9 | #include <asm/unistd.h> |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 10 | #include <ctype.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 11 | #include <errno.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 12 | #include <fcntl.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 13 | #include <grp.h> |
| 14 | #include <inttypes.h> |
Will Drewry | fe4a372 | 2011-09-16 14:50:50 -0500 | [diff] [blame] | 15 | #include <limits.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 16 | #include <linux/capability.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 17 | #include <pwd.h> |
| 18 | #include <sched.h> |
| 19 | #include <signal.h> |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 20 | #include <stdarg.h> |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 21 | #include <stdbool.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 22 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <syscall.h> |
| 27 | #include <sys/capability.h> |
| 28 | #include <sys/mount.h> |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 29 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 30 | #include <sys/prctl.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 33 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 34 | #include <sys/wait.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
| 37 | #include "libminijail.h" |
| 38 | #include "libminijail-private.h" |
| 39 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 40 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 41 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 42 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 43 | |
Lei Zhang | eee3155 | 2012-10-17 21:27:10 -0700 | [diff] [blame] | 44 | #ifdef HAVE_SECUREBITS_H |
| 45 | #include <linux/securebits.h> |
| 46 | #else |
| 47 | #define SECURE_ALL_BITS 0x15 |
| 48 | #define SECURE_ALL_LOCKS (SECURE_ALL_BITS << 1) |
| 49 | #endif |
| 50 | |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 51 | /* Until these are reliably available in linux/prctl.h */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 52 | #ifndef PR_SET_SECCOMP |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 53 | # define PR_SET_SECCOMP 22 |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 54 | #endif |
| 55 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 56 | #ifndef PR_ALT_SYSCALL |
| 57 | # define PR_ALT_SYSCALL 0x43724f53 |
| 58 | #endif |
| 59 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 60 | /* For seccomp_filter using BPF. */ |
| 61 | #ifndef PR_SET_NO_NEW_PRIVS |
| 62 | # define PR_SET_NO_NEW_PRIVS 38 |
| 63 | #endif |
| 64 | #ifndef SECCOMP_MODE_FILTER |
| 65 | # define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 66 | #endif |
| 67 | |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 68 | #ifdef USE_SECCOMP_SOFTFAIL |
| 69 | # define SECCOMP_SOFTFAIL 1 |
| 70 | #else |
| 71 | # define SECCOMP_SOFTFAIL 0 |
| 72 | #endif |
| 73 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 74 | struct mountpoint { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 75 | char *src; |
| 76 | char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 77 | char *type; |
| 78 | unsigned long flags; |
| 79 | struct mountpoint *next; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 82 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 83 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 84 | * WARNING: if you add a flag here you need to make sure it's |
| 85 | * accounted for in minijail_pre{enter|exec}() below. |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 86 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 87 | struct { |
| 88 | int uid:1; |
| 89 | int gid:1; |
| 90 | int caps:1; |
| 91 | int vfs:1; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 92 | int enter_vfs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 93 | int pids:1; |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 94 | int ipc:1; |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 95 | int net:1; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 96 | int enter_net:1; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 97 | int userns:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 98 | int seccomp:1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 99 | int remount_proc_ro:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 100 | int usergroups:1; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 101 | int no_new_privs:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 102 | int seccomp_filter:1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 103 | int log_seccomp_filter:1; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 104 | int chroot:1; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 105 | int pivot_root:1; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 106 | int mount_tmp:1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 107 | int do_init:1; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 108 | int pid_file:1; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 109 | int alt_syscall:1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 110 | } flags; |
| 111 | uid_t uid; |
| 112 | gid_t gid; |
| 113 | gid_t usergid; |
| 114 | char *user; |
| 115 | uint64_t caps; |
| 116 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 117 | int mountns_fd; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 118 | int netns_fd; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 119 | int filter_len; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 120 | char *chrootdir; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 121 | char *pid_file_path; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 122 | char *uidmap; |
| 123 | char *gidmap; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 124 | char *alt_syscall_table; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 125 | struct sock_fprog *filter_prog; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 126 | struct mountpoint *mounts_head; |
| 127 | struct mountpoint *mounts_tail; |
| 128 | int mounts_count; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 129 | }; |
| 130 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 131 | /* |
| 132 | * Strip out flags meant for the parent. |
| 133 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 134 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 135 | */ |
| 136 | void minijail_preenter(struct minijail *j) |
| 137 | { |
| 138 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 139 | j->flags.enter_vfs = 0; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 140 | j->flags.remount_proc_ro = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 141 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 142 | j->flags.do_init = 0; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 143 | j->flags.pid_file = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Strip out flags meant for the child. |
| 148 | * We keep things that are inherited across execve(2). |
| 149 | */ |
| 150 | void minijail_preexec(struct minijail *j) |
| 151 | { |
| 152 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 153 | int enter_vfs = j->flags.enter_vfs; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 154 | int remount_proc_ro = j->flags.remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 155 | int userns = j->flags.userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 156 | if (j->user) |
| 157 | free(j->user); |
| 158 | j->user = NULL; |
| 159 | memset(&j->flags, 0, sizeof(j->flags)); |
| 160 | /* Now restore anything we meant to keep. */ |
| 161 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 162 | j->flags.enter_vfs = enter_vfs; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 163 | j->flags.remount_proc_ro = remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 164 | j->flags.userns = userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 165 | /* Note, |pids| will already have been used before this call. */ |
| 166 | } |
| 167 | |
| 168 | /* Minijail API. */ |
| 169 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 170 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 171 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 172 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 175 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 176 | { |
| 177 | if (uid == 0) |
| 178 | die("useless change to uid 0"); |
| 179 | j->uid = uid; |
| 180 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 183 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 184 | { |
| 185 | if (gid == 0) |
| 186 | die("useless change to gid 0"); |
| 187 | j->gid = gid; |
| 188 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 191 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 192 | { |
| 193 | char *buf = NULL; |
| 194 | struct passwd pw; |
| 195 | struct passwd *ppw = NULL; |
| 196 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 197 | if (sz == -1) |
| 198 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 199 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 200 | /* |
| 201 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 202 | * the maximum needed size of the buffer, so we don't have to search. |
| 203 | */ |
| 204 | buf = malloc(sz); |
| 205 | if (!buf) |
| 206 | return -ENOMEM; |
| 207 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 208 | /* |
| 209 | * We're safe to free the buffer here. The strings inside pw point |
| 210 | * inside buf, but we don't use any of them; this leaves the pointers |
| 211 | * dangling but it's safe. ppw points at pw if getpwnam_r succeeded. |
| 212 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 213 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 214 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 215 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 216 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 217 | minijail_change_uid(j, ppw->pw_uid); |
| 218 | j->user = strdup(user); |
| 219 | if (!j->user) |
| 220 | return -ENOMEM; |
| 221 | j->usergid = ppw->pw_gid; |
| 222 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 225 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 226 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 227 | char *buf = NULL; |
Yabin Cui | 1b21c8f | 2015-07-22 10:34:45 -0700 | [diff] [blame] | 228 | struct group gr; |
| 229 | struct group *pgr = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 230 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 231 | if (sz == -1) |
| 232 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 233 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 234 | /* |
| 235 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 236 | * the maximum needed size of the buffer, so we don't have to search. |
| 237 | */ |
| 238 | buf = malloc(sz); |
| 239 | if (!buf) |
| 240 | return -ENOMEM; |
| 241 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 242 | /* |
| 243 | * We're safe to free the buffer here. The strings inside gr point |
| 244 | * inside buf, but we don't use any of them; this leaves the pointers |
| 245 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 246 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 247 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 248 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 249 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 250 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 251 | minijail_change_gid(j, pgr->gr_gid); |
| 252 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 255 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 256 | { |
| 257 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 260 | void API minijail_no_new_privs(struct minijail *j) |
| 261 | { |
| 262 | j->flags.no_new_privs = 1; |
| 263 | } |
| 264 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 265 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 266 | { |
| 267 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 270 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 271 | { |
| 272 | j->flags.log_seccomp_filter = 1; |
| 273 | } |
| 274 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 275 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 276 | { |
| 277 | j->caps = capmask; |
| 278 | j->flags.caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 281 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 282 | { |
| 283 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 286 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 287 | { |
| 288 | int ns_fd = open(ns_path, O_RDONLY); |
| 289 | if (ns_fd < 0) { |
| 290 | pdie("failed to open namespace '%s'", ns_path); |
| 291 | } |
| 292 | j->mountns_fd = ns_fd; |
| 293 | j->flags.enter_vfs = 1; |
| 294 | } |
| 295 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 296 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 297 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 298 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 299 | j->flags.remount_proc_ro = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 300 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 301 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 302 | } |
| 303 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 304 | void API minijail_namespace_ipc(struct minijail *j) |
| 305 | { |
| 306 | j->flags.ipc = 1; |
| 307 | } |
| 308 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 309 | void API minijail_namespace_net(struct minijail *j) |
| 310 | { |
| 311 | j->flags.net = 1; |
| 312 | } |
| 313 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 314 | void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path) |
| 315 | { |
| 316 | int ns_fd = open(ns_path, O_RDONLY); |
| 317 | if (ns_fd < 0) { |
| 318 | pdie("failed to open namespace '%s'", ns_path); |
| 319 | } |
| 320 | j->netns_fd = ns_fd; |
| 321 | j->flags.enter_net = 1; |
| 322 | } |
| 323 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 324 | void API minijail_remount_proc_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 325 | { |
| 326 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 327 | j->flags.remount_proc_ro = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 330 | void API minijail_namespace_user(struct minijail *j) |
| 331 | { |
| 332 | j->flags.userns = 1; |
| 333 | } |
| 334 | |
| 335 | int API minijail_uidmap(struct minijail *j, const char *uidmap) |
| 336 | { |
| 337 | j->uidmap = strdup(uidmap); |
| 338 | if (!j->uidmap) |
| 339 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 340 | char *ch; |
| 341 | for (ch = j->uidmap; *ch; ch++) { |
| 342 | if (*ch == ',') |
| 343 | *ch = '\n'; |
| 344 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int API minijail_gidmap(struct minijail *j, const char *gidmap) |
| 349 | { |
| 350 | j->gidmap = strdup(gidmap); |
| 351 | if (!j->gidmap) |
| 352 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 353 | char *ch; |
| 354 | for (ch = j->gidmap; *ch; ch++) { |
| 355 | if (*ch == ',') |
| 356 | *ch = '\n'; |
| 357 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 361 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 362 | { |
| 363 | j->flags.usergroups = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 364 | } |
| 365 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 366 | void API minijail_run_as_init(struct minijail *j) |
| 367 | { |
| 368 | /* |
| 369 | * Since the jailed program will become 'init' in the new PID namespace, |
| 370 | * Minijail does not need to fork an 'init' process. |
| 371 | */ |
| 372 | j->flags.do_init = 0; |
| 373 | } |
| 374 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 375 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 376 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 377 | if (j->chrootdir) |
| 378 | return -EINVAL; |
| 379 | j->chrootdir = strdup(dir); |
| 380 | if (!j->chrootdir) |
| 381 | return -ENOMEM; |
| 382 | j->flags.chroot = 1; |
| 383 | return 0; |
| 384 | } |
| 385 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 386 | int API minijail_enter_pivot_root(struct minijail *j, const char *dir) |
| 387 | { |
| 388 | if (j->chrootdir) |
| 389 | return -EINVAL; |
| 390 | j->chrootdir = strdup(dir); |
| 391 | if (!j->chrootdir) |
| 392 | return -ENOMEM; |
| 393 | j->flags.pivot_root = 1; |
| 394 | return 0; |
| 395 | } |
| 396 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 397 | static char *append_external_path(const char *external_path, |
| 398 | const char *path_inside_chroot) |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 399 | { |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 400 | char *path; |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 401 | size_t pathlen; |
| 402 | |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 403 | /* One extra char for '/' and one for '\0', hence + 2. */ |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 404 | pathlen = strlen(path_inside_chroot) + strlen(external_path) + 2; |
| 405 | path = malloc(pathlen); |
| 406 | snprintf(path, pathlen, "%s/%s", external_path, path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 407 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 408 | return path; |
| 409 | } |
| 410 | |
| 411 | char API *minijail_get_original_path(struct minijail *j, |
| 412 | const char *path_inside_chroot) |
| 413 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 414 | struct mountpoint *b; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 415 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 416 | b = j->mounts_head; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 417 | while (b) { |
| 418 | /* |
| 419 | * If |path_inside_chroot| is the exact destination of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 420 | * mount, then the original path is exactly the source of |
| 421 | * the mount. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 422 | * for example: "-b /some/path/exe,/chroot/path/exe" |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 423 | * mount source = /some/path/exe, mount dest = |
| 424 | * /chroot/path/exe Then when getting the original path of |
| 425 | * "/chroot/path/exe", the source of that mount, |
| 426 | * "/some/path/exe" is what should be returned. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 427 | */ |
| 428 | if (!strcmp(b->dest, path_inside_chroot)) |
| 429 | return strdup(b->src); |
| 430 | |
| 431 | /* |
| 432 | * If |path_inside_chroot| is within the destination path of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 433 | * mount, take the suffix of the chroot path relative to the |
| 434 | * mount destination path, and append it to the mount source |
| 435 | * path. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 436 | */ |
| 437 | if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) { |
| 438 | const char *relative_path = |
| 439 | path_inside_chroot + strlen(b->dest); |
| 440 | return append_external_path(b->src, relative_path); |
| 441 | } |
| 442 | b = b->next; |
| 443 | } |
| 444 | |
| 445 | /* If there is a chroot path, append |path_inside_chroot| to that. */ |
| 446 | if (j->chrootdir) |
| 447 | return append_external_path(j->chrootdir, path_inside_chroot); |
| 448 | |
| 449 | /* No chroot, so the path outside is the same as it is inside. */ |
| 450 | return strdup(path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 453 | void API minijail_mount_tmp(struct minijail *j) |
| 454 | { |
| 455 | j->flags.mount_tmp = 1; |
| 456 | } |
| 457 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 458 | int API minijail_write_pid_file(struct minijail *j, const char *path) |
| 459 | { |
| 460 | j->pid_file_path = strdup(path); |
| 461 | if (!j->pid_file_path) |
| 462 | return -ENOMEM; |
| 463 | j->flags.pid_file = 1; |
| 464 | return 0; |
| 465 | } |
| 466 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 467 | int API minijail_mount(struct minijail *j, const char *src, const char *dest, |
| 468 | const char *type, unsigned long flags) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 469 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 470 | struct mountpoint *m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 471 | |
| 472 | if (*dest != '/') |
| 473 | return -EINVAL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 474 | m = calloc(1, sizeof(*m)); |
| 475 | if (!m) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 476 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 477 | m->dest = strdup(dest); |
| 478 | if (!m->dest) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 479 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 480 | m->src = strdup(src); |
| 481 | if (!m->src) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 482 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 483 | m->type = strdup(type); |
| 484 | if (!m->type) |
| 485 | goto error; |
| 486 | m->flags = flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 487 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 488 | info("mount %s -> %s type %s", src, dest, type); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 489 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 490 | /* |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 491 | * Force vfs namespacing so the mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 492 | * containing vfs namespace. |
| 493 | */ |
| 494 | minijail_namespace_vfs(j); |
| 495 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 496 | if (j->mounts_tail) |
| 497 | j->mounts_tail->next = m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 498 | else |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 499 | j->mounts_head = m; |
| 500 | j->mounts_tail = m; |
| 501 | j->mounts_count++; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 502 | |
| 503 | return 0; |
| 504 | |
| 505 | error: |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 506 | free(m->src); |
| 507 | free(m->dest); |
| 508 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 509 | return -ENOMEM; |
| 510 | } |
| 511 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 512 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 513 | int writeable) |
| 514 | { |
| 515 | unsigned long flags = MS_BIND; |
| 516 | |
| 517 | if (!writeable) |
| 518 | flags |= MS_RDONLY; |
| 519 | |
| 520 | return minijail_mount(j, src, dest, "", flags); |
| 521 | } |
| 522 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 523 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 524 | { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 525 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL)) { |
| 526 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 527 | warn("not loading seccomp filter, seccomp not supported"); |
| 528 | return; |
| 529 | } |
| 530 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 531 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 532 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 533 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 534 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 535 | |
| 536 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 537 | if (compile_filter(file, fprog, j->flags.log_seccomp_filter)) { |
| 538 | die("failed to compile seccomp filter BPF program in '%s'", |
| 539 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | j->filter_len = fprog->len; |
| 543 | j->filter_prog = fprog; |
| 544 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 545 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 548 | int API minijail_use_alt_syscall(struct minijail *j, const char *table) |
| 549 | { |
| 550 | j->alt_syscall_table = strdup(table); |
| 551 | if (!j->alt_syscall_table) |
| 552 | return -ENOMEM; |
| 553 | j->flags.alt_syscall = 1; |
| 554 | return 0; |
| 555 | } |
| 556 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 557 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 558 | size_t available; |
| 559 | size_t total; |
| 560 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 561 | }; |
| 562 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 563 | void marshal_state_init(struct marshal_state *state, |
| 564 | char *buf, size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 565 | { |
| 566 | state->available = available; |
| 567 | state->buf = buf; |
| 568 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 571 | void marshal_append(struct marshal_state *state, |
| 572 | char *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 573 | { |
| 574 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 575 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 576 | /* Up to |available| will be written. */ |
| 577 | if (copy_len) { |
| 578 | memcpy(state->buf, src, copy_len); |
| 579 | state->buf += copy_len; |
| 580 | state->available -= copy_len; |
| 581 | } |
| 582 | /* |total| will contain the expected length. */ |
| 583 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 584 | } |
| 585 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 586 | void minijail_marshal_helper(struct marshal_state *state, |
| 587 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 588 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 589 | struct mountpoint *m = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 590 | marshal_append(state, (char *)j, sizeof(*j)); |
| 591 | if (j->user) |
| 592 | marshal_append(state, j->user, strlen(j->user) + 1); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 593 | if (j->chrootdir) |
| 594 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 595 | if (j->alt_syscall_table) { |
| 596 | marshal_append(state, j->alt_syscall_table, |
| 597 | strlen(j->alt_syscall_table) + 1); |
| 598 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 599 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 600 | struct sock_fprog *fp = j->filter_prog; |
| 601 | marshal_append(state, (char *)fp->filter, |
| 602 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 603 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 604 | for (m = j->mounts_head; m; m = m->next) { |
| 605 | marshal_append(state, m->src, strlen(m->src) + 1); |
| 606 | marshal_append(state, m->dest, strlen(m->dest) + 1); |
| 607 | marshal_append(state, m->type, strlen(m->type) + 1); |
| 608 | marshal_append(state, (char *)&m->flags, sizeof(m->flags)); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 609 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 610 | } |
| 611 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 612 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 613 | { |
| 614 | struct marshal_state state; |
| 615 | marshal_state_init(&state, NULL, 0); |
| 616 | minijail_marshal_helper(&state, j); |
| 617 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 618 | } |
| 619 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 620 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 621 | { |
| 622 | struct marshal_state state; |
| 623 | marshal_state_init(&state, buf, available); |
| 624 | minijail_marshal_helper(&state, j); |
| 625 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 626 | } |
| 627 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame^] | 628 | /* |
| 629 | * consumebytes: consumes @length bytes from a buffer @buf of length @buflength |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 630 | * @length Number of bytes to consume |
| 631 | * @buf Buffer to consume from |
| 632 | * @buflength Size of @buf |
| 633 | * |
| 634 | * Returns a pointer to the base of the bytes, or NULL for errors. |
| 635 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 636 | void *consumebytes(size_t length, char **buf, size_t *buflength) |
| 637 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 638 | char *p = *buf; |
| 639 | if (length > *buflength) |
| 640 | return NULL; |
| 641 | *buf += length; |
| 642 | *buflength -= length; |
| 643 | return p; |
| 644 | } |
| 645 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame^] | 646 | /* |
| 647 | * consumestr: consumes a C string from a buffer @buf of length @length |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 648 | * @buf Buffer to consume |
| 649 | * @length Length of buffer |
| 650 | * |
| 651 | * Returns a pointer to the base of the string, or NULL for errors. |
| 652 | */ |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 653 | char *consumestr(char **buf, size_t *buflength) |
| 654 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 655 | size_t len = strnlen(*buf, *buflength); |
| 656 | if (len == *buflength) |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 657 | /* There's no null-terminator. */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 658 | return NULL; |
| 659 | return consumebytes(len + 1, buf, buflength); |
| 660 | } |
| 661 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 662 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 663 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 664 | int i; |
| 665 | int count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 666 | int ret = -EINVAL; |
| 667 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 668 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 669 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 670 | memcpy((void *)j, serialized, sizeof(*j)); |
| 671 | serialized += sizeof(*j); |
| 672 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 673 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 674 | /* Potentially stale pointers not used as signals. */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 675 | j->mounts_head = NULL; |
| 676 | j->mounts_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 677 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 678 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 679 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 680 | char *user = consumestr(&serialized, &length); |
| 681 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 682 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 683 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 684 | if (!j->user) |
| 685 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 686 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 687 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 688 | if (j->chrootdir) { /* stale pointer */ |
| 689 | char *chrootdir = consumestr(&serialized, &length); |
| 690 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 691 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 692 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 693 | if (!j->chrootdir) |
| 694 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 695 | } |
| 696 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 697 | if (j->alt_syscall_table) { /* stale pointer */ |
| 698 | char *alt_syscall_table = consumestr(&serialized, &length); |
| 699 | if (!alt_syscall_table) |
| 700 | goto bad_syscall_table; |
| 701 | j->alt_syscall_table = strdup(alt_syscall_table); |
| 702 | if (!j->alt_syscall_table) |
| 703 | goto bad_syscall_table; |
| 704 | } |
| 705 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 706 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 707 | size_t ninstrs = j->filter_len; |
| 708 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 709 | ninstrs > USHRT_MAX) |
| 710 | goto bad_filters; |
| 711 | |
| 712 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 713 | void *program = consumebytes(program_len, &serialized, &length); |
| 714 | if (!program) |
| 715 | goto bad_filters; |
| 716 | |
| 717 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
| 718 | j->filter_prog->len = ninstrs; |
| 719 | j->filter_prog->filter = malloc(program_len); |
| 720 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 721 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 722 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 723 | count = j->mounts_count; |
| 724 | j->mounts_count = 0; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 725 | for (i = 0; i < count; ++i) { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 726 | unsigned long *flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 727 | const char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 728 | const char *type; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 729 | const char *src = consumestr(&serialized, &length); |
| 730 | if (!src) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 731 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 732 | dest = consumestr(&serialized, &length); |
| 733 | if (!dest) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 734 | goto bad_mounts; |
| 735 | type = consumestr(&serialized, &length); |
| 736 | if (!type) |
| 737 | goto bad_mounts; |
| 738 | flags = consumebytes(sizeof(*flags), &serialized, &length); |
| 739 | if (!flags) |
| 740 | goto bad_mounts; |
| 741 | if (minijail_mount(j, src, dest, type, *flags)) |
| 742 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 745 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 746 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 747 | bad_mounts: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 748 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 749 | free(j->filter_prog->filter); |
| 750 | free(j->filter_prog); |
| 751 | } |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 752 | bad_filters: |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 753 | if (j->alt_syscall_table) |
| 754 | free(j->alt_syscall_table); |
| 755 | bad_syscall_table: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 756 | if (j->chrootdir) |
| 757 | free(j->chrootdir); |
| 758 | bad_chrootdir: |
| 759 | if (j->user) |
| 760 | free(j->user); |
| 761 | clear_pointers: |
| 762 | j->user = NULL; |
| 763 | j->chrootdir = NULL; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 764 | j->alt_syscall_table = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 765 | out: |
| 766 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 767 | } |
| 768 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 769 | static void write_ugid_mappings(const struct minijail *j, int *pipe_fds) |
| 770 | { |
| 771 | int fd, ret, len; |
| 772 | size_t sz; |
| 773 | char fname[32]; |
| 774 | close(pipe_fds[0]); |
| 775 | |
| 776 | sz = sizeof(fname); |
| 777 | if (j->uidmap) { |
| 778 | ret = snprintf(fname, sz, "/proc/%d/uid_map", j->initpid); |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 779 | if (ret < 0 || (size_t)ret >= sz) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 780 | die("failed to write file name of uid_map"); |
| 781 | fd = open(fname, O_WRONLY); |
| 782 | if (fd < 0) |
| 783 | pdie("failed to open '%s'", fname); |
| 784 | len = strlen(j->uidmap); |
| 785 | if (write(fd, j->uidmap, len) < len) |
| 786 | die("failed to set uid_map"); |
| 787 | close(fd); |
| 788 | } |
| 789 | if (j->gidmap) { |
| 790 | ret = snprintf(fname, sz, "/proc/%d/gid_map", j->initpid); |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 791 | if (ret < 0 || (size_t)ret >= sz) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 792 | die("failed to write file name of gid_map"); |
| 793 | fd = open(fname, O_WRONLY); |
| 794 | if (fd < 0) |
| 795 | pdie("failed to open '%s'", fname); |
| 796 | len = strlen(j->gidmap); |
| 797 | if (write(fd, j->gidmap, len) < len) |
| 798 | die("failed to set gid_map"); |
| 799 | close(fd); |
| 800 | } |
| 801 | |
| 802 | close(pipe_fds[1]); |
| 803 | } |
| 804 | |
| 805 | static void enter_user_namespace(const struct minijail *j, int *pipe_fds) |
| 806 | { |
| 807 | char buf; |
| 808 | |
| 809 | close(pipe_fds[1]); |
| 810 | |
| 811 | /* Wait for parent to set up uid/gid mappings. */ |
| 812 | if (read(pipe_fds[0], &buf, 1) != 0) |
| 813 | die("failed to sync with parent"); |
| 814 | close(pipe_fds[0]); |
| 815 | |
| 816 | if (j->uidmap && setresuid(0, 0, 0)) |
| 817 | pdie("setresuid"); |
| 818 | if (j->gidmap && setresgid(0, 0, 0)) |
| 819 | pdie("setresgid"); |
| 820 | } |
| 821 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame^] | 822 | /* |
| 823 | * mount_one: Applies mounts from @m for @j, recursing as needed. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 824 | * @j Minijail these mounts are for |
| 825 | * @m Head of list of mounts |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 826 | * |
| 827 | * Returns 0 for success. |
| 828 | */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 829 | static int mount_one(const struct minijail *j, struct mountpoint *m) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 830 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 831 | int ret; |
| 832 | char *dest; |
| 833 | int remount_ro = 0; |
| 834 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 835 | /* dest has a leading "/" */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 836 | if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 837 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * R/O bind mounts have to be remounted since bind and ro can't both be |
| 841 | * specified in the original bind mount. Remount R/O after the initial |
| 842 | * mount. |
| 843 | */ |
| 844 | if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) { |
| 845 | remount_ro = 1; |
| 846 | m->flags &= ~MS_RDONLY; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 847 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 848 | |
| 849 | ret = mount(m->src, dest, m->type, m->flags, NULL); |
| 850 | if (ret) |
| 851 | pdie("mount: %s -> %s", m->src, dest); |
| 852 | |
| 853 | if (remount_ro) { |
| 854 | m->flags |= MS_RDONLY; |
| 855 | ret = mount(m->src, dest, NULL, |
| 856 | m->flags | MS_REMOUNT, NULL); |
| 857 | if (ret) |
| 858 | pdie("bind ro: %s -> %s", m->src, dest); |
| 859 | } |
| 860 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 861 | free(dest); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 862 | if (m->next) |
| 863 | return mount_one(j, m->next); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 864 | return ret; |
| 865 | } |
| 866 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 867 | int enter_chroot(const struct minijail *j) |
| 868 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 869 | int ret; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 870 | |
| 871 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 872 | return ret; |
| 873 | |
| 874 | if (chroot(j->chrootdir)) |
| 875 | return -errno; |
| 876 | |
| 877 | if (chdir("/")) |
| 878 | return -errno; |
| 879 | |
| 880 | return 0; |
| 881 | } |
| 882 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 883 | int enter_pivot_root(const struct minijail *j) |
| 884 | { |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 885 | int ret, oldroot, newroot; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 886 | |
| 887 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 888 | return ret; |
| 889 | |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 890 | /* Keep the fd for both old and new root. It will be used in fchdir later. */ |
| 891 | oldroot = open("/", O_DIRECTORY | O_RDONLY); |
| 892 | if (oldroot < 0) |
| 893 | pdie("failed to open / for fchdir"); |
| 894 | newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY); |
| 895 | if (newroot < 0) |
| 896 | pdie("failed to open %s for fchdir", j->chrootdir); |
| 897 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 898 | /* To ensure chrootdir is the root of a file system, do a self bind mount. */ |
| 899 | if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, "")) |
| 900 | pdie("failed to bind mount '%s'", j->chrootdir); |
| 901 | if (chdir(j->chrootdir)) |
| 902 | return -errno; |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 903 | if (syscall(SYS_pivot_root, ".", ".")) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 904 | pdie("pivot_root"); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 905 | |
| 906 | /* |
| 907 | * Now the old root is mounted on top of the new root. Use fchdir to |
| 908 | * change to the old root and unmount it. |
| 909 | */ |
| 910 | if (fchdir(oldroot)) |
| 911 | pdie("failed to fchdir to old /"); |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 912 | /* The old root might be busy, so use lazy unmount. */ |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 913 | if (umount2(".", MNT_DETACH)) |
| 914 | pdie("umount(/)"); |
| 915 | /* Change back to the new root. */ |
| 916 | if (fchdir(newroot)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 917 | return -errno; |
| 918 | if (chroot("/")) |
| 919 | return -errno; |
Jorge Lucangeli Obes | 46a5509 | 2015-10-12 15:31:59 -0700 | [diff] [blame] | 920 | /* Set correct CWD for getcwd(3). */ |
| 921 | if (chdir("/")) |
| 922 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 927 | int mount_tmp(void) |
| 928 | { |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 929 | return mount("none", "/tmp", "tmpfs", 0, "size=64M,mode=777"); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 932 | int remount_proc_readonly(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 933 | { |
| 934 | const char *kProcPath = "/proc"; |
| 935 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 936 | /* |
| 937 | * Right now, we're holding a reference to our parent's old mount of |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 938 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 939 | * mutate our parent's mount as well, even though we're in a VFS |
| 940 | * namespace (!). Instead, remove their mount from our namespace |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 941 | * and make our own. However, if we are in a new user namespace, /proc |
| 942 | * is not seen as mounted, so don't return error if umount() fails. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 943 | */ |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 944 | if (umount2(kProcPath, MNT_DETACH) && !j->flags.userns) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 945 | return -errno; |
| 946 | if (mount("", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
| 947 | return -errno; |
| 948 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 949 | } |
| 950 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 951 | static void write_pid_file(const struct minijail *j) |
| 952 | { |
| 953 | FILE *fp = fopen(j->pid_file_path, "w"); |
| 954 | |
| 955 | if (!fp) |
| 956 | pdie("failed to open '%s'", j->pid_file_path); |
| 957 | if (fprintf(fp, "%d\n", (int)j->initpid) < 0) |
| 958 | pdie("fprintf(%s)", j->pid_file_path); |
| 959 | if (fclose(fp)) |
| 960 | pdie("fclose(%s)", j->pid_file_path); |
| 961 | } |
| 962 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 963 | void drop_ugid(const struct minijail *j) |
| 964 | { |
| 965 | if (j->flags.usergroups) { |
| 966 | if (initgroups(j->user, j->usergid)) |
| 967 | pdie("initgroups"); |
| 968 | } else { |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame^] | 969 | /* |
| 970 | * Only attempt to clear supplemental groups if we are changing |
| 971 | * users. |
| 972 | */ |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 973 | if ((j->uid || j->gid) && setgroups(0, NULL)) |
| 974 | pdie("setgroups"); |
| 975 | } |
| 976 | |
| 977 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
| 978 | pdie("setresgid"); |
| 979 | |
| 980 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
| 981 | pdie("setresuid"); |
| 982 | } |
| 983 | |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 984 | /* |
| 985 | * We specifically do not use cap_valid() as that only tells us the last |
| 986 | * valid cap we were *compiled* against (i.e. what the version of kernel |
| 987 | * headers says). If we run on a different kernel version, then it's not |
| 988 | * uncommon for that to be less (if an older kernel) or more (if a newer |
| 989 | * kernel). So suck up the answer via /proc. |
| 990 | */ |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 991 | static unsigned int get_last_valid_cap() |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 992 | { |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 993 | const char cap_file[] = "/proc/sys/kernel/cap_last_cap"; |
| 994 | FILE *fp = fopen(cap_file, "re"); |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 995 | unsigned int last_valid_cap; |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 996 | |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 997 | if (fscanf(fp, "%u", &last_valid_cap) != 1) |
| 998 | pdie("fscanf(%s)", cap_file); |
| 999 | fclose(fp); |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1000 | |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1001 | return last_valid_cap; |
Mike Frysinger | 3adfef7 | 2013-05-09 17:19:08 -0400 | [diff] [blame] | 1002 | } |
| 1003 | |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 1004 | void drop_caps(const struct minijail *j, unsigned int last_valid_cap) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1005 | { |
| 1006 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1007 | cap_value_t flag[1]; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1008 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1009 | unsigned int i; |
| 1010 | if (!caps) |
| 1011 | die("can't get process caps"); |
| 1012 | if (cap_clear_flag(caps, CAP_INHERITABLE)) |
| 1013 | die("can't clear inheritable caps"); |
| 1014 | if (cap_clear_flag(caps, CAP_EFFECTIVE)) |
| 1015 | die("can't clear effective caps"); |
| 1016 | if (cap_clear_flag(caps, CAP_PERMITTED)) |
| 1017 | die("can't clear permitted caps"); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1018 | for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1019 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1020 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1021 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1022 | flag[0] = i; |
| 1023 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1024 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1025 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1026 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1027 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1028 | die("can't add inheritable cap"); |
| 1029 | } |
| 1030 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1031 | die("can't apply initial cleaned capset"); |
| 1032 | |
| 1033 | /* |
| 1034 | * Instead of dropping bounding set first, do it here in case |
| 1035 | * the caller had a more permissive bounding set which could |
| 1036 | * have been used above to raise a capability that wasn't already |
| 1037 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 1038 | */ |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1039 | for (i = 0; i < sizeof(j->caps) * 8 && i <= last_valid_cap; ++i) { |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1040 | if (j->caps & (one << i)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1041 | continue; |
| 1042 | if (prctl(PR_CAPBSET_DROP, i)) |
| 1043 | pdie("prctl(PR_CAPBSET_DROP)"); |
| 1044 | } |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1045 | |
| 1046 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1047 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1048 | flag[0] = CAP_SETPCAP; |
| 1049 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 1050 | die("can't clear effective cap"); |
| 1051 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 1052 | die("can't clear permitted cap"); |
| 1053 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 1054 | die("can't clear inheritable cap"); |
| 1055 | } |
| 1056 | |
| 1057 | if (cap_set_proc(caps)) |
| 1058 | die("can't apply final cleaned capset"); |
| 1059 | |
| 1060 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1061 | } |
| 1062 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1063 | void set_seccomp_filter(const struct minijail *j) |
| 1064 | { |
| 1065 | /* |
| 1066 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 1067 | * in the kernel source tree for an explanation of the parameters. |
| 1068 | */ |
| 1069 | if (j->flags.no_new_privs) { |
| 1070 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 1071 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | * If we're logging seccomp filter failures, |
| 1076 | * install the SIGSYS handler first. |
| 1077 | */ |
| 1078 | if (j->flags.seccomp_filter && j->flags.log_seccomp_filter) { |
| 1079 | if (install_sigsys_handler()) |
| 1080 | pdie("install SIGSYS handler"); |
| 1081 | warn("logging seccomp filter failures"); |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * Install the syscall filter. |
| 1086 | */ |
| 1087 | if (j->flags.seccomp_filter) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1088 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, j->filter_prog)) { |
| 1089 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 1090 | warn("seccomp not supported"); |
| 1091 | return; |
| 1092 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1093 | pdie("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1094 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1098 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1099 | { |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1100 | /* |
| 1101 | * Get the last valid cap from /proc, since /proc can be unmounted |
| 1102 | * before drop_caps(). |
| 1103 | */ |
Jorge Lucangeli Obes | 2034274 | 2015-10-27 11:39:59 -0700 | [diff] [blame] | 1104 | unsigned int last_valid_cap = get_last_valid_cap(); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1105 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1106 | if (j->flags.pids) |
| 1107 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1108 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1109 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1110 | if (j->flags.usergroups && !j->user) |
| 1111 | die("usergroup inheritance without username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1112 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1113 | /* |
| 1114 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1115 | * so we don't even try. If any of our operations fail, we abort() the |
| 1116 | * entire process. |
| 1117 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1118 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
| 1119 | pdie("setns(CLONE_NEWNS)"); |
| 1120 | |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 1121 | if (j->flags.vfs) { |
| 1122 | if (unshare(CLONE_NEWNS)) |
| 1123 | pdie("unshare(vfs)"); |
| 1124 | /* |
| 1125 | * Remount all filesystems as private. If they are shared |
| 1126 | * new bind mounts will creep out of our namespace. |
| 1127 | * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt |
| 1128 | */ |
| 1129 | if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) |
| 1130 | pdie("mount(/, private)"); |
| 1131 | } |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 1132 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 1133 | if (j->flags.ipc && unshare(CLONE_NEWIPC)) { |
| 1134 | pdie("unshare(ipc)"); |
| 1135 | } |
| 1136 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1137 | if (j->flags.enter_net) { |
| 1138 | if (setns(j->netns_fd, CLONE_NEWNET)) |
| 1139 | pdie("setns(CLONE_NEWNET)"); |
| 1140 | } else if (j->flags.net && unshare(CLONE_NEWNET)) { |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 1141 | pdie("unshare(net)"); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1142 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1143 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1144 | if (j->flags.chroot && enter_chroot(j)) |
| 1145 | pdie("chroot"); |
| 1146 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1147 | if (j->flags.pivot_root && enter_pivot_root(j)) |
| 1148 | pdie("pivot_root"); |
| 1149 | |
Jorge Lucangeli Obes | 3901da6 | 2015-03-03 13:55:11 -0800 | [diff] [blame] | 1150 | if (j->flags.mount_tmp && mount_tmp()) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1151 | pdie("mount_tmp"); |
| 1152 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1153 | if (j->flags.remount_proc_ro && remount_proc_readonly(j)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1154 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1155 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1156 | if (j->flags.caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1157 | /* |
| 1158 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1159 | * capability to change uids, our attempt to use setuid() |
| 1160 | * below will fail. Hang on to root caps across setuid(), then |
| 1161 | * lock securebits. |
| 1162 | */ |
| 1163 | if (prctl(PR_SET_KEEPCAPS, 1)) |
| 1164 | pdie("prctl(PR_SET_KEEPCAPS)"); |
| 1165 | if (prctl |
| 1166 | (PR_SET_SECUREBITS, SECURE_ALL_BITS | SECURE_ALL_LOCKS)) |
| 1167 | pdie("prctl(PR_SET_SECUREBITS)"); |
| 1168 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1169 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1170 | /* |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1171 | * If we're setting no_new_privs, we can drop privileges |
| 1172 | * before setting seccomp filter. This way filter policies |
| 1173 | * don't need to allow privilege-dropping syscalls. |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1174 | */ |
| 1175 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1176 | drop_ugid(j); |
| 1177 | if (j->flags.caps) |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1178 | drop_caps(j, last_valid_cap); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1179 | |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1180 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1181 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1182 | /* |
| 1183 | * If we're not setting no_new_privs, |
| 1184 | * we need to set seccomp filter *before* dropping privileges. |
| 1185 | * WARNING: this means that filter policies *must* allow |
| 1186 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 1187 | * capget()/capset()/prctl() for dropping caps. |
| 1188 | */ |
| 1189 | set_seccomp_filter(j); |
| 1190 | |
| 1191 | drop_ugid(j); |
| 1192 | if (j->flags.caps) |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1193 | drop_caps(j, last_valid_cap); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1194 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1195 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1196 | /* |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1197 | * Select the specified alternate syscall table. The table must not |
| 1198 | * block prctl(2) if we're using seccomp as well. |
| 1199 | */ |
| 1200 | if (j->flags.alt_syscall) { |
| 1201 | if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table)) |
| 1202 | pdie("prctl(PR_ALT_SYSCALL)"); |
| 1203 | } |
| 1204 | |
| 1205 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1206 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1207 | * privilege-dropping syscalls :) |
| 1208 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1209 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
| 1210 | if ((errno == ENOSYS) && SECCOMP_SOFTFAIL) { |
| 1211 | warn("seccomp not supported"); |
| 1212 | return; |
| 1213 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1214 | pdie("prctl(PR_SET_SECCOMP)"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1215 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1216 | } |
| 1217 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1218 | /* TODO(wad) will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1219 | static int init_exitstatus = 0; |
| 1220 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1221 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1222 | { |
| 1223 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1224 | } |
| 1225 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1226 | int init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1227 | { |
| 1228 | pid_t pid; |
| 1229 | int status; |
| 1230 | /* so that we exit with the right status */ |
| 1231 | signal(SIGTERM, init_term); |
| 1232 | /* TODO(wad) self jail with seccomp_filters here. */ |
| 1233 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1234 | /* |
| 1235 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1236 | * left inside our pid namespace or we get a signal. |
| 1237 | */ |
| 1238 | if (pid == rootpid) |
| 1239 | init_exitstatus = status; |
| 1240 | } |
| 1241 | if (!WIFEXITED(init_exitstatus)) |
| 1242 | _exit(MINIJAIL_ERR_INIT); |
| 1243 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1246 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1247 | { |
| 1248 | size_t sz = 0; |
| 1249 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 1250 | char *buf; |
| 1251 | int r; |
| 1252 | if (sizeof(sz) != bytes) |
| 1253 | return -EINVAL; |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1254 | if (sz > USHRT_MAX) /* arbitrary sanity check */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1255 | return -E2BIG; |
| 1256 | buf = malloc(sz); |
| 1257 | if (!buf) |
| 1258 | return -ENOMEM; |
| 1259 | bytes = read(fd, buf, sz); |
| 1260 | if (bytes != sz) { |
| 1261 | free(buf); |
| 1262 | return -EINVAL; |
| 1263 | } |
| 1264 | r = minijail_unmarshal(j, buf, sz); |
| 1265 | free(buf); |
| 1266 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1267 | } |
| 1268 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1269 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1270 | { |
| 1271 | char *buf; |
| 1272 | size_t sz = minijail_size(j); |
| 1273 | ssize_t written; |
| 1274 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1275 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1276 | if (!sz) |
| 1277 | return -EINVAL; |
| 1278 | buf = malloc(sz); |
| 1279 | r = minijail_marshal(j, buf, sz); |
| 1280 | if (r) { |
| 1281 | free(buf); |
| 1282 | return r; |
| 1283 | } |
| 1284 | /* Sends [size][minijail]. */ |
| 1285 | written = write(fd, &sz, sizeof(sz)); |
| 1286 | if (written != sizeof(sz)) { |
| 1287 | free(buf); |
| 1288 | return -EFAULT; |
| 1289 | } |
| 1290 | written = write(fd, buf, sz); |
| 1291 | if (written < 0 || (size_t) written != sz) { |
| 1292 | free(buf); |
| 1293 | return -EFAULT; |
| 1294 | } |
| 1295 | free(buf); |
| 1296 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1297 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1298 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1299 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1300 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 1301 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1302 | /* Don't use LDPRELOAD on Brillo. */ |
| 1303 | return 0; |
| 1304 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1305 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 1306 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 1307 | if (!newenv) |
| 1308 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1309 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1310 | /* Only insert a separating space if we have something to separate... */ |
| 1311 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 1312 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1313 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1314 | /* setenv() makes a copy of the string we give it. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1315 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 1316 | free(newenv); |
| 1317 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1318 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1319 | } |
| 1320 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1321 | int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1322 | { |
| 1323 | int r = pipe(fds); |
| 1324 | char fd_buf[11]; |
| 1325 | if (r) |
| 1326 | return r; |
| 1327 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 1328 | if (r <= 0) |
| 1329 | return -EINVAL; |
| 1330 | setenv(kFdEnvVar, fd_buf, 1); |
| 1331 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1332 | } |
| 1333 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1334 | int setup_pipe_end(int fds[2], size_t index) |
| 1335 | { |
| 1336 | if (index > 1) |
| 1337 | return -1; |
| 1338 | |
| 1339 | close(fds[1 - index]); |
| 1340 | return fds[index]; |
| 1341 | } |
| 1342 | |
| 1343 | int setup_and_dupe_pipe_end(int fds[2], size_t index, int fd) |
| 1344 | { |
| 1345 | if (index > 1) |
| 1346 | return -1; |
| 1347 | |
| 1348 | close(fds[1 - index]); |
| 1349 | /* dup2(2) the corresponding end of the pipe into |fd|. */ |
| 1350 | return dup2(fds[index], fd); |
| 1351 | } |
| 1352 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1353 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1354 | char *const argv[], pid_t *pchild_pid, |
| 1355 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1356 | int use_preload); |
| 1357 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1358 | int API minijail_run(struct minijail *j, const char *filename, |
| 1359 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1360 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1361 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1362 | true); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 1366 | char *const argv[], pid_t *pchild_pid) |
| 1367 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1368 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1369 | NULL, NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1373 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1374 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1375 | return minijail_run_internal(j, filename, argv, NULL, pstdin_fd, |
| 1376 | NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1379 | int API minijail_run_pid_pipes(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 1380 | char *const argv[], pid_t *pchild_pid, |
| 1381 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1382 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1383 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1384 | pstdin_fd, pstdout_fd, pstderr_fd, true); |
| 1385 | } |
| 1386 | |
| 1387 | int API minijail_run_no_preload(struct minijail *j, const char *filename, |
| 1388 | char *const argv[]) |
| 1389 | { |
| 1390 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1391 | false); |
| 1392 | } |
| 1393 | |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1394 | int API minijail_run_pid_pipes_no_preload(struct minijail *j, |
| 1395 | const char *filename, char *const argv[], |
| 1396 | pid_t *pchild_pid, |
| 1397 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) { |
| 1398 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1399 | pstdin_fd, pstdout_fd, pstderr_fd, false); |
| 1400 | } |
| 1401 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1402 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1403 | char *const argv[], pid_t *pchild_pid, |
| 1404 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1405 | int use_preload) |
| 1406 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1407 | char *oldenv, *oldenv_copy = NULL; |
| 1408 | pid_t child_pid; |
| 1409 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1410 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1411 | int stdout_fds[2]; |
| 1412 | int stderr_fds[2]; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1413 | int userns_pipe_fds[2]; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1414 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1415 | /* We need to remember this across the minijail_preexec() call. */ |
| 1416 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1417 | int do_init = j->flags.do_init; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1418 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1419 | if (use_preload) { |
| 1420 | oldenv = getenv(kLdPreloadEnvVar); |
| 1421 | if (oldenv) { |
| 1422 | oldenv_copy = strdup(oldenv); |
| 1423 | if (!oldenv_copy) |
| 1424 | return -ENOMEM; |
| 1425 | } |
| 1426 | |
| 1427 | if (setup_preload()) |
| 1428 | return -EFAULT; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1429 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1430 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1431 | if (!use_preload) { |
| 1432 | if (j->flags.caps) |
| 1433 | die("Capabilities are not supported without " |
| 1434 | "LD_PRELOAD"); |
| 1435 | } |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1436 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1437 | /* |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 1438 | * Make the process group ID of this process equal to its PID, so that |
| 1439 | * both the Minijail process and the jailed process can be killed |
| 1440 | * together. |
| 1441 | * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when |
| 1442 | * the process is already a process group leader. |
| 1443 | */ |
| 1444 | if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) { |
| 1445 | if (errno != EPERM) { |
| 1446 | pdie("setpgid(0, 0)"); |
| 1447 | } |
| 1448 | } |
| 1449 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1450 | if (use_preload) { |
| 1451 | /* |
| 1452 | * Before we fork(2) and execve(2) the child process, we need |
| 1453 | * to open a pipe(2) to send the minijail configuration over. |
| 1454 | */ |
| 1455 | if (setup_pipe(pipe_fds)) |
| 1456 | return -EFAULT; |
| 1457 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1458 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1459 | /* |
| 1460 | * If we want to write to the child process' standard input, |
| 1461 | * create the pipe(2) now. |
| 1462 | */ |
| 1463 | if (pstdin_fd) { |
| 1464 | if (pipe(stdin_fds)) |
| 1465 | return -EFAULT; |
| 1466 | } |
| 1467 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1468 | /* |
| 1469 | * If we want to read from the child process' standard output, |
| 1470 | * create the pipe(2) now. |
| 1471 | */ |
| 1472 | if (pstdout_fd) { |
| 1473 | if (pipe(stdout_fds)) |
| 1474 | return -EFAULT; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * If we want to read from the child process' standard error, |
| 1479 | * create the pipe(2) now. |
| 1480 | */ |
| 1481 | if (pstderr_fd) { |
| 1482 | if (pipe(stderr_fds)) |
| 1483 | return -EFAULT; |
| 1484 | } |
| 1485 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1486 | /* |
| 1487 | * If we want to set up a new uid/gid mapping in the user namespace, |
| 1488 | * create the pipe(2) to sync between parent and child. |
| 1489 | */ |
| 1490 | if (j->flags.userns) { |
| 1491 | if (pipe(userns_pipe_fds)) |
| 1492 | return -EFAULT; |
| 1493 | } |
| 1494 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame^] | 1495 | /* |
| 1496 | * Use sys_clone() if and only if we're creating a pid namespace. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1497 | * |
| 1498 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 1499 | * |
| 1500 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 1501 | * some of which may be held by other threads at the time that we call |
| 1502 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 1503 | * ensure that we hold all of these locks before it calls clone() |
| 1504 | * internally and drop them after clone() returns, but when we call |
| 1505 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 1506 | * child address space where some of libc's important locks are held by |
| 1507 | * other threads (which did not get cloned, and hence will never release |
| 1508 | * those locks). This is okay so long as we call exec() immediately |
| 1509 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 1510 | * take locks. |
| 1511 | * |
| 1512 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 1513 | * namespacing. If we follow this path, the child's address space might |
| 1514 | * have broken locks; you may only call functions that do not acquire |
| 1515 | * any locks. |
| 1516 | * |
| 1517 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 1518 | * previously detailed, so this function is highly likely to deadlock |
| 1519 | * later on (see "deadlock here") if we're multithreaded. |
| 1520 | * |
| 1521 | * We might hack around this by having the clone()d child (init of the |
| 1522 | * pid namespace) return directly, rather than leaving the clone()d |
| 1523 | * process hanging around to be init for the new namespace (and having |
| 1524 | * its fork()ed child return in turn), but that process would be crippled |
| 1525 | * with its libc locks potentially broken. We might try fork()ing in the |
| 1526 | * parent before we clone() to ensure that we own all the locks, but |
| 1527 | * then we have to have the forked child hanging around consuming |
| 1528 | * resources (and possibly having file descriptors / shared memory |
| 1529 | * regions / etc attached). We'd need to keep the child around to avoid |
| 1530 | * having its children get reparented to init. |
| 1531 | * |
| 1532 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 1533 | * problem is fixable or not. It would be nice if we worked in this |
| 1534 | * case. |
| 1535 | */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1536 | if (pid_namespace) { |
| 1537 | int clone_flags = CLONE_NEWPID | SIGCHLD; |
| 1538 | if (j->flags.userns) |
| 1539 | clone_flags |= CLONE_NEWUSER; |
| 1540 | child_pid = syscall(SYS_clone, clone_flags, NULL); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1541 | } else { |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1542 | child_pid = fork(); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1543 | } |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1544 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1545 | if (child_pid < 0) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1546 | if (use_preload) { |
| 1547 | free(oldenv_copy); |
| 1548 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 1549 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1550 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1551 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1552 | if (child_pid) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1553 | if (use_preload) { |
| 1554 | /* Restore parent's LD_PRELOAD. */ |
| 1555 | if (oldenv_copy) { |
| 1556 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 1557 | free(oldenv_copy); |
| 1558 | } else { |
| 1559 | unsetenv(kLdPreloadEnvVar); |
| 1560 | } |
| 1561 | unsetenv(kFdEnvVar); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1562 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1563 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1564 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1565 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1566 | if (j->flags.pid_file) |
| 1567 | write_pid_file(j); |
| 1568 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1569 | if (j->flags.userns) |
| 1570 | write_ugid_mappings(j, userns_pipe_fds); |
| 1571 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1572 | if (use_preload) { |
| 1573 | /* Send marshalled minijail. */ |
| 1574 | close(pipe_fds[0]); /* read endpoint */ |
| 1575 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 1576 | close(pipe_fds[1]); /* write endpoint */ |
| 1577 | if (ret) { |
| 1578 | kill(j->initpid, SIGKILL); |
| 1579 | die("failed to send marshalled minijail"); |
| 1580 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1581 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1582 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1583 | if (pchild_pid) |
| 1584 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1585 | |
| 1586 | /* |
| 1587 | * If we want to write to the child process' standard input, |
| 1588 | * set up the write end of the pipe. |
| 1589 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1590 | if (pstdin_fd) |
| 1591 | *pstdin_fd = setup_pipe_end(stdin_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1592 | 1 /* write end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1593 | |
| 1594 | /* |
| 1595 | * If we want to read from the child process' standard output, |
| 1596 | * set up the read end of the pipe. |
| 1597 | */ |
| 1598 | if (pstdout_fd) |
| 1599 | *pstdout_fd = setup_pipe_end(stdout_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1600 | 0 /* read end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1601 | |
| 1602 | /* |
| 1603 | * If we want to read from the child process' standard error, |
| 1604 | * set up the read end of the pipe. |
| 1605 | */ |
| 1606 | if (pstderr_fd) |
| 1607 | *pstderr_fd = setup_pipe_end(stderr_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1608 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1609 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1610 | return 0; |
| 1611 | } |
| 1612 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 1613 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 1614 | if (j->flags.userns) |
| 1615 | enter_user_namespace(j, userns_pipe_fds); |
| 1616 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1617 | /* |
| 1618 | * If we want to write to the jailed process' standard input, |
| 1619 | * set up the read end of the pipe. |
| 1620 | */ |
| 1621 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1622 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 1623 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1624 | die("failed to set up stdin pipe"); |
| 1625 | } |
| 1626 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1627 | /* |
| 1628 | * If we want to read from the jailed process' standard output, |
| 1629 | * set up the write end of the pipe. |
| 1630 | */ |
| 1631 | if (pstdout_fd) { |
| 1632 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 1633 | STDOUT_FILENO) < 0) |
| 1634 | die("failed to set up stdout pipe"); |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * If we want to read from the jailed process' standard error, |
| 1639 | * set up the write end of the pipe. |
| 1640 | */ |
| 1641 | if (pstderr_fd) { |
| 1642 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 1643 | STDERR_FILENO) < 0) |
| 1644 | die("failed to set up stderr pipe"); |
| 1645 | } |
| 1646 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1647 | /* If running an init program, let it decide when/how to mount /proc. */ |
| 1648 | if (pid_namespace && !do_init) |
| 1649 | j->flags.remount_proc_ro = 0; |
| 1650 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1651 | if (use_preload) { |
| 1652 | /* Strip out flags that cannot be inherited across execve(2). */ |
| 1653 | minijail_preexec(j); |
| 1654 | } else { |
| 1655 | j->flags.pids = 0; |
| 1656 | } |
| 1657 | /* Jail this process, then execve() the target. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1658 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1659 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1660 | if (pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1661 | /* |
| 1662 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1663 | * namespace. We don't want all programs we might exec to have |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1664 | * to know how to be init. Normally (do_init == 1) we fork off |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1665 | * a child to actually run the program. If |do_init == 0|, we |
| 1666 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 1667 | * |
| 1668 | * If we're multithreaded, we'll probably deadlock here. See |
| 1669 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1670 | */ |
| 1671 | child_pid = fork(); |
| 1672 | if (child_pid < 0) |
| 1673 | _exit(child_pid); |
| 1674 | else if (child_pid > 0) |
| 1675 | init(child_pid); /* never returns */ |
| 1676 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1677 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1678 | /* |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1679 | * If we aren't pid-namespaced, or the jailed program asked to be init: |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1680 | * calling process |
| 1681 | * -> execve()-ing process |
| 1682 | * If we are: |
| 1683 | * calling process |
| 1684 | * -> init()-ing process |
| 1685 | * -> execve()-ing process |
| 1686 | */ |
| 1687 | _exit(execve(filename, argv, environ)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1688 | } |
| 1689 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1690 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1691 | { |
| 1692 | int st; |
| 1693 | if (kill(j->initpid, SIGTERM)) |
| 1694 | return -errno; |
| 1695 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1696 | return -errno; |
| 1697 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1698 | } |
| 1699 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1700 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1701 | { |
| 1702 | int st; |
| 1703 | if (waitpid(j->initpid, &st, 0) < 0) |
| 1704 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1705 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1706 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1707 | int error_status = st; |
| 1708 | if (WIFSIGNALED(st)) { |
| 1709 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1710 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 1711 | j->initpid, signum); |
| 1712 | /* |
| 1713 | * We return MINIJAIL_ERR_JAIL if the process received |
| 1714 | * SIGSYS, which happens when a syscall is blocked by |
| 1715 | * seccomp filters. |
| 1716 | * If not, we do what bash(1) does: |
| 1717 | * $? = 128 + signum |
| 1718 | */ |
| 1719 | if (signum == SIGSYS) { |
| 1720 | error_status = MINIJAIL_ERR_JAIL; |
| 1721 | } else { |
| 1722 | error_status = 128 + signum; |
| 1723 | } |
| 1724 | } |
| 1725 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1726 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1727 | |
| 1728 | int exit_status = WEXITSTATUS(st); |
| 1729 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 1730 | info("child process %d exited with status %d", |
| 1731 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 1732 | |
| 1733 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1734 | } |
| 1735 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1736 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1737 | { |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1738 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 1739 | free(j->filter_prog->filter); |
| 1740 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1741 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1742 | while (j->mounts_head) { |
| 1743 | struct mountpoint *m = j->mounts_head; |
| 1744 | j->mounts_head = j->mounts_head->next; |
| 1745 | free(m->type); |
| 1746 | free(m->dest); |
| 1747 | free(m->src); |
| 1748 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1749 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1750 | j->mounts_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1751 | if (j->user) |
| 1752 | free(j->user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1753 | if (j->chrootdir) |
| 1754 | free(j->chrootdir); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1755 | if (j->alt_syscall_table) |
| 1756 | free(j->alt_syscall_table); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1757 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1758 | } |