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 |
Arthur Gautier | 7a56907 | 2016-04-23 17:25:20 +0000 | [diff] [blame] | 7 | #define _DEFAULT_SOURCE |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 8 | #define _GNU_SOURCE |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 9 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 10 | #include <asm/unistd.h> |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 11 | #include <dirent.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 12 | #include <errno.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 13 | #include <fcntl.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 14 | #include <grp.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 15 | #include <linux/capability.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 16 | #include <pwd.h> |
| 17 | #include <sched.h> |
| 18 | #include <signal.h> |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 19 | #include <stdbool.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 20 | #include <stddef.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 24 | #include <sys/capability.h> |
| 25 | #include <sys/mount.h> |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 26 | #include <sys/param.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 27 | #include <sys/prctl.h> |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 28 | #include <sys/resource.h> |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 31 | #include <sys/user.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 32 | #include <sys/wait.h> |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 33 | #include <syscall.h> |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
| 36 | #include "libminijail.h" |
| 37 | #include "libminijail-private.h" |
| 38 | |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 39 | #include "signal_handler.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 40 | #include "syscall_filter.h" |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 41 | #include "syscall_wrapper.h" |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 42 | #include "system.h" |
Jorge Lucangeli Obes | a6b034d | 2012-08-07 15:29:20 -0700 | [diff] [blame] | 43 | #include "util.h" |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 44 | |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 45 | /* Until these are reliably available in linux/prctl.h. */ |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 46 | #ifndef PR_ALT_SYSCALL |
| 47 | # define PR_ALT_SYSCALL 0x43724f53 |
| 48 | #endif |
| 49 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 50 | /* Seccomp filter related flags. */ |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 51 | #ifndef PR_SET_NO_NEW_PRIVS |
| 52 | # define PR_SET_NO_NEW_PRIVS 38 |
| 53 | #endif |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 54 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 55 | #ifndef SECCOMP_MODE_FILTER |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 56 | #define SECCOMP_MODE_FILTER 2 /* Uses user-supplied filter. */ |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 57 | #endif |
| 58 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 59 | #ifndef SECCOMP_SET_MODE_STRICT |
| 60 | # define SECCOMP_SET_MODE_STRICT 0 |
| 61 | #endif |
| 62 | #ifndef SECCOMP_SET_MODE_FILTER |
| 63 | # define SECCOMP_SET_MODE_FILTER 1 |
| 64 | #endif |
| 65 | |
| 66 | #ifndef SECCOMP_FILTER_FLAG_TSYNC |
| 67 | # define SECCOMP_FILTER_FLAG_TSYNC 1 |
| 68 | #endif |
| 69 | /* End seccomp filter related flags. */ |
| 70 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 71 | /* New cgroup namespace might not be in linux-headers yet. */ |
| 72 | #ifndef CLONE_NEWCGROUP |
| 73 | # define CLONE_NEWCGROUP 0x02000000 |
| 74 | #endif |
| 75 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 76 | #define MAX_CGROUPS 10 /* 10 different controllers supported by Linux. */ |
| 77 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 78 | #define MAX_RLIMITS 32 /* Currently there are 15 supported by Linux. */ |
| 79 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 80 | /* Keyctl commands. */ |
| 81 | #define KEYCTL_JOIN_SESSION_KEYRING 1 |
| 82 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 83 | struct minijail_rlimit { |
| 84 | int type; |
| 85 | uint32_t cur; |
| 86 | uint32_t max; |
| 87 | }; |
| 88 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 89 | struct mountpoint { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 90 | char *src; |
| 91 | char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 92 | char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 93 | char *data; |
| 94 | int has_data; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 95 | unsigned long flags; |
| 96 | struct mountpoint *next; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 97 | }; |
| 98 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 99 | struct minijail { |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 100 | /* |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 101 | * WARNING: if you add a flag here you need to make sure it's |
| 102 | * accounted for in minijail_pre{enter|exec}() below. |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 103 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 104 | struct { |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 105 | int uid : 1; |
| 106 | int gid : 1; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 107 | int inherit_suppl_gids : 1; |
| 108 | int set_suppl_gids : 1; |
| 109 | int keep_suppl_gids : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 110 | int use_caps : 1; |
| 111 | int capbset_drop : 1; |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 112 | int set_ambient_caps : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 113 | int vfs : 1; |
| 114 | int enter_vfs : 1; |
| 115 | int skip_remount_private : 1; |
| 116 | int pids : 1; |
| 117 | int ipc : 1; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 118 | int uts : 1; |
Luis Hector Chavez | fb449ab | 2016-10-14 09:49:22 -0700 | [diff] [blame] | 119 | int net : 1; |
| 120 | int enter_net : 1; |
| 121 | int ns_cgroups : 1; |
| 122 | int userns : 1; |
| 123 | int disable_setgroups : 1; |
| 124 | int seccomp : 1; |
| 125 | int remount_proc_ro : 1; |
| 126 | int no_new_privs : 1; |
| 127 | int seccomp_filter : 1; |
| 128 | int seccomp_filter_tsync : 1; |
| 129 | int seccomp_filter_logging : 1; |
| 130 | int chroot : 1; |
| 131 | int pivot_root : 1; |
| 132 | int mount_tmp : 1; |
| 133 | int do_init : 1; |
| 134 | int pid_file : 1; |
| 135 | int cgroups : 1; |
| 136 | int alt_syscall : 1; |
| 137 | int reset_signal_mask : 1; |
| 138 | int close_open_fds : 1; |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 139 | int new_session_keyring : 1; |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 140 | int forward_signals : 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 141 | } flags; |
| 142 | uid_t uid; |
| 143 | gid_t gid; |
| 144 | gid_t usergid; |
| 145 | char *user; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 146 | size_t suppl_gid_count; |
| 147 | gid_t *suppl_gid_list; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 148 | uint64_t caps; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 149 | uint64_t cap_bset; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 150 | pid_t initpid; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 151 | int mountns_fd; |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 152 | int netns_fd; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 153 | char *chrootdir; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 154 | char *pid_file_path; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 155 | char *uidmap; |
| 156 | char *gidmap; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 157 | char *hostname; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 158 | size_t filter_len; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 159 | struct sock_fprog *filter_prog; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 160 | char *alt_syscall_table; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 161 | struct mountpoint *mounts_head; |
| 162 | struct mountpoint *mounts_tail; |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 163 | size_t mounts_count; |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 164 | size_t tmpfs_size; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 165 | char *cgroups[MAX_CGROUPS]; |
| 166 | size_t cgroup_count; |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 167 | struct minijail_rlimit rlimits[MAX_RLIMITS]; |
| 168 | size_t rlimit_count; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 169 | }; |
| 170 | |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Strip out flags meant for the parent. |
| 173 | * We keep things that are not inherited across execve(2) (e.g. capabilities), |
| 174 | * or are easier to set after execve(2) (e.g. seccomp filters). |
| 175 | */ |
| 176 | void minijail_preenter(struct minijail *j) |
| 177 | { |
| 178 | j->flags.vfs = 0; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 179 | j->flags.enter_vfs = 0; |
Shuhei Takahashi | 3da4031 | 2016-03-07 17:37:49 +0900 | [diff] [blame] | 180 | j->flags.skip_remount_private = 0; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 181 | j->flags.remount_proc_ro = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 182 | j->flags.pids = 0; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 183 | j->flags.do_init = 0; |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 184 | j->flags.pid_file = 0; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 185 | j->flags.cgroups = 0; |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 186 | j->flags.forward_signals = 0; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Strip out flags meant for the child. |
| 191 | * We keep things that are inherited across execve(2). |
| 192 | */ |
| 193 | void minijail_preexec(struct minijail *j) |
| 194 | { |
| 195 | int vfs = j->flags.vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 196 | int enter_vfs = j->flags.enter_vfs; |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 197 | int skip_remount_private = j->flags.skip_remount_private; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 198 | int remount_proc_ro = j->flags.remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 199 | int userns = j->flags.userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 200 | if (j->user) |
| 201 | free(j->user); |
| 202 | j->user = NULL; |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 203 | if (j->suppl_gid_list) |
| 204 | free(j->suppl_gid_list); |
| 205 | j->suppl_gid_list = NULL; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 206 | memset(&j->flags, 0, sizeof(j->flags)); |
| 207 | /* Now restore anything we meant to keep. */ |
| 208 | j->flags.vfs = vfs; |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 209 | j->flags.enter_vfs = enter_vfs; |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 210 | j->flags.skip_remount_private = skip_remount_private; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 211 | j->flags.remount_proc_ro = remount_proc_ro; |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 212 | j->flags.userns = userns; |
Jorge Lucangeli Obes | 4ae30cc | 2014-04-10 15:35:33 -0700 | [diff] [blame] | 213 | /* Note, |pids| will already have been used before this call. */ |
| 214 | } |
| 215 | |
| 216 | /* Minijail API. */ |
| 217 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 218 | struct minijail API *minijail_new(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 219 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 220 | return calloc(1, sizeof(struct minijail)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 221 | } |
| 222 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 223 | void API minijail_change_uid(struct minijail *j, uid_t uid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 224 | { |
| 225 | if (uid == 0) |
| 226 | die("useless change to uid 0"); |
| 227 | j->uid = uid; |
| 228 | j->flags.uid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 231 | void API minijail_change_gid(struct minijail *j, gid_t gid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 232 | { |
| 233 | if (gid == 0) |
| 234 | die("useless change to gid 0"); |
| 235 | j->gid = gid; |
| 236 | j->flags.gid = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 239 | void API minijail_set_supplementary_gids(struct minijail *j, size_t size, |
| 240 | const gid_t *list) |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 241 | { |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 242 | size_t i; |
| 243 | |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 244 | if (j->flags.inherit_suppl_gids) |
| 245 | die("cannot inherit *and* set supplementary groups"); |
| 246 | if (j->flags.keep_suppl_gids) |
| 247 | die("cannot keep *and* set supplementary groups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 248 | |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 249 | if (size == 0) { |
| 250 | /* Clear supplementary groups. */ |
| 251 | j->suppl_gid_list = NULL; |
| 252 | j->suppl_gid_count = 0; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 253 | j->flags.set_suppl_gids = 1; |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 254 | return; |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 255 | } |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 256 | |
| 257 | /* Copy the gid_t array. */ |
| 258 | j->suppl_gid_list = calloc(size, sizeof(gid_t)); |
| 259 | if (!j->suppl_gid_list) { |
Jorge Lucangeli Obes | fd5fc56 | 2016-01-08 10:29:27 -0800 | [diff] [blame] | 260 | die("failed to allocate internal supplementary group array"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 261 | } |
Jorge Lucangeli Obes | 06940be | 2015-12-04 18:09:21 -0800 | [diff] [blame] | 262 | for (i = 0; i < size; i++) { |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 263 | j->suppl_gid_list[i] = list[i]; |
| 264 | } |
| 265 | j->suppl_gid_count = size; |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 266 | j->flags.set_suppl_gids = 1; |
| 267 | } |
| 268 | |
| 269 | void API minijail_keep_supplementary_gids(struct minijail *j) { |
| 270 | j->flags.keep_suppl_gids = 1; |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 273 | int API minijail_change_user(struct minijail *j, const char *user) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 274 | { |
| 275 | char *buf = NULL; |
| 276 | struct passwd pw; |
| 277 | struct passwd *ppw = NULL; |
| 278 | ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); |
| 279 | if (sz == -1) |
| 280 | sz = 65536; /* your guess is as good as mine... */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 281 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 282 | /* |
| 283 | * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 284 | * the maximum needed size of the buffer, so we don't have to search. |
| 285 | */ |
| 286 | buf = malloc(sz); |
| 287 | if (!buf) |
| 288 | return -ENOMEM; |
| 289 | getpwnam_r(user, &pw, buf, sz, &ppw); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 290 | /* |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 291 | * We're safe to free the buffer here. The strings inside |pw| point |
| 292 | * inside |buf|, but we don't use any of them; this leaves the pointers |
Jorge Lucangeli Obes | 87bf01d | 2016-03-08 11:20:03 -0800 | [diff] [blame] | 293 | * dangling but it's safe. |ppw| points at |pw| if getpwnam_r(3) |
| 294 | * succeeded. |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 295 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 296 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 297 | /* getpwnam_r(3) does *not* set errno when |ppw| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 298 | if (!ppw) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 299 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 300 | minijail_change_uid(j, ppw->pw_uid); |
| 301 | j->user = strdup(user); |
| 302 | if (!j->user) |
| 303 | return -ENOMEM; |
| 304 | j->usergid = ppw->pw_gid; |
| 305 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 306 | } |
| 307 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 308 | int API minijail_change_group(struct minijail *j, const char *group) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 309 | { |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 310 | char *buf = NULL; |
Yabin Cui | 1b21c8f | 2015-07-22 10:34:45 -0700 | [diff] [blame] | 311 | struct group gr; |
| 312 | struct group *pgr = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 313 | ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); |
| 314 | if (sz == -1) |
| 315 | sz = 65536; /* and mine is as good as yours, really */ |
Elly Jones | eb300c5 | 2011-09-22 14:35:43 -0400 | [diff] [blame] | 316 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 317 | /* |
| 318 | * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 319 | * the maximum needed size of the buffer, so we don't have to search. |
| 320 | */ |
| 321 | buf = malloc(sz); |
| 322 | if (!buf) |
| 323 | return -ENOMEM; |
| 324 | getgrnam_r(group, &gr, buf, sz, &pgr); |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 325 | /* |
| 326 | * We're safe to free the buffer here. The strings inside gr point |
| 327 | * inside buf, but we don't use any of them; this leaves the pointers |
| 328 | * dangling but it's safe. pgr points at gr if getgrnam_r succeeded. |
| 329 | */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 330 | free(buf); |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 331 | /* getgrnam_r(3) does *not* set errno when |pgr| is NULL. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 332 | if (!pgr) |
Jorge Lucangeli Obes | 4e48065 | 2014-03-26 10:56:42 -0700 | [diff] [blame] | 333 | return -1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 334 | minijail_change_gid(j, pgr->gr_gid); |
| 335 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 338 | void API minijail_use_seccomp(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 339 | { |
| 340 | j->flags.seccomp = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 343 | void API minijail_no_new_privs(struct minijail *j) |
| 344 | { |
| 345 | j->flags.no_new_privs = 1; |
| 346 | } |
| 347 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 348 | void API minijail_use_seccomp_filter(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 349 | { |
| 350 | j->flags.seccomp_filter = 1; |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 351 | } |
| 352 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 353 | void API minijail_set_seccomp_filter_tsync(struct minijail *j) |
| 354 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 355 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 356 | die("minijail_set_seccomp_filter_tsync() must be called " |
| 357 | "before minijail_parse_seccomp_filters()"); |
| 358 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 359 | j->flags.seccomp_filter_tsync = 1; |
| 360 | } |
| 361 | |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 362 | void API minijail_log_seccomp_filter_failures(struct minijail *j) |
| 363 | { |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 364 | if (j->filter_len > 0 && j->filter_prog != NULL) { |
| 365 | die("minijail_log_seccomp_filter_failures() must be called " |
| 366 | "before minijail_parse_seccomp_filters()"); |
| 367 | } |
| 368 | j->flags.seccomp_filter_logging = 1; |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 371 | void API minijail_use_caps(struct minijail *j, uint64_t capmask) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 372 | { |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 373 | /* |
| 374 | * 'minijail_use_caps' configures a runtime-capabilities-only |
| 375 | * environment, including a bounding set matching the thread's runtime |
| 376 | * (permitted|inheritable|effective) sets. |
| 377 | * Therefore, it will override any existing bounding set configurations |
| 378 | * since the latter would allow gaining extra runtime capabilities from |
| 379 | * file capabilities. |
| 380 | */ |
| 381 | if (j->flags.capbset_drop) { |
| 382 | warn("overriding bounding set configuration"); |
| 383 | j->cap_bset = 0; |
| 384 | j->flags.capbset_drop = 0; |
| 385 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 386 | j->caps = capmask; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 387 | j->flags.use_caps = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 388 | } |
| 389 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 390 | void API minijail_capbset_drop(struct minijail *j, uint64_t capmask) |
| 391 | { |
| 392 | if (j->flags.use_caps) { |
| 393 | /* |
| 394 | * 'minijail_use_caps' will have already configured a capability |
| 395 | * bounding set matching the (permitted|inheritable|effective) |
| 396 | * sets. Abort if the user tries to configure a separate |
| 397 | * bounding set. 'minijail_capbset_drop' and 'minijail_use_caps' |
| 398 | * are mutually exclusive. |
| 399 | */ |
| 400 | die("runtime capabilities already configured, can't drop " |
| 401 | "bounding set separately"); |
| 402 | } |
| 403 | j->cap_bset = capmask; |
| 404 | j->flags.capbset_drop = 1; |
| 405 | } |
| 406 | |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 407 | void API minijail_set_ambient_caps(struct minijail *j) |
| 408 | { |
| 409 | j->flags.set_ambient_caps = 1; |
| 410 | } |
| 411 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 412 | void API minijail_reset_signal_mask(struct minijail *j) |
| 413 | { |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 414 | j->flags.reset_signal_mask = 1; |
| 415 | } |
| 416 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 417 | void API minijail_namespace_vfs(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 418 | { |
| 419 | j->flags.vfs = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 422 | void API minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path) |
| 423 | { |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 424 | int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 425 | if (ns_fd < 0) { |
| 426 | pdie("failed to open namespace '%s'", ns_path); |
| 427 | } |
| 428 | j->mountns_fd = ns_fd; |
| 429 | j->flags.enter_vfs = 1; |
| 430 | } |
| 431 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 432 | void API minijail_new_session_keyring(struct minijail *j) |
| 433 | { |
| 434 | j->flags.new_session_keyring = 1; |
| 435 | } |
| 436 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 437 | void API minijail_skip_remount_private(struct minijail *j) |
| 438 | { |
| 439 | j->flags.skip_remount_private = 1; |
| 440 | } |
| 441 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 442 | void API minijail_namespace_pids(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 443 | { |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 444 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 445 | j->flags.remount_proc_ro = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 446 | j->flags.pids = 1; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 447 | j->flags.do_init = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 448 | } |
| 449 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 450 | void API minijail_namespace_ipc(struct minijail *j) |
| 451 | { |
| 452 | j->flags.ipc = 1; |
| 453 | } |
| 454 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 455 | void API minijail_namespace_uts(struct minijail *j) |
| 456 | { |
| 457 | j->flags.uts = 1; |
| 458 | } |
| 459 | |
| 460 | int API minijail_namespace_set_hostname(struct minijail *j, const char *name) |
| 461 | { |
| 462 | if (j->hostname) |
| 463 | return -EINVAL; |
| 464 | minijail_namespace_uts(j); |
| 465 | j->hostname = strdup(name); |
| 466 | if (!j->hostname) |
| 467 | return -ENOMEM; |
| 468 | return 0; |
| 469 | } |
| 470 | |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 471 | void API minijail_namespace_net(struct minijail *j) |
| 472 | { |
| 473 | j->flags.net = 1; |
| 474 | } |
| 475 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 476 | void API minijail_namespace_enter_net(struct minijail *j, const char *ns_path) |
| 477 | { |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 478 | int ns_fd = open(ns_path, O_RDONLY | O_CLOEXEC); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 479 | if (ns_fd < 0) { |
| 480 | pdie("failed to open namespace '%s'", ns_path); |
| 481 | } |
| 482 | j->netns_fd = ns_fd; |
| 483 | j->flags.enter_net = 1; |
| 484 | } |
| 485 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 486 | void API minijail_namespace_cgroups(struct minijail *j) |
| 487 | { |
| 488 | j->flags.ns_cgroups = 1; |
| 489 | } |
| 490 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 491 | void API minijail_close_open_fds(struct minijail *j) |
| 492 | { |
| 493 | j->flags.close_open_fds = 1; |
| 494 | } |
| 495 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 496 | void API minijail_remount_proc_readonly(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 497 | { |
| 498 | j->flags.vfs = 1; |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 499 | j->flags.remount_proc_ro = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 502 | void API minijail_namespace_user(struct minijail *j) |
| 503 | { |
| 504 | j->flags.userns = 1; |
| 505 | } |
| 506 | |
Jorge Lucangeli Obes | 200299c | 2016-09-23 15:21:57 -0400 | [diff] [blame] | 507 | void API minijail_namespace_user_disable_setgroups(struct minijail *j) |
| 508 | { |
| 509 | j->flags.disable_setgroups = 1; |
| 510 | } |
| 511 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 512 | int API minijail_uidmap(struct minijail *j, const char *uidmap) |
| 513 | { |
| 514 | j->uidmap = strdup(uidmap); |
| 515 | if (!j->uidmap) |
| 516 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 517 | char *ch; |
| 518 | for (ch = j->uidmap; *ch; ch++) { |
| 519 | if (*ch == ',') |
| 520 | *ch = '\n'; |
| 521 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | int API minijail_gidmap(struct minijail *j, const char *gidmap) |
| 526 | { |
| 527 | j->gidmap = strdup(gidmap); |
| 528 | if (!j->gidmap) |
| 529 | return -ENOMEM; |
Yu-Hsi Chiang | 1912c5b | 2015-08-31 18:59:49 +0800 | [diff] [blame] | 530 | char *ch; |
| 531 | for (ch = j->gidmap; *ch; ch++) { |
| 532 | if (*ch == ',') |
| 533 | *ch = '\n'; |
| 534 | } |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 538 | void API minijail_inherit_usergroups(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 539 | { |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 540 | j->flags.inherit_suppl_gids = 1; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 543 | void API minijail_run_as_init(struct minijail *j) |
| 544 | { |
| 545 | /* |
| 546 | * Since the jailed program will become 'init' in the new PID namespace, |
| 547 | * Minijail does not need to fork an 'init' process. |
| 548 | */ |
| 549 | j->flags.do_init = 0; |
| 550 | } |
| 551 | |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 552 | int API minijail_enter_chroot(struct minijail *j, const char *dir) |
| 553 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 554 | if (j->chrootdir) |
| 555 | return -EINVAL; |
| 556 | j->chrootdir = strdup(dir); |
| 557 | if (!j->chrootdir) |
| 558 | return -ENOMEM; |
| 559 | j->flags.chroot = 1; |
| 560 | return 0; |
| 561 | } |
| 562 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 563 | int API minijail_enter_pivot_root(struct minijail *j, const char *dir) |
| 564 | { |
| 565 | if (j->chrootdir) |
| 566 | return -EINVAL; |
| 567 | j->chrootdir = strdup(dir); |
| 568 | if (!j->chrootdir) |
| 569 | return -ENOMEM; |
| 570 | j->flags.pivot_root = 1; |
| 571 | return 0; |
| 572 | } |
| 573 | |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 574 | char API *minijail_get_original_path(struct minijail *j, |
| 575 | const char *path_inside_chroot) |
| 576 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 577 | struct mountpoint *b; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 578 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 579 | b = j->mounts_head; |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 580 | while (b) { |
| 581 | /* |
| 582 | * If |path_inside_chroot| is the exact destination of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 583 | * mount, then the original path is exactly the source of |
| 584 | * the mount. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 585 | * for example: "-b /some/path/exe,/chroot/path/exe" |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 586 | * mount source = /some/path/exe, mount dest = |
| 587 | * /chroot/path/exe Then when getting the original path of |
| 588 | * "/chroot/path/exe", the source of that mount, |
| 589 | * "/some/path/exe" is what should be returned. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 590 | */ |
| 591 | if (!strcmp(b->dest, path_inside_chroot)) |
| 592 | return strdup(b->src); |
| 593 | |
| 594 | /* |
| 595 | * If |path_inside_chroot| is within the destination path of a |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 596 | * mount, take the suffix of the chroot path relative to the |
| 597 | * mount destination path, and append it to the mount source |
| 598 | * path. |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 599 | */ |
| 600 | if (!strncmp(b->dest, path_inside_chroot, strlen(b->dest))) { |
| 601 | const char *relative_path = |
| 602 | path_inside_chroot + strlen(b->dest); |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 603 | return path_join(b->src, relative_path); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 604 | } |
| 605 | b = b->next; |
| 606 | } |
| 607 | |
| 608 | /* If there is a chroot path, append |path_inside_chroot| to that. */ |
| 609 | if (j->chrootdir) |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 610 | return path_join(j->chrootdir, path_inside_chroot); |
Dylan Reid | a14e08d | 2015-10-22 21:05:29 -0700 | [diff] [blame] | 611 | |
| 612 | /* No chroot, so the path outside is the same as it is inside. */ |
| 613 | return strdup(path_inside_chroot); |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 616 | size_t minijail_get_tmpfs_size(const struct minijail *j) |
| 617 | { |
| 618 | return j->tmpfs_size; |
| 619 | } |
| 620 | |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 621 | void API minijail_mount_tmp(struct minijail *j) |
| 622 | { |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 623 | minijail_mount_tmp_size(j, 64 * 1024 * 1024); |
| 624 | } |
| 625 | |
| 626 | void API minijail_mount_tmp_size(struct minijail *j, size_t size) |
| 627 | { |
| 628 | j->tmpfs_size = size; |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 629 | j->flags.mount_tmp = 1; |
| 630 | } |
| 631 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 632 | int API minijail_write_pid_file(struct minijail *j, const char *path) |
| 633 | { |
| 634 | j->pid_file_path = strdup(path); |
| 635 | if (!j->pid_file_path) |
| 636 | return -ENOMEM; |
| 637 | j->flags.pid_file = 1; |
| 638 | return 0; |
| 639 | } |
| 640 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 641 | int API minijail_add_to_cgroup(struct minijail *j, const char *path) |
| 642 | { |
| 643 | if (j->cgroup_count >= MAX_CGROUPS) |
| 644 | return -ENOMEM; |
| 645 | j->cgroups[j->cgroup_count] = strdup(path); |
| 646 | if (!j->cgroups[j->cgroup_count]) |
| 647 | return -ENOMEM; |
| 648 | j->cgroup_count++; |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 649 | j->flags.cgroups = 1; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 653 | int API minijail_rlimit(struct minijail *j, int type, uint32_t cur, |
| 654 | uint32_t max) |
| 655 | { |
| 656 | size_t i; |
| 657 | |
| 658 | if (j->rlimit_count >= MAX_RLIMITS) |
| 659 | return -ENOMEM; |
| 660 | /* It's an error if the caller sets the same rlimit multiple times. */ |
| 661 | for (i = 0; i < j->rlimit_count; i++) { |
| 662 | if (j->rlimits[i].type == type) |
| 663 | return -EEXIST; |
| 664 | } |
| 665 | |
| 666 | j->rlimits[j->rlimit_count].type = type; |
| 667 | j->rlimits[j->rlimit_count].cur = cur; |
| 668 | j->rlimits[j->rlimit_count].max = max; |
| 669 | j->rlimit_count++; |
| 670 | return 0; |
| 671 | } |
| 672 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 673 | int API minijail_forward_signals(struct minijail *j) |
| 674 | { |
| 675 | j->flags.forward_signals = 1; |
| 676 | return 0; |
| 677 | } |
| 678 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 679 | int API minijail_mount_with_data(struct minijail *j, const char *src, |
| 680 | const char *dest, const char *type, |
| 681 | unsigned long flags, const char *data) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 682 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 683 | struct mountpoint *m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 684 | |
| 685 | if (*dest != '/') |
| 686 | return -EINVAL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 687 | m = calloc(1, sizeof(*m)); |
| 688 | if (!m) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 689 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 690 | m->dest = strdup(dest); |
| 691 | if (!m->dest) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 692 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 693 | m->src = strdup(src); |
| 694 | if (!m->src) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 695 | goto error; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 696 | m->type = strdup(type); |
| 697 | if (!m->type) |
| 698 | goto error; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 699 | if (data) { |
| 700 | m->data = strdup(data); |
| 701 | if (!m->data) |
| 702 | goto error; |
| 703 | m->has_data = 1; |
| 704 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 705 | m->flags = flags; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 706 | |
Jorge Lucangeli Obes | 6c755d2 | 2016-01-28 15:24:40 -0800 | [diff] [blame] | 707 | info("mount %s -> %s type '%s'", src, dest, type); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 708 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 709 | /* |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 710 | * Force vfs namespacing so the mounts don't leak out into the |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 711 | * containing vfs namespace. |
| 712 | */ |
| 713 | minijail_namespace_vfs(j); |
| 714 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 715 | if (j->mounts_tail) |
| 716 | j->mounts_tail->next = m; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 717 | else |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 718 | j->mounts_head = m; |
| 719 | j->mounts_tail = m; |
| 720 | j->mounts_count++; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 721 | |
| 722 | return 0; |
| 723 | |
| 724 | error: |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 725 | free(m->type); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 726 | free(m->src); |
| 727 | free(m->dest); |
| 728 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 729 | return -ENOMEM; |
| 730 | } |
| 731 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 732 | int API minijail_mount(struct minijail *j, const char *src, const char *dest, |
| 733 | const char *type, unsigned long flags) |
| 734 | { |
| 735 | return minijail_mount_with_data(j, src, dest, type, flags, NULL); |
| 736 | } |
| 737 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 738 | int API minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 739 | int writeable) |
| 740 | { |
| 741 | unsigned long flags = MS_BIND; |
| 742 | |
| 743 | if (!writeable) |
| 744 | flags |= MS_RDONLY; |
| 745 | |
| 746 | return minijail_mount(j, src, dest, "", flags); |
| 747 | } |
| 748 | |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 749 | static void clear_seccomp_options(struct minijail *j) |
| 750 | { |
| 751 | j->flags.seccomp_filter = 0; |
| 752 | j->flags.seccomp_filter_tsync = 0; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 753 | j->flags.seccomp_filter_logging = 0; |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 754 | j->filter_len = 0; |
| 755 | j->filter_prog = NULL; |
| 756 | j->flags.no_new_privs = 0; |
| 757 | } |
| 758 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 759 | static int seccomp_should_parse_filters(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 760 | { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 761 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL) == -1) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 762 | /* |
| 763 | * |errno| will be set to EINVAL when seccomp has not been |
| 764 | * compiled into the kernel. On certain platforms and kernel |
| 765 | * versions this is not a fatal failure. In that case, and only |
| 766 | * in that case, disable seccomp and skip loading the filters. |
| 767 | */ |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 768 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 769 | warn("not loading seccomp filters, seccomp filter not " |
| 770 | "supported"); |
| 771 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 772 | return 0; |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 773 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 774 | /* |
| 775 | * If |errno| != EINVAL or seccomp_can_softfail() is false, |
| 776 | * we can proceed. Worst case scenario minijail_enter() will |
| 777 | * abort() if seccomp fails. |
| 778 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 779 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 780 | if (j->flags.seccomp_filter_tsync) { |
| 781 | /* Are the seccomp(2) syscall and the TSYNC option supported? */ |
| 782 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 783 | SECCOMP_FILTER_FLAG_TSYNC, NULL) == -1) { |
| 784 | int saved_errno = errno; |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 785 | if (saved_errno == ENOSYS && seccomp_can_softfail()) { |
| 786 | warn("seccomp(2) syscall not supported"); |
| 787 | clear_seccomp_options(j); |
| 788 | return 0; |
| 789 | } else if (saved_errno == EINVAL && |
| 790 | seccomp_can_softfail()) { |
| 791 | warn( |
| 792 | "seccomp filter thread sync not supported"); |
| 793 | clear_seccomp_options(j); |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 794 | return 0; |
| 795 | } |
| 796 | /* |
| 797 | * Similar logic here. If seccomp_can_softfail() is |
| 798 | * false, or |errno| != ENOSYS, or |errno| != EINVAL, |
| 799 | * we can proceed. Worst case scenario minijail_enter() |
| 800 | * will abort() if seccomp or TSYNC fail. |
| 801 | */ |
| 802 | } |
| 803 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 804 | return 1; |
| 805 | } |
| 806 | |
| 807 | static int parse_seccomp_filters(struct minijail *j, FILE *policy_file) |
| 808 | { |
| 809 | struct sock_fprog *fprog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 810 | int use_ret_trap = |
| 811 | j->flags.seccomp_filter_tsync || j->flags.seccomp_filter_logging; |
| 812 | int allow_logging = j->flags.seccomp_filter_logging; |
| 813 | |
| 814 | if (compile_filter(policy_file, fprog, use_ret_trap, allow_logging)) { |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 815 | free(fprog); |
| 816 | return -1; |
| 817 | } |
| 818 | |
| 819 | j->filter_len = fprog->len; |
| 820 | j->filter_prog = fprog; |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | void API minijail_parse_seccomp_filters(struct minijail *j, const char *path) |
| 825 | { |
| 826 | if (!seccomp_should_parse_filters(j)) |
| 827 | return; |
| 828 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 829 | FILE *file = fopen(path, "r"); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 830 | if (!file) { |
Jorge Lucangeli Obes | 224e427 | 2012-08-02 14:31:39 -0700 | [diff] [blame] | 831 | pdie("failed to open seccomp filter file '%s'", path); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 832 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 833 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 834 | if (parse_seccomp_filters(j, file) != 0) { |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 835 | die("failed to compile seccomp filter BPF program in '%s'", |
| 836 | path); |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 837 | } |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 838 | fclose(file); |
| 839 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 840 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 841 | void API minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd) |
| 842 | { |
| 843 | if (!seccomp_should_parse_filters(j)) |
| 844 | return; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 845 | |
Jorge Lucangeli Obes | 4d4b3be | 2016-08-16 16:58:14 -0400 | [diff] [blame] | 846 | FILE *file = fdopen(fd, "r"); |
| 847 | if (!file) { |
| 848 | pdie("failed to associate stream with fd %d", fd); |
| 849 | } |
| 850 | |
| 851 | if (parse_seccomp_filters(j, file) != 0) { |
| 852 | die("failed to compile seccomp filter BPF program from fd %d", |
| 853 | fd); |
| 854 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 855 | fclose(file); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 856 | } |
| 857 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 858 | int API minijail_use_alt_syscall(struct minijail *j, const char *table) |
| 859 | { |
| 860 | j->alt_syscall_table = strdup(table); |
| 861 | if (!j->alt_syscall_table) |
| 862 | return -ENOMEM; |
| 863 | j->flags.alt_syscall = 1; |
| 864 | return 0; |
| 865 | } |
| 866 | |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 867 | struct marshal_state { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 868 | size_t available; |
| 869 | size_t total; |
| 870 | char *buf; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 871 | }; |
| 872 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 873 | void marshal_state_init(struct marshal_state *state, char *buf, |
| 874 | size_t available) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 875 | { |
| 876 | state->available = available; |
| 877 | state->buf = buf; |
| 878 | state->total = 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 879 | } |
| 880 | |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 881 | void marshal_append(struct marshal_state *state, void *src, size_t length) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 882 | { |
| 883 | size_t copy_len = MIN(state->available, length); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 884 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 885 | /* Up to |available| will be written. */ |
| 886 | if (copy_len) { |
| 887 | memcpy(state->buf, src, copy_len); |
| 888 | state->buf += copy_len; |
| 889 | state->available -= copy_len; |
| 890 | } |
| 891 | /* |total| will contain the expected length. */ |
| 892 | state->total += length; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 893 | } |
| 894 | |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 895 | void marshal_mount(struct marshal_state *state, const struct mountpoint *m) |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 896 | { |
| 897 | marshal_append(state, m->src, strlen(m->src) + 1); |
| 898 | marshal_append(state, m->dest, strlen(m->dest) + 1); |
| 899 | marshal_append(state, m->type, strlen(m->type) + 1); |
| 900 | marshal_append(state, (char *)&m->has_data, sizeof(m->has_data)); |
| 901 | if (m->has_data) |
| 902 | marshal_append(state, m->data, strlen(m->data) + 1); |
| 903 | marshal_append(state, (char *)&m->flags, sizeof(m->flags)); |
| 904 | } |
| 905 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 906 | void minijail_marshal_helper(struct marshal_state *state, |
| 907 | const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 908 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 909 | struct mountpoint *m = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 910 | size_t i; |
| 911 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 912 | marshal_append(state, (char *)j, sizeof(*j)); |
| 913 | if (j->user) |
| 914 | marshal_append(state, j->user, strlen(j->user) + 1); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 915 | if (j->suppl_gid_list) { |
| 916 | marshal_append(state, j->suppl_gid_list, |
| 917 | j->suppl_gid_count * sizeof(gid_t)); |
| 918 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 919 | if (j->chrootdir) |
| 920 | marshal_append(state, j->chrootdir, strlen(j->chrootdir) + 1); |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 921 | if (j->hostname) |
| 922 | marshal_append(state, j->hostname, strlen(j->hostname) + 1); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 923 | if (j->alt_syscall_table) { |
| 924 | marshal_append(state, j->alt_syscall_table, |
| 925 | strlen(j->alt_syscall_table) + 1); |
| 926 | } |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 927 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 928 | struct sock_fprog *fp = j->filter_prog; |
| 929 | marshal_append(state, (char *)fp->filter, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 930 | fp->len * sizeof(struct sock_filter)); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 931 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 932 | for (m = j->mounts_head; m; m = m->next) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 933 | marshal_mount(state, m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 934 | } |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 935 | for (i = 0; i < j->cgroup_count; ++i) |
| 936 | marshal_append(state, j->cgroups[i], strlen(j->cgroups[i]) + 1); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 937 | } |
| 938 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 939 | size_t API minijail_size(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 940 | { |
| 941 | struct marshal_state state; |
| 942 | marshal_state_init(&state, NULL, 0); |
| 943 | minijail_marshal_helper(&state, j); |
| 944 | return state.total; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 945 | } |
| 946 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 947 | int minijail_marshal(const struct minijail *j, char *buf, size_t available) |
| 948 | { |
| 949 | struct marshal_state state; |
| 950 | marshal_state_init(&state, buf, available); |
| 951 | minijail_marshal_helper(&state, j); |
| 952 | return (state.total > available); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 953 | } |
| 954 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 955 | int minijail_unmarshal(struct minijail *j, char *serialized, size_t length) |
| 956 | { |
Jorge Lucangeli Obes | c2ba9f5 | 2015-12-01 07:58:10 -0800 | [diff] [blame] | 957 | size_t i; |
| 958 | size_t count; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 959 | int ret = -EINVAL; |
| 960 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 961 | if (length < sizeof(*j)) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 962 | goto out; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 963 | memcpy((void *)j, serialized, sizeof(*j)); |
| 964 | serialized += sizeof(*j); |
| 965 | length -= sizeof(*j); |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 966 | |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 967 | /* Potentially stale pointers not used as signals. */ |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 968 | j->pid_file_path = NULL; |
| 969 | j->uidmap = NULL; |
| 970 | j->gidmap = NULL; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 971 | j->mounts_head = NULL; |
| 972 | j->mounts_tail = NULL; |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 973 | j->filter_prog = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 974 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 975 | if (j->user) { /* stale pointer */ |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 976 | char *user = consumestr(&serialized, &length); |
| 977 | if (!user) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 978 | goto clear_pointers; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 979 | j->user = strdup(user); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 980 | if (!j->user) |
| 981 | goto clear_pointers; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 982 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 983 | |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 984 | if (j->suppl_gid_list) { /* stale pointer */ |
| 985 | if (j->suppl_gid_count > NGROUPS_MAX) { |
| 986 | goto bad_gid_list; |
| 987 | } |
| 988 | size_t gid_list_size = j->suppl_gid_count * sizeof(gid_t); |
| 989 | void *gid_list_bytes = |
| 990 | consumebytes(gid_list_size, &serialized, &length); |
| 991 | if (!gid_list_bytes) |
| 992 | goto bad_gid_list; |
| 993 | |
| 994 | j->suppl_gid_list = calloc(j->suppl_gid_count, sizeof(gid_t)); |
| 995 | if (!j->suppl_gid_list) |
| 996 | goto bad_gid_list; |
| 997 | |
| 998 | memcpy(j->suppl_gid_list, gid_list_bytes, gid_list_size); |
| 999 | } |
| 1000 | |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1001 | if (j->chrootdir) { /* stale pointer */ |
| 1002 | char *chrootdir = consumestr(&serialized, &length); |
| 1003 | if (!chrootdir) |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1004 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1005 | j->chrootdir = strdup(chrootdir); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1006 | if (!j->chrootdir) |
| 1007 | goto bad_chrootdir; |
Elly Jones | a8d1e1b | 2011-10-21 15:38:00 -0400 | [diff] [blame] | 1008 | } |
| 1009 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1010 | if (j->hostname) { /* stale pointer */ |
| 1011 | char *hostname = consumestr(&serialized, &length); |
| 1012 | if (!hostname) |
| 1013 | goto bad_hostname; |
| 1014 | j->hostname = strdup(hostname); |
| 1015 | if (!j->hostname) |
| 1016 | goto bad_hostname; |
| 1017 | } |
| 1018 | |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1019 | if (j->alt_syscall_table) { /* stale pointer */ |
| 1020 | char *alt_syscall_table = consumestr(&serialized, &length); |
| 1021 | if (!alt_syscall_table) |
| 1022 | goto bad_syscall_table; |
| 1023 | j->alt_syscall_table = strdup(alt_syscall_table); |
| 1024 | if (!j->alt_syscall_table) |
| 1025 | goto bad_syscall_table; |
| 1026 | } |
| 1027 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1028 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 1029 | size_t ninstrs = j->filter_len; |
| 1030 | if (ninstrs > (SIZE_MAX / sizeof(struct sock_filter)) || |
| 1031 | ninstrs > USHRT_MAX) |
| 1032 | goto bad_filters; |
| 1033 | |
| 1034 | size_t program_len = ninstrs * sizeof(struct sock_filter); |
| 1035 | void *program = consumebytes(program_len, &serialized, &length); |
| 1036 | if (!program) |
| 1037 | goto bad_filters; |
| 1038 | |
| 1039 | j->filter_prog = malloc(sizeof(struct sock_fprog)); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1040 | if (!j->filter_prog) |
| 1041 | goto bad_filters; |
| 1042 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1043 | j->filter_prog->len = ninstrs; |
| 1044 | j->filter_prog->filter = malloc(program_len); |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1045 | if (!j->filter_prog->filter) |
| 1046 | goto bad_filter_prog_instrs; |
| 1047 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1048 | memcpy(j->filter_prog->filter, program, program_len); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1049 | } |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1050 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1051 | count = j->mounts_count; |
| 1052 | j->mounts_count = 0; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1053 | for (i = 0; i < count; ++i) { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1054 | unsigned long *flags; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1055 | int *has_data; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1056 | const char *dest; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1057 | const char *type; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1058 | const char *data = NULL; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1059 | const char *src = consumestr(&serialized, &length); |
| 1060 | if (!src) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1061 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1062 | dest = consumestr(&serialized, &length); |
| 1063 | if (!dest) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1064 | goto bad_mounts; |
| 1065 | type = consumestr(&serialized, &length); |
| 1066 | if (!type) |
| 1067 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1068 | has_data = consumebytes(sizeof(*has_data), &serialized, |
| 1069 | &length); |
| 1070 | if (!has_data) |
| 1071 | goto bad_mounts; |
| 1072 | if (*has_data) { |
| 1073 | data = consumestr(&serialized, &length); |
| 1074 | if (!data) |
| 1075 | goto bad_mounts; |
| 1076 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1077 | flags = consumebytes(sizeof(*flags), &serialized, &length); |
| 1078 | if (!flags) |
| 1079 | goto bad_mounts; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1080 | if (minijail_mount_with_data(j, src, dest, type, *flags, data)) |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1081 | goto bad_mounts; |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1082 | } |
| 1083 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1084 | count = j->cgroup_count; |
| 1085 | j->cgroup_count = 0; |
| 1086 | for (i = 0; i < count; ++i) { |
| 1087 | char *cgroup = consumestr(&serialized, &length); |
| 1088 | if (!cgroup) |
| 1089 | goto bad_cgroups; |
| 1090 | j->cgroups[i] = strdup(cgroup); |
| 1091 | if (!j->cgroups[i]) |
| 1092 | goto bad_cgroups; |
| 1093 | ++j->cgroup_count; |
| 1094 | } |
| 1095 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1096 | return 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1097 | |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1098 | bad_cgroups: |
| 1099 | while (j->mounts_head) { |
| 1100 | struct mountpoint *m = j->mounts_head; |
| 1101 | j->mounts_head = j->mounts_head->next; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1102 | free(m->data); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1103 | free(m->type); |
| 1104 | free(m->dest); |
| 1105 | free(m->src); |
| 1106 | free(m); |
| 1107 | } |
| 1108 | for (i = 0; i < j->cgroup_count; ++i) |
| 1109 | free(j->cgroups[i]); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1110 | bad_mounts: |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 1111 | if (j->flags.seccomp_filter && j->filter_len > 0) { |
| 1112 | free(j->filter_prog->filter); |
| 1113 | free(j->filter_prog); |
| 1114 | } |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1115 | bad_filter_prog_instrs: |
| 1116 | if (j->filter_prog) |
| 1117 | free(j->filter_prog); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1118 | bad_filters: |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1119 | if (j->alt_syscall_table) |
| 1120 | free(j->alt_syscall_table); |
| 1121 | bad_syscall_table: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1122 | if (j->chrootdir) |
| 1123 | free(j->chrootdir); |
| 1124 | bad_chrootdir: |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1125 | if (j->hostname) |
| 1126 | free(j->hostname); |
| 1127 | bad_hostname: |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1128 | if (j->suppl_gid_list) |
| 1129 | free(j->suppl_gid_list); |
| 1130 | bad_gid_list: |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1131 | if (j->user) |
| 1132 | free(j->user); |
| 1133 | clear_pointers: |
| 1134 | j->user = NULL; |
Jorge Lucangeli Obes | de02a5b | 2015-12-11 15:28:52 -0800 | [diff] [blame] | 1135 | j->suppl_gid_list = NULL; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1136 | j->chrootdir = NULL; |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1137 | j->hostname = NULL; |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1138 | j->alt_syscall_table = NULL; |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1139 | j->cgroup_count = 0; |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 1140 | out: |
| 1141 | return ret; |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 1142 | } |
| 1143 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1144 | /* |
| 1145 | * mount_one: Applies mounts from @m for @j, recursing as needed. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1146 | * @j Minijail these mounts are for |
| 1147 | * @m Head of list of mounts |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1148 | * |
| 1149 | * Returns 0 for success. |
| 1150 | */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1151 | static int mount_one(const struct minijail *j, struct mountpoint *m) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1152 | { |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1153 | int ret; |
| 1154 | char *dest; |
| 1155 | int remount_ro = 0; |
| 1156 | |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame] | 1157 | /* |dest| has a leading "/". */ |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1158 | if (asprintf(&dest, "%s%s", j->chrootdir, m->dest) < 0) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1159 | return -ENOMEM; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1160 | |
Dylan Reid | eec7796 | 2016-06-30 19:35:10 -0700 | [diff] [blame] | 1161 | if (setup_mount_destination(m->src, dest, j->uid, j->gid)) |
| 1162 | pdie("creating mount target '%s' failed", dest); |
| 1163 | |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1164 | /* |
Jorge Lucangeli Obes | 2b12ba4 | 2016-01-26 10:37:51 -0800 | [diff] [blame] | 1165 | * R/O bind mounts have to be remounted since 'bind' and 'ro' |
| 1166 | * can't both be specified in the original bind mount. |
| 1167 | * Remount R/O after the initial mount. |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1168 | */ |
| 1169 | if ((m->flags & MS_BIND) && (m->flags & MS_RDONLY)) { |
| 1170 | remount_ro = 1; |
| 1171 | m->flags &= ~MS_RDONLY; |
Elly Jones | a105963 | 2011-12-15 15:17:07 -0500 | [diff] [blame] | 1172 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1173 | |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1174 | ret = mount(m->src, dest, m->type, m->flags, m->data); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1175 | if (ret) |
| 1176 | pdie("mount: %s -> %s", m->src, dest); |
| 1177 | |
| 1178 | if (remount_ro) { |
| 1179 | m->flags |= MS_RDONLY; |
| 1180 | ret = mount(m->src, dest, NULL, |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 1181 | m->flags | MS_REMOUNT, m->data); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1182 | if (ret) |
| 1183 | pdie("bind ro: %s -> %s", m->src, dest); |
| 1184 | } |
| 1185 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1186 | free(dest); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1187 | if (m->next) |
| 1188 | return mount_one(j, m->next); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1189 | return ret; |
| 1190 | } |
| 1191 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1192 | static int enter_chroot(const struct minijail *j) |
Jorge Lucangeli Obes | c8b21e1 | 2014-06-13 14:26:16 -0700 | [diff] [blame] | 1193 | { |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1194 | int ret; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1195 | |
| 1196 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1197 | return ret; |
| 1198 | |
| 1199 | if (chroot(j->chrootdir)) |
| 1200 | return -errno; |
| 1201 | |
| 1202 | if (chdir("/")) |
| 1203 | return -errno; |
| 1204 | |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1208 | static int enter_pivot_root(const struct minijail *j) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1209 | { |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1210 | int ret, oldroot, newroot; |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 1211 | |
| 1212 | if (j->mounts_head && (ret = mount_one(j, j->mounts_head))) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1213 | return ret; |
| 1214 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1215 | /* |
| 1216 | * Keep the fd for both old and new root. |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1217 | * It will be used in fchdir(2) later. |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1218 | */ |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1219 | oldroot = open("/", O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1220 | if (oldroot < 0) |
| 1221 | pdie("failed to open / for fchdir"); |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1222 | newroot = open(j->chrootdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1223 | if (newroot < 0) |
| 1224 | pdie("failed to open %s for fchdir", j->chrootdir); |
| 1225 | |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1226 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1227 | * To ensure j->chrootdir is the root of a filesystem, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1228 | * do a self bind mount. |
| 1229 | */ |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1230 | if (mount(j->chrootdir, j->chrootdir, "bind", MS_BIND | MS_REC, "")) |
| 1231 | pdie("failed to bind mount '%s'", j->chrootdir); |
| 1232 | if (chdir(j->chrootdir)) |
| 1233 | return -errno; |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1234 | if (syscall(SYS_pivot_root, ".", ".")) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1235 | pdie("pivot_root"); |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1236 | |
| 1237 | /* |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1238 | * Now the old root is mounted on top of the new root. Use fchdir(2) to |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1239 | * change to the old root and unmount it. |
| 1240 | */ |
| 1241 | if (fchdir(oldroot)) |
| 1242 | pdie("failed to fchdir to old /"); |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1243 | |
| 1244 | /* |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1245 | * If j->flags.skip_remount_private was enabled for minijail_enter(), |
| 1246 | * there could be a shared mount point under |oldroot|. In that case, |
| 1247 | * mounts under this shared mount point will be unmounted below, and |
| 1248 | * this unmounting will propagate to the original mount namespace |
| 1249 | * (because the mount point is shared). To prevent this unexpected |
| 1250 | * unmounting, remove these mounts from their peer groups by recursively |
| 1251 | * remounting them as MS_PRIVATE. |
Hidehiko Abe | 097b719 | 2016-03-16 18:00:36 +0900 | [diff] [blame] | 1252 | */ |
| 1253 | if (mount(NULL, ".", NULL, MS_REC | MS_PRIVATE, NULL)) |
Jorge Lucangeli Obes | 6b0de9b | 2016-03-16 22:41:34 -0700 | [diff] [blame] | 1254 | pdie("failed to mount(/, private) before umount(/)"); |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1255 | /* The old root might be busy, so use lazy unmount. */ |
Yu-Hsi Chiang | e0a530e | 2015-09-08 18:49:49 +0800 | [diff] [blame] | 1256 | if (umount2(".", MNT_DETACH)) |
| 1257 | pdie("umount(/)"); |
| 1258 | /* Change back to the new root. */ |
| 1259 | if (fchdir(newroot)) |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1260 | return -errno; |
Ricky Zhou | bce609d | 2016-03-02 21:47:56 -0800 | [diff] [blame] | 1261 | if (close(oldroot)) |
| 1262 | return -errno; |
| 1263 | if (close(newroot)) |
| 1264 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1265 | if (chroot("/")) |
| 1266 | return -errno; |
Jorge Lucangeli Obes | 46a5509 | 2015-10-12 15:31:59 -0700 | [diff] [blame] | 1267 | /* Set correct CWD for getcwd(3). */ |
| 1268 | if (chdir("/")) |
| 1269 | return -errno; |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1274 | static int mount_tmp(const struct minijail *j) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1275 | { |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1276 | const char fmt[] = "size=%zu,mode=1777"; |
| 1277 | /* Count for the user storing ULLONG_MAX literally + extra space. */ |
| 1278 | char data[sizeof(fmt) + sizeof("18446744073709551615ULL")]; |
| 1279 | int ret; |
| 1280 | |
| 1281 | ret = snprintf(data, sizeof(data), fmt, j->tmpfs_size); |
| 1282 | |
| 1283 | if (ret <= 0) |
| 1284 | pdie("tmpfs size spec error"); |
| 1285 | else if ((size_t)ret >= sizeof(data)) |
| 1286 | pdie("tmpfs size spec too large"); |
Mike Frysinger | b91d404 | 2017-01-13 19:03:34 -0500 | [diff] [blame] | 1287 | return mount("none", "/tmp", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1288 | data); |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1291 | static int remount_proc_readonly(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1292 | { |
| 1293 | const char *kProcPath = "/proc"; |
| 1294 | const unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1295 | /* |
| 1296 | * 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] | 1297 | * /proc in our namespace, which means using MS_REMOUNT here would |
| 1298 | * mutate our parent's mount as well, even though we're in a VFS |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1299 | * namespace (!). Instead, remove their mount from our namespace lazily |
| 1300 | * (MNT_DETACH) and make our own. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1301 | */ |
Jorge Lucangeli Obes | df7fab1 | 2016-06-01 17:15:31 -0700 | [diff] [blame] | 1302 | if (umount2(kProcPath, MNT_DETACH)) { |
| 1303 | /* |
| 1304 | * If we are in a new user namespace, umount(2) will fail. |
| 1305 | * See http://man7.org/linux/man-pages/man7/user_namespaces.7.html |
| 1306 | */ |
| 1307 | if (j->flags.userns) { |
| 1308 | info("umount(/proc, MNT_DETACH) failed, " |
| 1309 | "this is expected when using user namespaces"); |
| 1310 | } else { |
| 1311 | return -errno; |
| 1312 | } |
| 1313 | } |
Mike Frysinger | 3ba8157 | 2017-01-17 23:33:28 -0500 | [diff] [blame] | 1314 | if (mount("proc", kProcPath, "proc", kSafeFlags | MS_RDONLY, "")) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1315 | return -errno; |
| 1316 | return 0; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1317 | } |
| 1318 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1319 | static void kill_child_and_die(const struct minijail *j, const char *msg) |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1320 | { |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1321 | kill(j->initpid, SIGKILL); |
| 1322 | die("%s", msg); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1323 | } |
| 1324 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1325 | static void write_pid_file_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1326 | { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1327 | if (write_pid_to_path(j->initpid, j->pid_file_path)) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1328 | kill_child_and_die(j, "failed to write pid file"); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1331 | static void add_to_cgroups_or_die(const struct minijail *j) |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 1332 | { |
| 1333 | size_t i; |
| 1334 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1335 | for (i = 0; i < j->cgroup_count; ++i) { |
Keshav Santhanam | db6dab4 | 2016-08-10 16:33:34 -0700 | [diff] [blame] | 1336 | if (write_pid_to_path(j->initpid, j->cgroups[i])) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1337 | kill_child_and_die(j, "failed to add to cgroups"); |
| 1338 | } |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 1339 | } |
| 1340 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 1341 | static void set_rlimits_or_die(const struct minijail *j) |
| 1342 | { |
| 1343 | size_t i; |
| 1344 | |
| 1345 | for (i = 0; i < j->rlimit_count; ++i) { |
| 1346 | struct rlimit limit; |
| 1347 | limit.rlim_cur = j->rlimits[i].cur; |
| 1348 | limit.rlim_max = j->rlimits[i].max; |
| 1349 | if (prlimit(j->initpid, j->rlimits[i].type, &limit, NULL)) |
| 1350 | kill_child_and_die(j, "failed to set rlimit"); |
| 1351 | } |
| 1352 | } |
| 1353 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1354 | static void write_ugid_maps_or_die(const struct minijail *j) |
| 1355 | { |
| 1356 | if (j->uidmap && write_proc_file(j->initpid, j->uidmap, "uid_map") != 0) |
| 1357 | kill_child_and_die(j, "failed to write uid_map"); |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1358 | if (j->gidmap && j->flags.disable_setgroups) { |
| 1359 | /* Older kernels might not have the /proc/<pid>/setgroups files. */ |
| 1360 | int ret = write_proc_file(j->initpid, "deny", "setgroups"); |
Mike Frysinger | eea841b | 2017-01-13 18:11:57 -0500 | [diff] [blame] | 1361 | if (ret != 0) { |
Mike Frysinger | 6b190c0 | 2017-01-04 17:18:42 -0500 | [diff] [blame] | 1362 | if (ret == -ENOENT) { |
| 1363 | /* See http://man7.org/linux/man-pages/man7/user_namespaces.7.html. */ |
| 1364 | warn("could not disable setgroups(2)"); |
| 1365 | } else |
| 1366 | kill_child_and_die(j, "failed to disable setgroups(2)"); |
| 1367 | } |
| 1368 | } |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1369 | if (j->gidmap && write_proc_file(j->initpid, j->gidmap, "gid_map") != 0) |
| 1370 | kill_child_and_die(j, "failed to write gid_map"); |
| 1371 | } |
| 1372 | |
| 1373 | static void enter_user_namespace(const struct minijail *j) |
| 1374 | { |
| 1375 | if (j->uidmap && setresuid(0, 0, 0)) |
| 1376 | pdie("user_namespaces: setresuid(0, 0, 0) failed"); |
| 1377 | if (j->gidmap && setresgid(0, 0, 0)) |
| 1378 | pdie("user_namespaces: setresgid(0, 0, 0) failed"); |
| 1379 | } |
| 1380 | |
| 1381 | static void parent_setup_complete(int *pipe_fds) |
| 1382 | { |
| 1383 | close(pipe_fds[0]); |
| 1384 | close(pipe_fds[1]); |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * wait_for_parent_setup: Called by the child process to wait for any |
| 1389 | * further parent-side setup to complete before continuing. |
| 1390 | */ |
| 1391 | static void wait_for_parent_setup(int *pipe_fds) |
| 1392 | { |
| 1393 | char buf; |
| 1394 | |
| 1395 | close(pipe_fds[1]); |
| 1396 | |
| 1397 | /* Wait for parent to complete setup and close the pipe. */ |
| 1398 | if (read(pipe_fds[0], &buf, 1) != 0) |
| 1399 | die("failed to sync with parent"); |
| 1400 | close(pipe_fds[0]); |
| 1401 | } |
| 1402 | |
| 1403 | static void drop_ugid(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1404 | { |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1405 | if (j->flags.inherit_suppl_gids + j->flags.keep_suppl_gids + |
| 1406 | j->flags.set_suppl_gids > 1) { |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 1407 | die("can only do one of inherit, keep, or set supplementary " |
| 1408 | "groups"); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1411 | if (j->flags.inherit_suppl_gids) { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1412 | if (initgroups(j->user, j->usergid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1413 | pdie("initgroups(%s, %d) failed", j->user, j->usergid); |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1414 | } else if (j->flags.set_suppl_gids) { |
| 1415 | if (setgroups(j->suppl_gid_count, j->suppl_gid_list)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1416 | pdie("setgroups(suppl_gids) failed"); |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1417 | } else if (!j->flags.keep_suppl_gids) { |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1418 | /* |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 1419 | * Only attempt to clear supplementary groups if we are changing |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1420 | * users or groups. |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 1421 | */ |
Jorge Lucangeli Obes | 2449956 | 2016-12-01 11:59:27 -0500 | [diff] [blame] | 1422 | if ((j->flags.uid || j->flags.gid) && setgroups(0, NULL)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1423 | pdie("setgroups(0, NULL) failed"); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | if (j->flags.gid && setresgid(j->gid, j->gid, j->gid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1427 | pdie("setresgid(%d, %d, %d) failed", j->gid, j->gid, j->gid); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1428 | |
| 1429 | if (j->flags.uid && setresuid(j->uid, j->uid, j->uid)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1430 | pdie("setresuid(%d, %d, %d) failed", j->uid, j->uid, j->uid); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1433 | static void drop_capbset(uint64_t keep_mask, unsigned int last_valid_cap) |
| 1434 | { |
| 1435 | const uint64_t one = 1; |
| 1436 | unsigned int i; |
| 1437 | for (i = 0; i < sizeof(keep_mask) * 8 && i <= last_valid_cap; ++i) { |
| 1438 | if (keep_mask & (one << i)) |
| 1439 | continue; |
| 1440 | if (prctl(PR_CAPBSET_DROP, i)) |
| 1441 | pdie("could not drop capability from bounding set"); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1445 | static void drop_caps(const struct minijail *j, unsigned int last_valid_cap) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1446 | { |
Jorge Lucangeli Obes | 7ea269e | 2016-02-26 22:07:09 -0800 | [diff] [blame] | 1447 | if (!j->flags.use_caps) |
| 1448 | return; |
| 1449 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1450 | cap_t caps = cap_get_proc(); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1451 | cap_value_t flag[1]; |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1452 | const size_t ncaps = sizeof(j->caps) * 8; |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1453 | const uint64_t one = 1; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1454 | unsigned int i; |
| 1455 | if (!caps) |
| 1456 | die("can't get process caps"); |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1457 | if (cap_clear(caps)) |
| 1458 | die("can't clear caps"); |
| 1459 | |
| 1460 | for (i = 0; i < ncaps && i <= last_valid_cap; ++i) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1461 | /* Keep CAP_SETPCAP for dropping bounding set bits. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1462 | if (i != CAP_SETPCAP && !(j->caps & (one << i))) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1463 | continue; |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1464 | flag[0] = i; |
| 1465 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1466 | die("can't add effective cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1467 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1468 | die("can't add permitted cap"); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1469 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_SET)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1470 | die("can't add inheritable cap"); |
| 1471 | } |
| 1472 | if (cap_set_proc(caps)) |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1473 | die("can't apply initial cleaned capset"); |
| 1474 | |
| 1475 | /* |
| 1476 | * Instead of dropping bounding set first, do it here in case |
| 1477 | * the caller had a more permissive bounding set which could |
| 1478 | * have been used above to raise a capability that wasn't already |
| 1479 | * present. This requires CAP_SETPCAP, so we raised/kept it above. |
| 1480 | */ |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1481 | drop_capbset(j->caps, last_valid_cap); |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1482 | |
| 1483 | /* If CAP_SETPCAP wasn't specifically requested, now we remove it. */ |
Kees Cook | e5609ac | 2013-02-06 14:12:41 -0800 | [diff] [blame] | 1484 | if ((j->caps & (one << CAP_SETPCAP)) == 0) { |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1485 | flag[0] = CAP_SETPCAP; |
| 1486 | if (cap_set_flag(caps, CAP_EFFECTIVE, 1, flag, CAP_CLEAR)) |
| 1487 | die("can't clear effective cap"); |
| 1488 | if (cap_set_flag(caps, CAP_PERMITTED, 1, flag, CAP_CLEAR)) |
| 1489 | die("can't clear permitted cap"); |
| 1490 | if (cap_set_flag(caps, CAP_INHERITABLE, 1, flag, CAP_CLEAR)) |
| 1491 | die("can't clear inheritable cap"); |
| 1492 | } |
| 1493 | |
| 1494 | if (cap_set_proc(caps)) |
| 1495 | die("can't apply final cleaned capset"); |
| 1496 | |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1497 | /* |
| 1498 | * If ambient capabilities are supported, clear all capabilities first, |
| 1499 | * then raise the requested ones. |
| 1500 | */ |
| 1501 | if (j->flags.set_ambient_caps) { |
| 1502 | if (!cap_ambient_supported()) { |
| 1503 | pdie("ambient capabilities not supported"); |
| 1504 | } |
Jorge Lucangeli Obes | f6058c3 | 2017-04-26 10:26:59 -0400 | [diff] [blame] | 1505 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != |
| 1506 | 0) { |
Jorge Lucangeli Obes | a6eb21a | 2017-04-20 10:44:00 -0400 | [diff] [blame] | 1507 | pdie("can't clear ambient capabilities"); |
| 1508 | } |
| 1509 | |
| 1510 | for (i = 0; i < ncaps && i <= last_valid_cap; ++i) { |
| 1511 | if (!(j->caps & (one << i))) |
| 1512 | continue; |
| 1513 | |
| 1514 | if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, i, 0, |
| 1515 | 0) != 0) { |
| 1516 | pdie("prctl(PR_CAP_AMBIENT, " |
| 1517 | "PR_CAP_AMBIENT_RAISE, %u) failed", |
| 1518 | i); |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
Kees Cook | 323878a | 2013-02-05 15:35:24 -0800 | [diff] [blame] | 1523 | cap_free(caps); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1524 | } |
| 1525 | |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 1526 | static void set_seccomp_filter(const struct minijail *j) |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1527 | { |
| 1528 | /* |
| 1529 | * Set no_new_privs. See </kernel/seccomp.c> and </kernel/sys.c> |
| 1530 | * in the kernel source tree for an explanation of the parameters. |
| 1531 | */ |
| 1532 | if (j->flags.no_new_privs) { |
| 1533 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) |
| 1534 | pdie("prctl(PR_SET_NO_NEW_PRIVS)"); |
| 1535 | } |
| 1536 | |
| 1537 | /* |
Jorge Lucangeli Obes | 2413f37 | 2016-04-06 18:43:10 -0700 | [diff] [blame] | 1538 | * Code running with ASan |
| 1539 | * (https://github.com/google/sanitizers/wiki/AddressSanitizer) |
| 1540 | * will make system calls not included in the syscall filter policy, |
| 1541 | * which will likely crash the program. Skip setting seccomp filter in |
| 1542 | * that case. |
| 1543 | * 'running_with_asan()' has no inputs and is completely defined at |
| 1544 | * build time, so this cannot be used by an attacker to skip setting |
| 1545 | * seccomp filter. |
| 1546 | */ |
| 1547 | if (j->flags.seccomp_filter && running_with_asan()) { |
| 1548 | warn("running with ASan, not setting seccomp filter"); |
| 1549 | return; |
| 1550 | } |
| 1551 | |
Jorge Lucangeli Obes | 713f6fb | 2016-10-03 13:03:25 -0400 | [diff] [blame] | 1552 | if (j->flags.seccomp_filter) { |
| 1553 | if (j->flags.seccomp_filter_logging) { |
| 1554 | /* |
| 1555 | * If logging seccomp filter failures, |
| 1556 | * install the SIGSYS handler first. |
| 1557 | */ |
| 1558 | if (install_sigsys_handler()) |
| 1559 | pdie("failed to install SIGSYS handler"); |
| 1560 | warn("logging seccomp filter failures"); |
| 1561 | } else if (j->flags.seccomp_filter_tsync) { |
| 1562 | /* |
| 1563 | * If setting thread sync, |
| 1564 | * reset the SIGSYS signal handler so that |
| 1565 | * the entire thread group is killed. |
| 1566 | */ |
| 1567 | if (signal(SIGSYS, SIG_DFL) == SIG_ERR) |
| 1568 | pdie("failed to reset SIGSYS disposition"); |
| 1569 | info("reset SIGSYS disposition"); |
| 1570 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Install the syscall filter. |
| 1575 | */ |
| 1576 | if (j->flags.seccomp_filter) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 1577 | if (j->flags.seccomp_filter_tsync) { |
| 1578 | if (sys_seccomp(SECCOMP_SET_MODE_FILTER, |
| 1579 | SECCOMP_FILTER_FLAG_TSYNC, |
| 1580 | j->filter_prog)) { |
| 1581 | pdie("seccomp(tsync) failed"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1582 | } |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 1583 | } else { |
| 1584 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, |
| 1585 | j->filter_prog)) { |
| 1586 | pdie("prctl(seccomp_filter) failed"); |
| 1587 | } |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1588 | } |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1589 | } |
| 1590 | } |
| 1591 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 1592 | static pid_t forward_pid = -1; |
| 1593 | |
| 1594 | static void forward_signal(__attribute__((unused)) int nr, |
| 1595 | __attribute__((unused)) siginfo_t *siginfo, |
| 1596 | __attribute__((unused)) void *void_context) |
| 1597 | { |
| 1598 | if (forward_pid != -1) { |
| 1599 | kill(forward_pid, nr); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | static void install_signal_handlers(void) |
| 1604 | { |
| 1605 | struct sigaction act; |
| 1606 | |
| 1607 | memset(&act, 0, sizeof(act)); |
| 1608 | act.sa_sigaction = &forward_signal; |
| 1609 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
| 1610 | |
| 1611 | /* Handle all signals, except SIGCHLD. */ |
| 1612 | for (int nr = 1; nr < NSIG; nr++) { |
| 1613 | /* |
| 1614 | * We don't care if we get EINVAL: that just means that we |
| 1615 | * can't handle this signal, so let's skip it and continue. |
| 1616 | */ |
| 1617 | sigaction(nr, &act, NULL); |
| 1618 | } |
| 1619 | /* Reset SIGCHLD's handler. */ |
| 1620 | signal(SIGCHLD, SIG_DFL); |
| 1621 | |
| 1622 | /* Handle real-time signals. */ |
| 1623 | for (int nr = SIGRTMIN; nr <= SIGRTMAX; nr++) { |
| 1624 | sigaction(nr, &act, NULL); |
| 1625 | } |
| 1626 | } |
| 1627 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1628 | void API minijail_enter(const struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1629 | { |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1630 | /* |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1631 | * If we're dropping caps, get the last valid cap from /proc now, |
| 1632 | * since /proc can be unmounted before drop_caps() is called. |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1633 | */ |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1634 | unsigned int last_valid_cap = 0; |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1635 | if (j->flags.capbset_drop || j->flags.use_caps) |
Jorge Lucangeli Obes | 43e29b3 | 2015-12-08 21:07:14 -0800 | [diff] [blame] | 1636 | last_valid_cap = get_last_valid_cap(); |
Dylan Reid | f682d47 | 2015-09-17 21:39:07 -0700 | [diff] [blame] | 1637 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1638 | if (j->flags.pids) |
| 1639 | die("tried to enter a pid-namespaced jail;" |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1640 | " try minijail_run()?"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1641 | |
Lutz Justen | 13807cb | 2017-01-03 17:11:55 +0100 | [diff] [blame] | 1642 | if (j->flags.inherit_suppl_gids && !j->user) |
Jorge Lucangeli Obes | 3454319 | 2017-01-11 16:07:57 -0500 | [diff] [blame] | 1643 | die("cannot inherit supplementary groups without setting a " |
| 1644 | "username"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1645 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1646 | /* |
| 1647 | * We can't recover from failures if we've dropped privileges partially, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1648 | * so we don't even try. If any of our operations fail, we abort() the |
| 1649 | * entire process. |
| 1650 | */ |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1651 | if (j->flags.enter_vfs && setns(j->mountns_fd, CLONE_NEWNS)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1652 | pdie("setns(CLONE_NEWNS) failed"); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 1653 | |
Jorge Lucangeli Obes | 805be39 | 2015-10-12 15:55:59 -0700 | [diff] [blame] | 1654 | if (j->flags.vfs) { |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1655 | if (unshare(CLONE_NEWNS)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1656 | pdie("unshare(CLONE_NEWNS) failed"); |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1657 | /* |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1658 | * Unless asked not to, remount all filesystems as private. |
| 1659 | * If they are shared, new bind mounts will creep out of our |
| 1660 | * namespace. |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1661 | * https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt |
| 1662 | */ |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1663 | if (!j->flags.skip_remount_private) { |
| 1664 | if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1665 | pdie("mount(NULL, /, NULL, MS_REC | MS_PRIVATE," |
| 1666 | " NULL) failed"); |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1667 | } |
Jorge Lucangeli Obes | f7a3868 | 2015-12-04 15:43:30 -0800 | [diff] [blame] | 1668 | } |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 1669 | |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 1670 | if (j->flags.ipc && unshare(CLONE_NEWIPC)) { |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1671 | pdie("unshare(CLONE_NEWIPC) failed"); |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 1672 | } |
| 1673 | |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 1674 | if (j->flags.uts) { |
| 1675 | if (unshare(CLONE_NEWUTS)) |
| 1676 | pdie("unshare(CLONE_NEWUTS) failed"); |
| 1677 | |
| 1678 | if (j->hostname && sethostname(j->hostname, strlen(j->hostname))) |
| 1679 | pdie("sethostname(%s) failed", j->hostname); |
| 1680 | } |
| 1681 | |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1682 | if (j->flags.enter_net) { |
| 1683 | if (setns(j->netns_fd, CLONE_NEWNET)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1684 | pdie("setns(CLONE_NEWNET) failed"); |
Mike Frysinger | 7559dfe | 2016-11-15 18:58:39 -0500 | [diff] [blame] | 1685 | } else if (j->flags.net) { |
| 1686 | if (unshare(CLONE_NEWNET)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1687 | pdie("unshare(CLONE_NEWNET) failed"); |
| 1688 | config_net_loopback(); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 1689 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1690 | |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 1691 | if (j->flags.ns_cgroups && unshare(CLONE_NEWCGROUP)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1692 | pdie("unshare(CLONE_NEWCGROUP) failed"); |
Dylan Reid | 4cbc2a5 | 2016-06-17 19:06:07 -0700 | [diff] [blame] | 1693 | |
Chirantan Ekbote | 866bb3a | 2017-02-07 12:26:42 -0800 | [diff] [blame] | 1694 | if (j->flags.new_session_keyring) { |
| 1695 | if (syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING, NULL) < 0) |
| 1696 | pdie("keyctl(KEYCTL_JOIN_SESSION_KEYRING) failed"); |
| 1697 | } |
| 1698 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 1699 | if (j->flags.chroot && enter_chroot(j)) |
| 1700 | pdie("chroot"); |
| 1701 | |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 1702 | if (j->flags.pivot_root && enter_pivot_root(j)) |
| 1703 | pdie("pivot_root"); |
| 1704 | |
Martin Pelikán | ab9eb44 | 2017-01-25 11:53:58 +1100 | [diff] [blame] | 1705 | if (j->flags.mount_tmp && mount_tmp(j)) |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 1706 | pdie("mount_tmp"); |
| 1707 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 1708 | if (j->flags.remount_proc_ro && remount_proc_readonly(j)) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1709 | pdie("remount"); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1710 | |
Jorge Lucangeli Obes | f9fcdbe | 2016-02-19 15:04:09 -0800 | [diff] [blame] | 1711 | /* |
| 1712 | * If we're only dropping capabilities from the bounding set, but not |
| 1713 | * from the thread's (permitted|inheritable|effective) sets, do it now. |
| 1714 | */ |
| 1715 | if (j->flags.capbset_drop) { |
| 1716 | drop_capbset(j->cap_bset, last_valid_cap); |
| 1717 | } |
| 1718 | |
| 1719 | if (j->flags.use_caps) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1720 | /* |
| 1721 | * POSIX capabilities are a bit tricky. If we drop our |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1722 | * capability to change uids, our attempt to use setuid() |
| 1723 | * below will fail. Hang on to root caps across setuid(), then |
| 1724 | * lock securebits. |
| 1725 | */ |
| 1726 | if (prctl(PR_SET_KEEPCAPS, 1)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1727 | pdie("prctl(PR_SET_KEEPCAPS) failed"); |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 1728 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 1729 | if (lock_securebits() < 0) { |
| 1730 | pdie("locking securebits failed"); |
Jorge Lucangeli Obes | f783b52 | 2016-03-14 14:34:10 -0700 | [diff] [blame] | 1731 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1732 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1733 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 1734 | if (j->flags.no_new_privs) { |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 1735 | /* |
| 1736 | * If we're setting no_new_privs, we can drop privileges |
| 1737 | * before setting seccomp filter. This way filter policies |
| 1738 | * don't need to allow privilege-dropping syscalls. |
| 1739 | */ |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1740 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 1741 | drop_caps(j, last_valid_cap); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1742 | set_seccomp_filter(j); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1743 | } else { |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1744 | /* |
| 1745 | * If we're not setting no_new_privs, |
| 1746 | * we need to set seccomp filter *before* dropping privileges. |
| 1747 | * WARNING: this means that filter policies *must* allow |
| 1748 | * setgroups()/setresgid()/setresuid() for dropping root and |
| 1749 | * capget()/capset()/prctl() for dropping caps. |
| 1750 | */ |
| 1751 | set_seccomp_filter(j); |
Jorge Lucangeli Obes | 6201cf5 | 2012-08-23 11:42:27 -0700 | [diff] [blame] | 1752 | drop_ugid(j); |
Jorge Lucangeli Obes | d8c8205 | 2016-02-25 16:00:32 -0800 | [diff] [blame] | 1753 | drop_caps(j, last_valid_cap); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1754 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1755 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1756 | /* |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1757 | * Select the specified alternate syscall table. The table must not |
| 1758 | * block prctl(2) if we're using seccomp as well. |
| 1759 | */ |
| 1760 | if (j->flags.alt_syscall) { |
| 1761 | if (prctl(PR_ALT_SYSCALL, 1, j->alt_syscall_table)) |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1762 | pdie("prctl(PR_ALT_SYSCALL) failed"); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | /* |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1766 | * seccomp has to come last since it cuts off all the other |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1767 | * privilege-dropping syscalls :) |
| 1768 | */ |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1769 | if (j->flags.seccomp && prctl(PR_SET_SECCOMP, 1)) { |
Jorge Lucangeli Obes | 7b2e29c | 2016-08-04 12:21:03 -0400 | [diff] [blame] | 1770 | if ((errno == EINVAL) && seccomp_can_softfail()) { |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1771 | warn("seccomp not supported"); |
| 1772 | return; |
| 1773 | } |
Jorge Lucangeli Obes | 457a5e3 | 2016-11-23 15:18:56 -0500 | [diff] [blame] | 1774 | pdie("prctl(PR_SET_SECCOMP) failed"); |
Utkarsh Sanghi | 0ef8a66 | 2014-08-18 15:50:11 -0700 | [diff] [blame] | 1775 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1776 | } |
| 1777 | |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 1778 | /* TODO(wad): will visibility affect this variable? */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1779 | static int init_exitstatus = 0; |
| 1780 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1781 | void init_term(int __attribute__ ((unused)) sig) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1782 | { |
| 1783 | _exit(init_exitstatus); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1784 | } |
| 1785 | |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 1786 | void init(pid_t rootpid) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1787 | { |
| 1788 | pid_t pid; |
| 1789 | int status; |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 1790 | /* So that we exit with the right status. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1791 | signal(SIGTERM, init_term); |
Jorge Lucangeli Obes | db0bc67 | 2016-08-03 10:45:21 -0400 | [diff] [blame] | 1792 | /* TODO(wad): self jail with seccomp filters here. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1793 | while ((pid = wait(&status)) > 0) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 1794 | /* |
| 1795 | * This loop will only end when either there are no processes |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1796 | * left inside our pid namespace or we get a signal. |
| 1797 | */ |
| 1798 | if (pid == rootpid) |
| 1799 | init_exitstatus = status; |
| 1800 | } |
| 1801 | if (!WIFEXITED(init_exitstatus)) |
| 1802 | _exit(MINIJAIL_ERR_INIT); |
| 1803 | _exit(WEXITSTATUS(init_exitstatus)); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1804 | } |
| 1805 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1806 | int API minijail_from_fd(int fd, struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1807 | { |
| 1808 | size_t sz = 0; |
| 1809 | size_t bytes = read(fd, &sz, sizeof(sz)); |
| 1810 | char *buf; |
| 1811 | int r; |
| 1812 | if (sizeof(sz) != bytes) |
| 1813 | return -EINVAL; |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1814 | if (sz > USHRT_MAX) /* arbitrary sanity check */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1815 | return -E2BIG; |
| 1816 | buf = malloc(sz); |
| 1817 | if (!buf) |
| 1818 | return -ENOMEM; |
| 1819 | bytes = read(fd, buf, sz); |
| 1820 | if (bytes != sz) { |
| 1821 | free(buf); |
| 1822 | return -EINVAL; |
| 1823 | } |
| 1824 | r = minijail_unmarshal(j, buf, sz); |
| 1825 | free(buf); |
| 1826 | return r; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1827 | } |
| 1828 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1829 | int API minijail_to_fd(struct minijail *j, int fd) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1830 | { |
| 1831 | char *buf; |
| 1832 | size_t sz = minijail_size(j); |
| 1833 | ssize_t written; |
| 1834 | int r; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1835 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1836 | if (!sz) |
| 1837 | return -EINVAL; |
| 1838 | buf = malloc(sz); |
| 1839 | r = minijail_marshal(j, buf, sz); |
| 1840 | if (r) { |
| 1841 | free(buf); |
| 1842 | return r; |
| 1843 | } |
| 1844 | /* Sends [size][minijail]. */ |
| 1845 | written = write(fd, &sz, sizeof(sz)); |
| 1846 | if (written != sizeof(sz)) { |
| 1847 | free(buf); |
| 1848 | return -EFAULT; |
| 1849 | } |
| 1850 | written = write(fd, buf, sz); |
| 1851 | if (written < 0 || (size_t) written != sz) { |
| 1852 | free(buf); |
| 1853 | return -EFAULT; |
| 1854 | } |
| 1855 | free(buf); |
| 1856 | return 0; |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 1857 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1858 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1859 | int setup_preload(void) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1860 | { |
Daniel Erat | 5b7a318 | 2015-08-19 16:06:22 -0600 | [diff] [blame] | 1861 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 1862 | /* Don't use LDPRELOAD on Android. */ |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1863 | return 0; |
| 1864 | #else |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1865 | char *oldenv = getenv(kLdPreloadEnvVar) ? : ""; |
| 1866 | char *newenv = malloc(strlen(oldenv) + 2 + strlen(PRELOADPATH)); |
| 1867 | if (!newenv) |
| 1868 | return -ENOMEM; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1869 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1870 | /* Only insert a separating space if we have something to separate... */ |
| 1871 | sprintf(newenv, "%s%s%s", oldenv, strlen(oldenv) ? " " : "", |
| 1872 | PRELOADPATH); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1873 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1874 | /* setenv() makes a copy of the string we give it. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1875 | setenv(kLdPreloadEnvVar, newenv, 1); |
| 1876 | free(newenv); |
| 1877 | return 0; |
Jorge Lucangeli Obes | a21c8fc | 2015-07-15 16:22:34 -0700 | [diff] [blame] | 1878 | #endif |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 1879 | } |
| 1880 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 1881 | static int setup_pipe(int fds[2]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1882 | { |
| 1883 | int r = pipe(fds); |
| 1884 | char fd_buf[11]; |
| 1885 | if (r) |
| 1886 | return r; |
| 1887 | r = snprintf(fd_buf, sizeof(fd_buf), "%d", fds[0]); |
| 1888 | if (r <= 0) |
| 1889 | return -EINVAL; |
| 1890 | setenv(kFdEnvVar, fd_buf, 1); |
| 1891 | return 0; |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 1892 | } |
| 1893 | |
Jorge Lucangeli Obes | 0b20877 | 2017-04-19 14:15:46 -0400 | [diff] [blame] | 1894 | static int close_open_fds(int *inheritable_fds, size_t size) |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 1895 | { |
| 1896 | const char *kFdPath = "/proc/self/fd"; |
| 1897 | |
| 1898 | DIR *d = opendir(kFdPath); |
| 1899 | struct dirent *dir_entry; |
| 1900 | |
| 1901 | if (d == NULL) |
| 1902 | return -1; |
| 1903 | int dir_fd = dirfd(d); |
| 1904 | while ((dir_entry = readdir(d)) != NULL) { |
| 1905 | size_t i; |
| 1906 | char *end; |
| 1907 | bool should_close = true; |
| 1908 | const int fd = strtol(dir_entry->d_name, &end, 10); |
| 1909 | |
| 1910 | if ((*end) != '\0') { |
| 1911 | continue; |
| 1912 | } |
| 1913 | /* |
| 1914 | * We might have set up some pipes that we want to share with |
| 1915 | * the parent process, and should not be closed. |
| 1916 | */ |
| 1917 | for (i = 0; i < size; ++i) { |
| 1918 | if (fd == inheritable_fds[i]) { |
| 1919 | should_close = false; |
| 1920 | break; |
| 1921 | } |
| 1922 | } |
| 1923 | /* Also avoid closing the directory fd. */ |
| 1924 | if (should_close && fd != dir_fd) |
| 1925 | close(fd); |
| 1926 | } |
| 1927 | closedir(d); |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1931 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1932 | char *const argv[], pid_t *pchild_pid, |
| 1933 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1934 | int use_preload); |
| 1935 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 1936 | int API minijail_run(struct minijail *j, const char *filename, |
| 1937 | char *const argv[]) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1938 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1939 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1940 | true); |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | int API minijail_run_pid(struct minijail *j, const char *filename, |
| 1944 | char *const argv[], pid_t *pchild_pid) |
| 1945 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1946 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1947 | NULL, NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | int API minijail_run_pipe(struct minijail *j, const char *filename, |
Jorge Lucangeli Obes | 6537a56 | 2012-09-05 10:39:40 -0700 | [diff] [blame] | 1951 | char *const argv[], int *pstdin_fd) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1952 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1953 | return minijail_run_internal(j, filename, argv, NULL, pstdin_fd, |
| 1954 | NULL, NULL, true); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1957 | 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] | 1958 | char *const argv[], pid_t *pchild_pid, |
| 1959 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd) |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1960 | { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1961 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1962 | pstdin_fd, pstdout_fd, pstderr_fd, true); |
| 1963 | } |
| 1964 | |
| 1965 | int API minijail_run_no_preload(struct minijail *j, const char *filename, |
| 1966 | char *const argv[]) |
| 1967 | { |
| 1968 | return minijail_run_internal(j, filename, argv, NULL, NULL, NULL, NULL, |
| 1969 | false); |
| 1970 | } |
| 1971 | |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1972 | int API minijail_run_pid_pipes_no_preload(struct minijail *j, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1973 | const char *filename, |
| 1974 | char *const argv[], |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1975 | pid_t *pchild_pid, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 1976 | int *pstdin_fd, int *pstdout_fd, |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 1977 | int *pstderr_fd) |
| 1978 | { |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 1979 | return minijail_run_internal(j, filename, argv, pchild_pid, |
| 1980 | pstdin_fd, pstdout_fd, pstderr_fd, false); |
| 1981 | } |
| 1982 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 1983 | int minijail_run_internal(struct minijail *j, const char *filename, |
| 1984 | char *const argv[], pid_t *pchild_pid, |
| 1985 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd, |
| 1986 | int use_preload) |
| 1987 | { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1988 | char *oldenv, *oldenv_copy = NULL; |
| 1989 | pid_t child_pid; |
| 1990 | int pipe_fds[2]; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 1991 | int stdin_fds[2]; |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 1992 | int stdout_fds[2]; |
| 1993 | int stderr_fds[2]; |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 1994 | int child_sync_pipe_fds[2]; |
| 1995 | int sync_child = 0; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 1996 | int ret; |
Elly Jones | a05d7bb | 2012-06-14 14:09:27 -0400 | [diff] [blame] | 1997 | /* We need to remember this across the minijail_preexec() call. */ |
| 1998 | int pid_namespace = j->flags.pids; |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 1999 | int do_init = j->flags.do_init; |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 2000 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2001 | if (use_preload) { |
| 2002 | oldenv = getenv(kLdPreloadEnvVar); |
| 2003 | if (oldenv) { |
| 2004 | oldenv_copy = strdup(oldenv); |
| 2005 | if (!oldenv_copy) |
| 2006 | return -ENOMEM; |
| 2007 | } |
| 2008 | |
| 2009 | if (setup_preload()) |
| 2010 | return -EFAULT; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2011 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2012 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2013 | if (!use_preload) { |
Luis Hector Chavez | fe5fb8e | 2017-06-29 10:41:27 -0700 | [diff] [blame^] | 2014 | if (j->flags.use_caps && j->caps != 0 && |
| 2015 | !j->flags.set_ambient_caps) { |
| 2016 | die("non-empty, non-ambient capabilities are not " |
| 2017 | "supported without LD_PRELOAD"); |
| 2018 | } |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2019 | } |
Will Drewry | 2f54b6a | 2011-09-16 13:45:31 -0500 | [diff] [blame] | 2020 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2021 | /* |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 2022 | * Make the process group ID of this process equal to its PID. |
| 2023 | * In the non-interactive case (e.g. when the parent process is started |
| 2024 | * from init) this ensures the parent process and the jailed process |
| 2025 | * can be killed together. |
| 2026 | * When the parent process is started from the console this ensures |
| 2027 | * the call to setsid(2) in the jailed process succeeds. |
| 2028 | * |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 2029 | * Don't fail on EPERM, since setpgid(0, 0) can only EPERM when |
| 2030 | * the process is already a process group leader. |
| 2031 | */ |
| 2032 | if (setpgid(0 /* use calling PID */, 0 /* make PGID = PID */)) { |
| 2033 | if (errno != EPERM) { |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 2034 | pdie("setpgid(0, 0) failed"); |
Jorge Lucangeli Obes | 3c84df1 | 2015-05-14 17:37:58 -0700 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2038 | if (use_preload) { |
| 2039 | /* |
| 2040 | * Before we fork(2) and execve(2) the child process, we need |
| 2041 | * to open a pipe(2) to send the minijail configuration over. |
| 2042 | */ |
| 2043 | if (setup_pipe(pipe_fds)) |
| 2044 | return -EFAULT; |
| 2045 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2046 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2047 | /* |
| 2048 | * If we want to write to the child process' standard input, |
| 2049 | * create the pipe(2) now. |
| 2050 | */ |
| 2051 | if (pstdin_fd) { |
| 2052 | if (pipe(stdin_fds)) |
| 2053 | return -EFAULT; |
| 2054 | } |
| 2055 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2056 | /* |
| 2057 | * If we want to read from the child process' standard output, |
| 2058 | * create the pipe(2) now. |
| 2059 | */ |
| 2060 | if (pstdout_fd) { |
| 2061 | if (pipe(stdout_fds)) |
| 2062 | return -EFAULT; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * If we want to read from the child process' standard error, |
| 2067 | * create the pipe(2) now. |
| 2068 | */ |
| 2069 | if (pstderr_fd) { |
| 2070 | if (pipe(stderr_fds)) |
| 2071 | return -EFAULT; |
| 2072 | } |
| 2073 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2074 | /* |
Jorge Lucangeli Obes | ab6fa6f | 2016-08-04 15:42:48 -0400 | [diff] [blame] | 2075 | * If we want to set up a new uid/gid map in the user namespace, |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2076 | * or if we need to add the child process to cgroups, create the pipe(2) |
| 2077 | * to sync between parent and child. |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2078 | */ |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2079 | if (j->flags.userns || j->flags.cgroups) { |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2080 | sync_child = 1; |
| 2081 | if (pipe(child_sync_pipe_fds)) |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2082 | return -EFAULT; |
| 2083 | } |
| 2084 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 2085 | /* |
| 2086 | * 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] | 2087 | * |
| 2088 | * tl;dr: WARNING: do not mix pid namespaces and multithreading. |
| 2089 | * |
| 2090 | * In multithreaded programs, there are a bunch of locks inside libc, |
| 2091 | * some of which may be held by other threads at the time that we call |
| 2092 | * minijail_run_pid(). If we call fork(), glibc does its level best to |
| 2093 | * ensure that we hold all of these locks before it calls clone() |
| 2094 | * internally and drop them after clone() returns, but when we call |
| 2095 | * sys_clone(2) directly, all that gets bypassed and we end up with a |
| 2096 | * child address space where some of libc's important locks are held by |
| 2097 | * other threads (which did not get cloned, and hence will never release |
| 2098 | * those locks). This is okay so long as we call exec() immediately |
| 2099 | * after, but a bunch of seemingly-innocent libc functions like setenv() |
| 2100 | * take locks. |
| 2101 | * |
| 2102 | * Hence, only call sys_clone() if we need to, in order to get at pid |
| 2103 | * namespacing. If we follow this path, the child's address space might |
| 2104 | * have broken locks; you may only call functions that do not acquire |
| 2105 | * any locks. |
| 2106 | * |
| 2107 | * Unfortunately, fork() acquires every lock it can get its hands on, as |
| 2108 | * previously detailed, so this function is highly likely to deadlock |
| 2109 | * later on (see "deadlock here") if we're multithreaded. |
| 2110 | * |
| 2111 | * We might hack around this by having the clone()d child (init of the |
| 2112 | * pid namespace) return directly, rather than leaving the clone()d |
| 2113 | * process hanging around to be init for the new namespace (and having |
Jorge Lucangeli Obes | a521bee | 2016-03-03 13:47:57 -0800 | [diff] [blame] | 2114 | * its fork()ed child return in turn), but that process would be |
| 2115 | * crippled with its libc locks potentially broken. We might try |
| 2116 | * fork()ing in the parent before we clone() to ensure that we own all |
| 2117 | * the locks, but then we have to have the forked child hanging around |
| 2118 | * consuming resources (and possibly having file descriptors / shared |
| 2119 | * memory regions / etc attached). We'd need to keep the child around to |
| 2120 | * avoid having its children get reparented to init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2121 | * |
| 2122 | * TODO(ellyjones): figure out if the "forked child hanging around" |
| 2123 | * problem is fixable or not. It would be nice if we worked in this |
| 2124 | * case. |
| 2125 | */ |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2126 | if (pid_namespace) { |
| 2127 | int clone_flags = CLONE_NEWPID | SIGCHLD; |
| 2128 | if (j->flags.userns) |
| 2129 | clone_flags |= CLONE_NEWUSER; |
| 2130 | child_pid = syscall(SYS_clone, clone_flags, NULL); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2131 | } else { |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2132 | child_pid = fork(); |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2133 | } |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2134 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2135 | if (child_pid < 0) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2136 | if (use_preload) { |
| 2137 | free(oldenv_copy); |
| 2138 | } |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 2139 | die("failed to fork child"); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2140 | } |
Will Drewry | f89aef5 | 2011-09-16 16:48:57 -0500 | [diff] [blame] | 2141 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2142 | if (child_pid) { |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2143 | if (use_preload) { |
| 2144 | /* Restore parent's LD_PRELOAD. */ |
| 2145 | if (oldenv_copy) { |
| 2146 | setenv(kLdPreloadEnvVar, oldenv_copy, 1); |
| 2147 | free(oldenv_copy); |
| 2148 | } else { |
| 2149 | unsetenv(kLdPreloadEnvVar); |
| 2150 | } |
| 2151 | unsetenv(kFdEnvVar); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2152 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2153 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2154 | j->initpid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2155 | |
Jorge Lucangeli Obes | dba6209 | 2017-05-18 17:10:23 -0400 | [diff] [blame] | 2156 | if (j->flags.forward_signals) { |
| 2157 | forward_pid = child_pid; |
| 2158 | install_signal_handlers(); |
| 2159 | } |
| 2160 | |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2161 | if (j->flags.pid_file) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2162 | write_pid_file_or_die(j); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 2163 | |
Jorge Lucangeli Obes | b8a5138 | 2016-01-25 20:08:22 -0800 | [diff] [blame] | 2164 | if (j->flags.cgroups) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2165 | add_to_cgroups_or_die(j); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2166 | |
Dylan Reid | 0f72ef4 | 2017-06-06 15:42:49 -0700 | [diff] [blame] | 2167 | if (j->rlimit_count) |
| 2168 | set_rlimits_or_die(j); |
| 2169 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2170 | if (j->flags.userns) |
Jorge Lucangeli Obes | f205fff | 2016-08-06 09:06:21 -0400 | [diff] [blame] | 2171 | write_ugid_maps_or_die(j); |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2172 | |
| 2173 | if (sync_child) |
| 2174 | parent_setup_complete(child_sync_pipe_fds); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2175 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2176 | if (use_preload) { |
| 2177 | /* Send marshalled minijail. */ |
| 2178 | close(pipe_fds[0]); /* read endpoint */ |
| 2179 | ret = minijail_to_fd(j, pipe_fds[1]); |
| 2180 | close(pipe_fds[1]); /* write endpoint */ |
| 2181 | if (ret) { |
| 2182 | kill(j->initpid, SIGKILL); |
| 2183 | die("failed to send marshalled minijail"); |
| 2184 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2185 | } |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2186 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 2187 | if (pchild_pid) |
| 2188 | *pchild_pid = child_pid; |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2189 | |
| 2190 | /* |
| 2191 | * If we want to write to the child process' standard input, |
| 2192 | * set up the write end of the pipe. |
| 2193 | */ |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2194 | if (pstdin_fd) |
| 2195 | *pstdin_fd = setup_pipe_end(stdin_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2196 | 1 /* write end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2197 | |
| 2198 | /* |
| 2199 | * If we want to read from the child process' standard output, |
| 2200 | * set up the read end of the pipe. |
| 2201 | */ |
| 2202 | if (pstdout_fd) |
| 2203 | *pstdout_fd = setup_pipe_end(stdout_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2204 | 0 /* read end */); |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2205 | |
| 2206 | /* |
| 2207 | * If we want to read from the child process' standard error, |
| 2208 | * set up the read end of the pipe. |
| 2209 | */ |
| 2210 | if (pstderr_fd) |
| 2211 | *pstderr_fd = setup_pipe_end(stderr_fds, |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2212 | 0 /* read end */); |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2213 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2214 | return 0; |
| 2215 | } |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2216 | /* Child process. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2217 | free(oldenv_copy); |
Ben Chan | 541c7e5 | 2011-08-26 14:55:53 -0700 | [diff] [blame] | 2218 | |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 2219 | if (j->flags.reset_signal_mask) { |
| 2220 | sigset_t signal_mask; |
| 2221 | if (sigemptyset(&signal_mask) != 0) |
| 2222 | pdie("sigemptyset failed"); |
| 2223 | if (sigprocmask(SIG_SETMASK, &signal_mask, NULL) != 0) |
| 2224 | pdie("sigprocmask failed"); |
| 2225 | } |
| 2226 | |
Luis Hector Chavez | 43ff080 | 2016-10-07 12:21:07 -0700 | [diff] [blame] | 2227 | if (j->flags.close_open_fds) { |
| 2228 | const size_t kMaxInheritableFdsSize = 10; |
| 2229 | int inheritable_fds[kMaxInheritableFdsSize]; |
| 2230 | size_t size = 0; |
| 2231 | if (use_preload) { |
| 2232 | inheritable_fds[size++] = pipe_fds[0]; |
| 2233 | inheritable_fds[size++] = pipe_fds[1]; |
| 2234 | } |
| 2235 | if (sync_child) { |
| 2236 | inheritable_fds[size++] = child_sync_pipe_fds[0]; |
| 2237 | inheritable_fds[size++] = child_sync_pipe_fds[1]; |
| 2238 | } |
| 2239 | if (pstdin_fd) { |
| 2240 | inheritable_fds[size++] = stdin_fds[0]; |
| 2241 | inheritable_fds[size++] = stdin_fds[1]; |
| 2242 | } |
| 2243 | if (pstdout_fd) { |
| 2244 | inheritable_fds[size++] = stdout_fds[0]; |
| 2245 | inheritable_fds[size++] = stdout_fds[1]; |
| 2246 | } |
| 2247 | if (pstderr_fd) { |
| 2248 | inheritable_fds[size++] = stderr_fds[0]; |
| 2249 | inheritable_fds[size++] = stderr_fds[1]; |
| 2250 | } |
| 2251 | |
| 2252 | if (close_open_fds(inheritable_fds, size) < 0) |
| 2253 | die("failed to close open file descriptors"); |
| 2254 | } |
| 2255 | |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2256 | if (sync_child) |
| 2257 | wait_for_parent_setup(child_sync_pipe_fds); |
| 2258 | |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2259 | if (j->flags.userns) |
Dylan Reid | ce5b55e | 2016-01-13 11:04:16 -0800 | [diff] [blame] | 2260 | enter_user_namespace(j); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 2261 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2262 | /* |
| 2263 | * If we want to write to the jailed process' standard input, |
| 2264 | * set up the read end of the pipe. |
| 2265 | */ |
| 2266 | if (pstdin_fd) { |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2267 | if (setup_and_dupe_pipe_end(stdin_fds, 0 /* read end */, |
| 2268 | STDIN_FILENO) < 0) |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 2269 | die("failed to set up stdin pipe"); |
| 2270 | } |
| 2271 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 2272 | /* |
| 2273 | * If we want to read from the jailed process' standard output, |
| 2274 | * set up the write end of the pipe. |
| 2275 | */ |
| 2276 | if (pstdout_fd) { |
| 2277 | if (setup_and_dupe_pipe_end(stdout_fds, 1 /* write end */, |
| 2278 | STDOUT_FILENO) < 0) |
| 2279 | die("failed to set up stdout pipe"); |
| 2280 | } |
| 2281 | |
| 2282 | /* |
| 2283 | * If we want to read from the jailed process' standard error, |
| 2284 | * set up the write end of the pipe. |
| 2285 | */ |
| 2286 | if (pstderr_fd) { |
| 2287 | if (setup_and_dupe_pipe_end(stderr_fds, 1 /* write end */, |
| 2288 | STDERR_FILENO) < 0) |
| 2289 | die("failed to set up stderr pipe"); |
| 2290 | } |
| 2291 | |
Jorge Lucangeli Obes | aa235b9 | 2016-11-23 13:48:15 -0500 | [diff] [blame] | 2292 | /* |
| 2293 | * If any of stdin, stdout, or stderr are TTYs, create a new session. |
| 2294 | * This prevents the jailed process from using the TIOCSTI ioctl |
| 2295 | * to push characters into the parent process terminal's input buffer, |
| 2296 | * therefore escaping the jail. |
| 2297 | */ |
| 2298 | if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || |
| 2299 | isatty(STDERR_FILENO)) { |
| 2300 | if (setsid() < 0) { |
| 2301 | pdie("setsid() failed"); |
| 2302 | } |
| 2303 | } |
| 2304 | |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 2305 | /* If running an init program, let it decide when/how to mount /proc. */ |
| 2306 | if (pid_namespace && !do_init) |
| 2307 | j->flags.remount_proc_ro = 0; |
| 2308 | |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2309 | if (use_preload) { |
| 2310 | /* Strip out flags that cannot be inherited across execve(2). */ |
| 2311 | minijail_preexec(j); |
| 2312 | } else { |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2313 | /* |
| 2314 | * If not using LD_PRELOAD, do all jailing before execve(2). |
| 2315 | * Note that PID namespaces can only be entered on fork(2), |
| 2316 | * so that flag is still cleared. |
| 2317 | */ |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2318 | j->flags.pids = 0; |
| 2319 | } |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2320 | /* Jail this process, then execve(2) the target. */ |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2321 | minijail_enter(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2322 | |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 2323 | if (pid_namespace && do_init) { |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2324 | /* |
| 2325 | * pid namespace: this process will become init inside the new |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 2326 | * 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] | 2327 | * 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] | 2328 | * a child to actually run the program. If |do_init == 0|, we |
| 2329 | * let the program keep pid 1 and be init. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 2330 | * |
| 2331 | * If we're multithreaded, we'll probably deadlock here. See |
| 2332 | * WARNING above. |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2333 | */ |
| 2334 | child_pid = fork(); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2335 | if (child_pid < 0) { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2336 | _exit(child_pid); |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2337 | } else if (child_pid > 0) { |
Jorge Lucangeli Obes | 1365061 | 2016-09-02 11:27:29 -0400 | [diff] [blame] | 2338 | /* |
| 2339 | * Best effort. Don't bother checking the return value. |
| 2340 | */ |
Jorge Lucangeli Obes | 963eeec | 2016-08-10 16:02:43 -0400 | [diff] [blame] | 2341 | prctl(PR_SET_NAME, "minijail-init"); |
| 2342 | init(child_pid); /* Never returns. */ |
| 2343 | } |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2344 | } |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2345 | |
Elly Jones | dd3e851 | 2012-01-23 15:13:38 -0500 | [diff] [blame] | 2346 | /* |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 2347 | * 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] | 2348 | * calling process |
| 2349 | * -> execve()-ing process |
| 2350 | * If we are: |
| 2351 | * calling process |
| 2352 | * -> init()-ing process |
| 2353 | * -> execve()-ing process |
| 2354 | */ |
Jorge Lucangeli Obes | a205390 | 2016-08-02 12:08:15 -0400 | [diff] [blame] | 2355 | ret = execve(filename, argv, environ); |
| 2356 | if (ret == -1) { |
| 2357 | pwarn("execve(%s) failed", filename); |
| 2358 | } |
| 2359 | _exit(ret); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2360 | } |
| 2361 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2362 | int API minijail_kill(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2363 | { |
| 2364 | int st; |
| 2365 | if (kill(j->initpid, SIGTERM)) |
| 2366 | return -errno; |
| 2367 | if (waitpid(j->initpid, &st, 0) < 0) |
| 2368 | return -errno; |
| 2369 | return st; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2370 | } |
| 2371 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2372 | int API minijail_wait(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2373 | { |
| 2374 | int st; |
| 2375 | if (waitpid(j->initpid, &st, 0) < 0) |
| 2376 | return -errno; |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2377 | |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2378 | if (!WIFEXITED(st)) { |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 2379 | int error_status = st; |
| 2380 | if (WIFSIGNALED(st)) { |
| 2381 | int signum = WTERMSIG(st); |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 2382 | warn("child process %d received signal %d", |
Jorge Lucangeli Obes | 18d1eba | 2014-04-18 13:58:20 -0700 | [diff] [blame] | 2383 | j->initpid, signum); |
| 2384 | /* |
| 2385 | * We return MINIJAIL_ERR_JAIL if the process received |
| 2386 | * SIGSYS, which happens when a syscall is blocked by |
| 2387 | * seccomp filters. |
| 2388 | * If not, we do what bash(1) does: |
| 2389 | * $? = 128 + signum |
| 2390 | */ |
| 2391 | if (signum == SIGSYS) { |
| 2392 | error_status = MINIJAIL_ERR_JAIL; |
| 2393 | } else { |
| 2394 | error_status = 128 + signum; |
| 2395 | } |
| 2396 | } |
| 2397 | return error_status; |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 2398 | } |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2399 | |
| 2400 | int exit_status = WEXITSTATUS(st); |
| 2401 | if (exit_status != 0) |
mukesh agrawal | c420a26 | 2013-06-11 17:22:42 -0700 | [diff] [blame] | 2402 | info("child process %d exited with status %d", |
| 2403 | j->initpid, exit_status); |
Jorge Lucangeli Obes | 1530b74 | 2012-12-11 14:08:09 -0800 | [diff] [blame] | 2404 | |
| 2405 | return exit_status; |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2406 | } |
| 2407 | |
Will Drewry | 6ac9112 | 2011-10-21 16:38:58 -0500 | [diff] [blame] | 2408 | void API minijail_destroy(struct minijail *j) |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2409 | { |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2410 | size_t i; |
| 2411 | |
Jorge Lucangeli Obes | 524c040 | 2012-01-17 11:30:23 -0800 | [diff] [blame] | 2412 | if (j->flags.seccomp_filter && j->filter_prog) { |
| 2413 | free(j->filter_prog->filter); |
| 2414 | free(j->filter_prog); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2415 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 2416 | while (j->mounts_head) { |
| 2417 | struct mountpoint *m = j->mounts_head; |
| 2418 | j->mounts_head = j->mounts_head->next; |
Dylan Reid | 81e2397 | 2016-05-18 14:06:35 -0700 | [diff] [blame] | 2419 | free(m->data); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 2420 | free(m->type); |
| 2421 | free(m->dest); |
| 2422 | free(m->src); |
| 2423 | free(m); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 2424 | } |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 2425 | j->mounts_tail = NULL; |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2426 | if (j->user) |
| 2427 | free(j->user); |
Jorge Lucangeli Obes | e81a52f | 2015-12-04 16:05:23 -0800 | [diff] [blame] | 2428 | if (j->suppl_gid_list) |
| 2429 | free(j->suppl_gid_list); |
Will Drewry | bee7ba7 | 2011-10-21 20:47:01 -0500 | [diff] [blame] | 2430 | if (j->chrootdir) |
| 2431 | free(j->chrootdir); |
Jorge Lucangeli Obes | 3b2e6e4 | 2016-08-04 12:26:19 -0400 | [diff] [blame] | 2432 | if (j->pid_file_path) |
| 2433 | free(j->pid_file_path); |
| 2434 | if (j->uidmap) |
| 2435 | free(j->uidmap); |
| 2436 | if (j->gidmap) |
| 2437 | free(j->gidmap); |
Mike Frysinger | b9a7b16 | 2017-05-30 15:25:49 -0400 | [diff] [blame] | 2438 | if (j->hostname) |
| 2439 | free(j->hostname); |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 2440 | if (j->alt_syscall_table) |
| 2441 | free(j->alt_syscall_table); |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 2442 | for (i = 0; i < j->cgroup_count; ++i) |
| 2443 | free(j->cgroups[i]); |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 2444 | free(j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2445 | } |