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