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