Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 4 | |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 7 | #include <signal.h> |
Luis Hector Chavez | ff5978f | 2017-06-27 12:52:58 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <sys/mount.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/types.h> |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 14 | #include <sys/wait.h> |
Luis Hector Chavez | 836d7b2 | 2017-09-14 15:11:15 -0700 | [diff] [blame] | 15 | #include <syscall.h> |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 18 | #include <algorithm> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 19 | #include <map> |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 20 | #include <memory> |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 21 | #include <ostream> |
Stephen Barber | 771653f | 2017-10-04 23:48:57 -0700 | [diff] [blame] | 22 | #include <set> |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 23 | #include <sstream> |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 24 | #include <string> |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 25 | #include <tuple> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 26 | #include <utility> |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | #include <base/bind.h> |
| 30 | #include <base/bind_helpers.h> |
| 31 | #include <base/callback_helpers.h> |
| 32 | #include <base/files/file_path.h> |
| 33 | #include <base/files/file_util.h> |
| 34 | #include <base/files/scoped_file.h> |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 35 | #include <base/logging.h> |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 36 | #include <base/macros.h> |
| 37 | #include <base/strings/string_util.h> |
| 38 | #include <base/strings/stringprintf.h> |
Luis Hector Chavez | 836d7b2 | 2017-09-14 15:11:15 -0700 | [diff] [blame] | 39 | #include <libminijail.h> |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 40 | #include <scoped_minijail.h> |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 41 | |
Luis Hector Chavez | 76ae9ac | 2017-09-20 21:13:08 -0700 | [diff] [blame] | 42 | #include "libcontainer/cgroup.h" |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 43 | #include "libcontainer/config.h" |
Luis Hector Chavez | 836d7b2 | 2017-09-14 15:11:15 -0700 | [diff] [blame] | 44 | #include "libcontainer/libcontainer.h" |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 45 | #include "libcontainer/libcontainer_util.h" |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 46 | |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 47 | #define QUOTE(s) ('"' + std::string(s) + '"') |
| 48 | |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 49 | namespace { |
| 50 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 51 | using libcontainer::DeviceMapperDetach; |
| 52 | using libcontainer::DeviceMapperSetup; |
| 53 | using libcontainer::GetUsernsOutsideId; |
| 54 | using libcontainer::LoopdevDetach; |
| 55 | using libcontainer::LoopdevSetup; |
| 56 | using libcontainer::MakeDir; |
| 57 | using libcontainer::MountExternal; |
| 58 | using libcontainer::TouchFile; |
Mike Frysinger | 412dbd2 | 2017-01-06 01:50:34 -0500 | [diff] [blame] | 59 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 60 | constexpr size_t kMaxNumSetfilesArgs = 128; |
| 61 | constexpr size_t kMaxRlimits = 32; // Linux defines 15 at the time of writing. |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 62 | |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 63 | struct Mount { |
| 64 | std::string name; |
| 65 | base::FilePath source; |
| 66 | base::FilePath destination; |
| 67 | std::string type; |
| 68 | std::string data; |
| 69 | std::string verity; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 70 | int flags; |
| 71 | int uid; |
| 72 | int gid; |
| 73 | int mode; |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 74 | |
| 75 | // True if mount should happen in new vfs ns. |
| 76 | bool mount_in_ns; |
| 77 | |
| 78 | // True if target should be created if it doesn't exist. |
| 79 | bool create; |
| 80 | |
| 81 | // True if target should be mounted via loopback. |
| 82 | bool loopback; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 85 | struct Device { |
| 86 | // 'c' or 'b' for char or block |
| 87 | char type; |
| 88 | base::FilePath path; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 89 | int fs_permissions; |
| 90 | int major; |
| 91 | int minor; |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 92 | |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 93 | // Copy the major from existing node, ignores |major|. |
| 94 | bool copy_major; |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 95 | // Copy the minor from existing node, ignores |minor|. |
| 96 | bool copy_minor; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 97 | int uid; |
| 98 | int gid; |
Dylan Reid | 4843d6b | 2017-03-31 18:14:30 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 101 | struct CgroupDevice { |
| 102 | bool allow; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 103 | char type; |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 104 | |
| 105 | // -1 for either major or minor means all. |
| 106 | int major; |
| 107 | int minor; |
| 108 | |
| 109 | bool read; |
| 110 | bool write; |
| 111 | bool modify; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 114 | struct CpuCgroup { |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 115 | int shares; |
| 116 | int quota; |
| 117 | int period; |
| 118 | int rt_runtime; |
| 119 | int rt_period; |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 120 | }; |
| 121 | |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 122 | struct Rlimit { |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 123 | int type; |
| 124 | uint32_t cur; |
| 125 | uint32_t max; |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 128 | } // namespace |
| 129 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 130 | // Structure that configures how the container is run. |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 131 | struct container_config { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 132 | // Path to the root of the container itself. |
| 133 | base::FilePath config_root; |
| 134 | |
| 135 | // Path to the root of the container's filesystem. |
| 136 | base::FilePath rootfs; |
| 137 | |
| 138 | // Flags that will be passed to mount() for the rootfs. |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 139 | unsigned long rootfs_mount_flags = 0x0; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 140 | |
| 141 | // Path to where the container will be run. |
| 142 | base::FilePath premounted_runfs; |
| 143 | |
| 144 | // Path to the file where the pid should be written. |
| 145 | base::FilePath pid_file_path; |
| 146 | |
| 147 | // The program to run and args, e.g. "/sbin/init". |
| 148 | std::vector<std::string> program_argv; |
| 149 | |
| 150 | // The uid the container will run as. |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 151 | uid_t uid = 0; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 152 | |
| 153 | // Mapping of UIDs in the container, e.g. "0 100000 1024" |
| 154 | std::string uid_map; |
| 155 | |
| 156 | // The gid the container will run as. |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 157 | gid_t gid = 0; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 158 | |
| 159 | // Mapping of GIDs in the container, e.g. "0 100000 1024" |
| 160 | std::string gid_map; |
| 161 | |
| 162 | // Syscall table to use or nullptr if none. |
| 163 | std::string alt_syscall_table; |
| 164 | |
| 165 | // Filesystems to mount in the new namespace. |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 166 | std::vector<Mount> mounts; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 167 | |
Stephen Barber | 771653f | 2017-10-04 23:48:57 -0700 | [diff] [blame] | 168 | // Namespaces that should be used for the container. |
| 169 | std::set<std::string> namespaces; |
| 170 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 171 | // Device nodes to create. |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 172 | std::vector<Device> devices; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 173 | |
| 174 | // Device node cgroup permissions. |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 175 | std::vector<CgroupDevice> cgroup_devices; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 176 | |
| 177 | // Should run setfiles on mounts to enable selinux. |
| 178 | std::string run_setfiles; |
| 179 | |
| 180 | // CPU cgroup params. |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 181 | CpuCgroup cpu_cgparams; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 182 | |
| 183 | // Parent dir for cgroup creation |
| 184 | base::FilePath cgroup_parent; |
| 185 | |
| 186 | // uid to own the created cgroups |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 187 | uid_t cgroup_owner = 0; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 188 | |
| 189 | // gid to own the created cgroups |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 190 | gid_t cgroup_group = 0; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 191 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 192 | // Allow the child process to keep open FDs (for stdin/out/err). |
yusukes | f125f33 | 2017-12-08 13:45:15 -0800 | [diff] [blame] | 193 | bool keep_fds_open = false; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 194 | |
| 195 | // Array of rlimits for the contained process. |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 196 | Rlimit rlimits[kMaxRlimits]; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 197 | |
| 198 | // The number of elements in `rlimits`. |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 199 | int num_rlimits = 0; |
yusukes | f125f33 | 2017-12-08 13:45:15 -0800 | [diff] [blame] | 200 | bool use_capmask = false; |
| 201 | bool use_capmask_ambient = false; |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 202 | uint64_t capmask = 0x0; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 203 | |
| 204 | // The mask of securebits to skip when restricting caps. |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 205 | uint64_t securebits_skip_mask = 0x0; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 206 | |
| 207 | // Whether the container needs an extra process to be run as init. |
yusukes | f125f33 | 2017-12-08 13:45:15 -0800 | [diff] [blame] | 208 | bool do_init = false; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 209 | |
| 210 | // The SELinux context name the container will run under. |
| 211 | std::string selinux_context; |
| 212 | |
| 213 | // A function pointer to be called prior to calling execve(2). |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 214 | minijail_hook_t pre_start_hook = nullptr; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 215 | |
| 216 | // Parameter that will be passed to pre_start_hook(). |
yusukes | b7b9a04 | 2017-12-08 13:14:25 -0800 | [diff] [blame] | 217 | void* pre_start_hook_payload = nullptr; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 218 | |
Luis Hector Chavez | e03926a | 2017-09-28 17:28:49 -0700 | [diff] [blame] | 219 | // A list of file descriptors to inherit. |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 220 | std::vector<int> inherited_fds; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 221 | |
| 222 | // A list of hooks that will be called upon minijail reaching various states |
| 223 | // of execution. |
| 224 | std::map<minijail_hook_event_t, std::vector<libcontainer::HookCallback>> |
| 225 | hooks; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 228 | // Container manipulation |
| 229 | struct container { |
Luis Hector Chavez | 76ae9ac | 2017-09-20 21:13:08 -0700 | [diff] [blame] | 230 | std::unique_ptr<libcontainer::Cgroup> cgroup; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 231 | ScopedMinijail jail; |
Luis Hector Chavez | 15d0d1a | 2017-10-12 09:30:19 -0700 | [diff] [blame] | 232 | pid_t init_pid = -1; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 233 | base::FilePath config_root; |
| 234 | base::FilePath runfs; |
| 235 | base::FilePath rundir; |
| 236 | base::FilePath runfsroot; |
| 237 | base::FilePath pid_file_path; |
| 238 | |
| 239 | // Mounts made outside of the minijail. |
| 240 | std::vector<base::FilePath> ext_mounts; |
| 241 | std::vector<base::FilePath> loopdev_paths; |
| 242 | std::vector<std::string> device_mappers; |
| 243 | std::string name; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 244 | |
| 245 | std::vector<std::pair<libcontainer::HookState, |
| 246 | std::vector<libcontainer::HookCallback>>> |
| 247 | hook_states; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | namespace { |
| 251 | |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 252 | std::ostream& operator<<(std::ostream& stream, const Mount& mount) { |
| 253 | stream << "mount:" << std::endl |
| 254 | << " name: " << QUOTE(mount.name) << std::endl |
| 255 | << " source: " << QUOTE(mount.source.value()) << std::endl |
| 256 | << " destination: " << QUOTE(mount.destination.value()) << std::endl |
| 257 | << " type: " << QUOTE(mount.type) << std::endl |
| 258 | << " data: " << QUOTE(mount.data) << std::endl |
| 259 | << " verity: " << QUOTE(mount.verity) << std::endl |
| 260 | << " flags: 0x" << std::hex << mount.flags << std::dec << std::endl |
| 261 | << " uid: " << mount.uid << std::endl |
| 262 | << " gid: " << mount.gid << std::endl |
| 263 | << " mode: 0" << std::oct << mount.mode << std::dec << std::endl |
| 264 | << " mount_in_ns: " << mount.mount_in_ns << std::endl |
| 265 | << " create: " << mount.create << std::endl |
| 266 | << " loopback: " << mount.loopback << std::endl; |
| 267 | |
| 268 | return stream; |
| 269 | } |
| 270 | |
| 271 | std::ostream& operator<<(std::ostream& stream, const Device& device) { |
| 272 | stream << "device:" << std::endl |
| 273 | << " type: " << device.type << std::endl |
| 274 | << " path: " << QUOTE(device.path.value()) << std::endl |
| 275 | << " fs_permissions: 0" << std::oct << device.fs_permissions |
| 276 | << std::dec << std::endl |
| 277 | << " major: " << device.major << std::endl |
| 278 | << " minor: " << device.minor << std::endl |
| 279 | << " copy_minor: " << device.copy_minor << std::endl |
| 280 | << " uid: " << device.uid << std::endl |
| 281 | << " gid: " << device.gid << std::endl; |
| 282 | |
| 283 | return stream; |
| 284 | } |
| 285 | |
| 286 | std::ostream& operator<<(std::ostream& stream, |
| 287 | const CgroupDevice& cgroup_device) { |
| 288 | stream << "cgroup_device:" << std::endl |
| 289 | << " allow: " << cgroup_device.allow << std::endl |
| 290 | << " type: " << cgroup_device.type << std::endl |
| 291 | << " major: " << cgroup_device.major << std::endl |
| 292 | << " minor: " << cgroup_device.minor << std::endl |
| 293 | << " read: " << cgroup_device.read << std::endl |
| 294 | << " write: " << cgroup_device.write << std::endl |
| 295 | << " modify: " << cgroup_device.modify << std::endl; |
| 296 | |
| 297 | return stream; |
| 298 | } |
| 299 | |
| 300 | std::ostream& operator<<(std::ostream& stream, const CpuCgroup& cpu_cgroup) { |
| 301 | stream << "cpu_cgroup:" << std::endl |
| 302 | << " shares: " << cpu_cgroup.shares << std::endl |
| 303 | << " quota: " << cpu_cgroup.quota << std::endl |
| 304 | << " period: " << cpu_cgroup.period << std::endl |
| 305 | << " rt_runtime: " << cpu_cgroup.rt_runtime << std::endl |
| 306 | << " rt_period: " << cpu_cgroup.rt_period << std::endl; |
| 307 | |
| 308 | return stream; |
| 309 | } |
| 310 | |
| 311 | std::ostream& operator<<(std::ostream& stream, const Rlimit& rlimit) { |
| 312 | stream << "rlimit:" << std::endl |
| 313 | << " type: " << rlimit.type << std::endl |
| 314 | << " cur: " << rlimit.cur << std::endl |
| 315 | << " max: " << rlimit.max << std::endl; |
| 316 | |
| 317 | return stream; |
| 318 | } |
| 319 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 320 | void DumpConfig(std::ostream* stream, |
| 321 | const container_config* c, |
| 322 | bool sort_vectors) { |
| 323 | *stream << "config_root: " << QUOTE(c->config_root.value()) << std::endl |
| 324 | << "rootfs: " << QUOTE(c->rootfs.value()) << std::endl |
| 325 | << "rootfs_mount_flags: 0x" << std::hex << c->rootfs_mount_flags |
| 326 | << std::dec << std::endl |
| 327 | << "premounted_runfs: " << QUOTE(c->premounted_runfs.value()) |
| 328 | << std::endl |
| 329 | << "pid_file_path: " << QUOTE(c->pid_file_path.value()) << std::endl |
| 330 | << "program_argv: size=" << c->program_argv.size() << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 331 | |
| 332 | for (const std::string& argv : c->program_argv) |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 333 | *stream << " " << QUOTE(argv) << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 334 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 335 | *stream << "uid: " << c->uid << std::endl |
| 336 | << "uid_map: " << QUOTE(c->uid_map) << std::endl |
| 337 | << "gid: " << c->gid << std::endl |
| 338 | << "gid_map: " << QUOTE(c->gid_map) << std::endl |
| 339 | << "alt_syscall_table: " << QUOTE(c->alt_syscall_table) << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 340 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 341 | auto mount_sorted = c->mounts; |
| 342 | if (sort_vectors) { |
| 343 | std::stable_sort(mount_sorted.begin(), mount_sorted.end(), |
| 344 | [](const Mount& lhs, const Mount& rhs) { |
| 345 | return std::make_tuple(lhs.destination.value(), |
| 346 | lhs.source.value(), lhs.flags) < |
| 347 | std::make_tuple(rhs.destination.value(), |
| 348 | rhs.source.value(), rhs.flags); |
| 349 | }); |
| 350 | } |
| 351 | for (const auto& mount : mount_sorted) |
| 352 | *stream << mount; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 353 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 354 | *stream << "namespaces: size=" << c->namespaces.size() << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 355 | for (const std::string& ns : c->namespaces) |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 356 | *stream << " " << QUOTE(ns) << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 357 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 358 | auto devices_sorted = c->devices; |
| 359 | if (sort_vectors) { |
| 360 | std::stable_sort(devices_sorted.begin(), devices_sorted.end(), |
| 361 | [](const Device& lhs, const Device& rhs) { |
| 362 | return lhs.path.value() < rhs.path.value(); |
| 363 | }); |
| 364 | } |
| 365 | for (const auto& device : devices_sorted) |
| 366 | *stream << device; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 367 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 368 | auto cgroup_devices_sorted = c->cgroup_devices; |
| 369 | if (sort_vectors) { |
| 370 | std::stable_sort(cgroup_devices_sorted.begin(), cgroup_devices_sorted.end(), |
| 371 | [](const CgroupDevice& lhs, const CgroupDevice& rhs) { |
| 372 | return std::make_tuple(lhs.type, lhs.major, lhs.minor) < |
| 373 | std::make_tuple(rhs.type, rhs.major, rhs.minor); |
| 374 | }); |
| 375 | } |
| 376 | for (const auto& cgroup_device : cgroup_devices_sorted) |
| 377 | *stream << cgroup_device; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 378 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 379 | *stream << "run_setfiles: " << QUOTE(c->run_setfiles) << std::endl |
| 380 | << c->cpu_cgparams |
| 381 | << "cgroup_parent: " << QUOTE(c->cgroup_parent.value()) << std::endl |
| 382 | << "cgroup_owner: " << c->cgroup_owner << std::endl |
| 383 | << "cgroup_group: " << c->cgroup_group << std::endl |
| 384 | << "keep_fds_open: " << c->keep_fds_open << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 385 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 386 | *stream << "num_rlimits: " << c->num_rlimits << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 387 | for (size_t i = 0; i < c->num_rlimits; ++i) |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 388 | *stream << c->rlimits[i]; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 389 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 390 | *stream << "use_capmask: " << c->use_capmask << std::endl |
| 391 | << "use_capmask_ambient: " << c->use_capmask_ambient << std::endl |
| 392 | << "capmask: 0x" << std::hex << c->capmask << std::dec << std::endl |
| 393 | << "securebits_skip_mask: 0x" << std::hex << c->securebits_skip_mask |
| 394 | << std::dec << std::endl |
| 395 | << "do_init: " << c->do_init << std::endl |
| 396 | << "selinux_context: " << QUOTE(c->selinux_context) << std::endl |
| 397 | << "pre_start_hook: " << reinterpret_cast<void*>(c->pre_start_hook) |
| 398 | << std::endl |
| 399 | << "pre_start_hook_payload: " << c->pre_start_hook_payload |
| 400 | << std::endl |
| 401 | << "inherited_fds: size=" << c->inherited_fds.size() << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 402 | |
| 403 | for (int fd : c->inherited_fds) |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 404 | *stream << " " << fd << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 405 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 406 | *stream << "hooks: size=" << c->hooks.size() << std::endl; |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 409 | // Returns the path for |path_in_container| in the outer namespace. |
| 410 | base::FilePath GetPathInOuterNamespace( |
| 411 | const base::FilePath& root, const base::FilePath& path_in_container) { |
| 412 | if (path_in_container.IsAbsolute()) |
| 413 | return base::FilePath(root.value() + path_in_container.value()); |
| 414 | return root.Append(path_in_container); |
| 415 | } |
| 416 | |
| 417 | // Make sure the mount target exists in the new rootfs. Create if needed and |
| 418 | // possible. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 419 | bool SetupMountDestination(const struct container_config* config, |
| 420 | const Mount& mount, |
| 421 | const base::FilePath& source, |
| 422 | const base::FilePath& dest) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 423 | struct stat st_buf; |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 424 | if (stat(dest.value().c_str(), &st_buf) == 0) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 425 | // destination exists. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 426 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // Try to create the destination. Either make directory or touch a file |
| 430 | // depending on the source type. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 431 | int uid_userns; |
| 432 | if (!GetUsernsOutsideId(config->uid_map, mount.uid, &uid_userns)) |
| 433 | return false; |
| 434 | int gid_userns; |
| 435 | if (!GetUsernsOutsideId(config->gid_map, mount.gid, &gid_userns)) |
| 436 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 437 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 438 | if (stat(source.value().c_str(), &st_buf) != 0 || S_ISDIR(st_buf.st_mode) || |
| 439 | S_ISBLK(st_buf.st_mode)) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 440 | return MakeDir(dest, uid_userns, gid_userns, mount.mode); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 441 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 442 | |
| 443 | return TouchFile(dest, uid_userns, gid_userns, mount.mode); |
| 444 | } |
| 445 | |
| 446 | // Fork and exec the setfiles command to configure the selinux policy. |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 447 | bool RunSetfilesCommand(const struct container* c, |
| 448 | const struct container_config* config, |
| 449 | const std::vector<base::FilePath>& destinations, |
| 450 | pid_t container_pid) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 451 | int pid = fork(); |
| 452 | if (pid == 0) { |
| 453 | size_t arg_index = 0; |
| 454 | const char* argv[kMaxNumSetfilesArgs]; |
| 455 | const char* env[] = { |
| 456 | nullptr, |
| 457 | }; |
| 458 | |
| 459 | base::FilePath context_path = c->runfsroot.Append("file_contexts"); |
| 460 | |
| 461 | argv[arg_index++] = config->run_setfiles.c_str(); |
| 462 | argv[arg_index++] = "-r"; |
| 463 | argv[arg_index++] = c->runfsroot.value().c_str(); |
| 464 | argv[arg_index++] = context_path.value().c_str(); |
| 465 | if (arg_index + destinations.size() >= kMaxNumSetfilesArgs) |
| 466 | _exit(-E2BIG); |
| 467 | for (const auto& destination : destinations) |
| 468 | argv[arg_index++] = destination.value().c_str(); |
| 469 | argv[arg_index] = nullptr; |
| 470 | |
| 471 | execve( |
| 472 | argv[0], const_cast<char* const*>(argv), const_cast<char* const*>(env)); |
| 473 | |
| 474 | /* Command failed to exec if execve returns. */ |
| 475 | _exit(-errno); |
| 476 | } |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 477 | if (pid < 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 478 | PLOG(ERROR) << "Failed to fork to run setfiles"; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 479 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 480 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 481 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 482 | int status; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 483 | if (HANDLE_EINTR(waitpid(pid, &status, 0)) < 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 484 | PLOG(ERROR) << "Failed to wait for setfiles"; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 485 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 486 | } |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 487 | if (!WIFEXITED(status)) { |
| 488 | LOG(ERROR) << "setfiles did not terminate cleanly"; |
| 489 | return false; |
| 490 | } |
| 491 | if (WEXITSTATUS(status) != 0) { |
| 492 | LOG(ERROR) << "setfiles exited with non-zero status: " |
| 493 | << WEXITSTATUS(status); |
| 494 | return false; |
| 495 | } |
| 496 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Unmounts anything we mounted in this mount namespace in the opposite order |
| 500 | // that they were mounted. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 501 | bool UnmountExternalMounts(struct container* c) { |
| 502 | bool ret = true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 503 | |
| 504 | for (auto it = c->ext_mounts.rbegin(); it != c->ext_mounts.rend(); ++it) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 505 | if (umount(it->value().c_str()) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 506 | PLOG(ERROR) << "Failed to unmount " << it->value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 507 | ret = false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 508 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 509 | } |
| 510 | c->ext_mounts.clear(); |
| 511 | |
| 512 | for (auto it = c->loopdev_paths.rbegin(); it != c->loopdev_paths.rend(); |
| 513 | ++it) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 514 | if (!LoopdevDetach(*it)) |
| 515 | ret = false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 516 | } |
| 517 | c->loopdev_paths.clear(); |
| 518 | |
| 519 | for (auto it = c->device_mappers.rbegin(); it != c->device_mappers.rend(); |
| 520 | ++it) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 521 | if (!DeviceMapperDetach(*it)) |
| 522 | ret = false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 523 | } |
| 524 | c->device_mappers.clear(); |
| 525 | |
| 526 | return ret; |
| 527 | } |
| 528 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 529 | bool DoContainerMount(struct container* c, |
| 530 | const struct container_config* config, |
| 531 | const Mount& mount) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 532 | base::FilePath dest = |
| 533 | GetPathInOuterNamespace(c->runfsroot, mount.destination); |
| 534 | |
| 535 | // If it's a bind mount relative to rootfs, append source to |
| 536 | // rootfs path, otherwise source path is absolute. |
| 537 | base::FilePath source; |
| 538 | if ((mount.flags & MS_BIND) && !mount.source.IsAbsolute()) { |
| 539 | source = GetPathInOuterNamespace(c->runfsroot, mount.source); |
| 540 | } else if (mount.loopback && !mount.source.IsAbsolute() && |
| 541 | !c->config_root.empty()) { |
| 542 | source = GetPathInOuterNamespace(c->config_root, mount.source); |
| 543 | } else { |
| 544 | source = mount.source; |
| 545 | } |
| 546 | |
| 547 | // Only create the destinations for external mounts, minijail will take |
| 548 | // care of those mounted in the new namespace. |
| 549 | if (mount.create && !mount.mount_in_ns) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 550 | if (!SetupMountDestination(config, mount, source, dest)) |
| 551 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 552 | } |
| 553 | if (mount.loopback) { |
| 554 | // Record this loopback file for cleanup later. |
| 555 | base::FilePath loop_source = source; |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 556 | if (!LoopdevSetup(loop_source, &source)) |
| 557 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 558 | |
| 559 | // Save this to cleanup when shutting down. |
| 560 | c->loopdev_paths.push_back(source); |
| 561 | } |
| 562 | if (!mount.verity.empty()) { |
| 563 | // Set this device up via dm-verity. |
| 564 | std::string dm_name; |
| 565 | base::FilePath dm_source = source; |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 566 | if (!DeviceMapperSetup(dm_source, mount.verity, &source, &dm_name)) |
| 567 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 568 | |
| 569 | // Save this to cleanup when shutting down. |
| 570 | c->device_mappers.push_back(dm_name); |
| 571 | } |
| 572 | if (mount.mount_in_ns) { |
| 573 | // We can mount this with minijail. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 574 | if (minijail_mount_with_data( |
| 575 | c->jail.get(), source.value().c_str(), |
| 576 | mount.destination.value().c_str(), mount.type.c_str(), mount.flags, |
| 577 | mount.data.empty() ? nullptr : mount.data.c_str()) != 0) { |
| 578 | return false; |
| 579 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 580 | } else { |
| 581 | // Mount this externally and unmount it on exit. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 582 | if (!MountExternal(source.value(), dest.value(), mount.type, mount.flags, |
| 583 | mount.data)) { |
| 584 | return false; |
| 585 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 586 | // Save this to unmount when shutting down. |
| 587 | c->ext_mounts.push_back(dest); |
| 588 | } |
| 589 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 590 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 593 | bool DoContainerMounts(struct container* c, |
| 594 | const struct container_config* config) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 595 | UnmountExternalMounts(c); |
| 596 | |
| 597 | // This will run in all the error cases. |
| 598 | base::ScopedClosureRunner teardown(base::Bind( |
| 599 | base::IgnoreResult(&UnmountExternalMounts), base::Unretained(c))); |
| 600 | |
| 601 | for (const auto& mount : config->mounts) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 602 | if (!DoContainerMount(c, config, mount)) |
| 603 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // The mounts have been done successfully, no need to tear them down anymore. |
| 607 | ignore_result(teardown.Release()); |
| 608 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 609 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 612 | bool ContainerCreateDevice(const struct container* c, |
| 613 | const struct container_config* config, |
| 614 | const Device& dev, |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 615 | int major, |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 616 | int minor) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 617 | mode_t mode = dev.fs_permissions; |
| 618 | switch (dev.type) { |
| 619 | case 'b': |
| 620 | mode |= S_IFBLK; |
| 621 | break; |
| 622 | case 'c': |
| 623 | mode |= S_IFCHR; |
| 624 | break; |
| 625 | default: |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 626 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 629 | int uid_userns; |
| 630 | if (!GetUsernsOutsideId(config->uid_map, dev.uid, &uid_userns)) |
| 631 | return false; |
| 632 | int gid_userns; |
| 633 | if (!GetUsernsOutsideId(config->gid_map, dev.gid, &gid_userns)) |
| 634 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 635 | |
| 636 | base::FilePath path = GetPathInOuterNamespace(c->runfsroot, dev.path); |
Luis Hector Chavez | 92278e8 | 2017-10-16 11:30:27 -0700 | [diff] [blame] | 637 | if (!libcontainer::CreateDirectoryOwnedBy(path.DirName(), 0755, uid_userns, |
| 638 | gid_userns)) { |
Luis Hector Chavez | 5d51abb | 2017-10-11 17:05:57 -0700 | [diff] [blame] | 639 | PLOG(ERROR) << "Failed to create parent directory for " << path.value(); |
| 640 | return false; |
| 641 | } |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 642 | if (mknod(path.value().c_str(), mode, makedev(major, minor)) != 0 && |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 643 | errno != EEXIST) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 644 | PLOG(ERROR) << "Failed to mknod " << path.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 645 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 646 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 647 | if (chown(path.value().c_str(), uid_userns, gid_userns) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 648 | PLOG(ERROR) << "Failed to chown " << path.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 649 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 650 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 651 | if (chmod(path.value().c_str(), dev.fs_permissions) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 652 | PLOG(ERROR) << "Failed to chmod " << path.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 653 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 654 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 655 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 656 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 659 | bool MountRunfs(struct container* c, const struct container_config* config) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 660 | { |
| 661 | std::string runfs_template = base::StringPrintf( |
| 662 | "%s/%s_XXXXXX", c->rundir.value().c_str(), c->name.c_str()); |
| 663 | // TODO(lhchavez): Replace this with base::CreateTemporaryDirInDir(). |
| 664 | char* runfs_path = mkdtemp(const_cast<char*>(runfs_template.c_str())); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 665 | if (!runfs_path) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 666 | PLOG(ERROR) << "Failed to mkdtemp in " << c->rundir.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 667 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 668 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 669 | c->runfs = base::FilePath(runfs_path); |
| 670 | } |
| 671 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 672 | int uid_userns; |
| 673 | if (!GetUsernsOutsideId(config->uid_map, config->uid, &uid_userns)) |
| 674 | return false; |
| 675 | int gid_userns; |
| 676 | if (!GetUsernsOutsideId(config->gid_map, config->gid, &gid_userns)) |
| 677 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 678 | |
| 679 | // Make sure the container uid can access the rootfs. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 680 | if (chmod(c->runfs.value().c_str(), 0700) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 681 | PLOG(ERROR) << "Failed to chmod " << c->runfs.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 682 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 683 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 684 | if (chown(c->runfs.value().c_str(), uid_userns, gid_userns) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 685 | PLOG(ERROR) << "Failed to chown " << c->runfs.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 686 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 687 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 688 | |
| 689 | c->runfsroot = c->runfs.Append("root"); |
| 690 | |
| 691 | constexpr mode_t kRootDirMode = 0660; |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 692 | if (mkdir(c->runfsroot.value().c_str(), kRootDirMode) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 693 | PLOG(ERROR) << "Failed to mkdir " << c->runfsroot.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 694 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 695 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 696 | if (chmod(c->runfsroot.value().c_str(), kRootDirMode) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 697 | PLOG(ERROR) << "Failed to chmod " << c->runfsroot.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 698 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 699 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 700 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 701 | if (mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "", |
| 702 | MS_BIND | (config->rootfs_mount_flags & MS_REC), nullptr) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 703 | PLOG(ERROR) << "Failed to bind-mount " << config->rootfs.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 704 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | // MS_BIND ignores any flags passed to it (except MS_REC). We need a |
| 708 | // second call to mount() to actually set them. |
| 709 | if (config->rootfs_mount_flags && |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 710 | mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "", |
| 711 | (config->rootfs_mount_flags & ~MS_REC), nullptr) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 712 | PLOG(ERROR) << "Failed to remount " << c->runfsroot.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 713 | return false; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 716 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 719 | bool CreateDeviceNodes(struct container* c, |
| 720 | const struct container_config* config, |
| 721 | pid_t container_pid) { |
| 722 | for (const auto& dev : config->devices) { |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 723 | int major = dev.major; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 724 | int minor = dev.minor; |
| 725 | |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 726 | if (dev.copy_major || dev.copy_minor) { |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 727 | struct stat st_buff; |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 728 | if (stat(dev.path.value().c_str(), &st_buff) != 0) |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 729 | continue; |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 730 | |
| 731 | if (dev.copy_major) |
| 732 | major = major(st_buff.st_rdev); |
| 733 | if (dev.copy_minor) |
| 734 | minor = minor(st_buff.st_rdev); |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 735 | } |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 736 | if (major < 0 || minor < 0) |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 737 | continue; |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 738 | if (!ContainerCreateDevice(c, config, dev, major, minor)) |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 739 | return false; |
| 740 | } |
| 741 | |
| 742 | return true; |
| 743 | } |
| 744 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 745 | bool DeviceSetup(struct container* c, const struct container_config* config) { |
Luis Hector Chavez | 76ae9ac | 2017-09-20 21:13:08 -0700 | [diff] [blame] | 746 | c->cgroup->DenyAllDevices(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 747 | |
| 748 | for (const auto& dev : config->cgroup_devices) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 749 | if (!c->cgroup->AddDevice(dev.allow, dev.major, dev.minor, dev.read, |
| 750 | dev.write, dev.modify, dev.type)) { |
| 751 | return false; |
| 752 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 755 | for (const auto& loopdev_path : c->loopdev_paths) { |
| 756 | struct stat st; |
| 757 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 758 | if (stat(loopdev_path.value().c_str(), &st) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 759 | PLOG(ERROR) << "Failed to stat " << loopdev_path.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 760 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 761 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 762 | if (!c->cgroup->AddDevice(1, major(st.st_rdev), minor(st.st_rdev), 1, 0, 0, |
| 763 | 'b')) { |
| 764 | return false; |
| 765 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 768 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | int Setexeccon(void* payload) { |
| 772 | char* init_domain = reinterpret_cast<char*>(payload); |
| 773 | pid_t tid = syscall(SYS_gettid); |
| 774 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 775 | if (tid < 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 776 | PLOG(ERROR) << "Failed to gettid"; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 777 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 778 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 779 | |
| 780 | std::string exec_path = |
| 781 | base::StringPrintf("/proc/self/task/%d/attr/exec", tid); |
| 782 | |
| 783 | base::ScopedFD fd(open(exec_path.c_str(), O_WRONLY | O_CLOEXEC)); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 784 | if (!fd.is_valid()) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 785 | PLOG(ERROR) << "Failed to open " << exec_path; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 786 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 787 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 788 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 789 | if (!base::WriteFileDescriptor(fd.get(), init_domain, strlen(init_domain))) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 790 | PLOG(ERROR) << "Failed to write the SELinux label to " << exec_path; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 791 | return -errno; |
| 792 | } |
| 793 | |
| 794 | return 0; |
| 795 | } |
| 796 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 797 | bool ContainerTeardown(struct container* c) { |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 798 | UnmountExternalMounts(c); |
| 799 | if (!c->runfsroot.empty() && !c->runfs.empty()) { |
| 800 | /* |c->runfsroot| may have been mounted recursively. Thus use |
| 801 | * MNT_DETACH to "immediately disconnect the filesystem and all |
| 802 | * filesystems mounted below it from each other and from the |
| 803 | * mount table". Otherwise one would need to unmount every |
| 804 | * single dependent mount before unmounting |c->runfsroot| |
| 805 | * itself. |
| 806 | */ |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 807 | if (umount2(c->runfsroot.value().c_str(), MNT_DETACH) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 808 | PLOG(ERROR) << "Failed to detach " << c->runfsroot.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 809 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 810 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 811 | if (rmdir(c->runfsroot.value().c_str()) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 812 | PLOG(ERROR) << "Failed to rmdir " << c->runfsroot.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 813 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 814 | } |
Luis Hector Chavez | 15d0d1a | 2017-10-12 09:30:19 -0700 | [diff] [blame] | 815 | c->runfsroot = base::FilePath(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 816 | } |
| 817 | if (!c->pid_file_path.empty()) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 818 | if (unlink(c->pid_file_path.value().c_str()) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 819 | PLOG(ERROR) << "Failed to unlink " << c->pid_file_path.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 820 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 821 | } |
Luis Hector Chavez | 15d0d1a | 2017-10-12 09:30:19 -0700 | [diff] [blame] | 822 | c->pid_file_path = base::FilePath(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 823 | } |
| 824 | if (!c->runfs.empty()) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 825 | if (rmdir(c->runfs.value().c_str()) != 0) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 826 | PLOG(ERROR) << "Failed to rmdir " << c->runfs.value(); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 827 | return false; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 828 | } |
Luis Hector Chavez | 15d0d1a | 2017-10-12 09:30:19 -0700 | [diff] [blame] | 829 | c->runfs = base::FilePath(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 830 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 831 | return true; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Luis Hector Chavez | 15d0d1a | 2017-10-12 09:30:19 -0700 | [diff] [blame] | 834 | void CancelContainerStart(struct container* c) { |
| 835 | if (c->init_pid != -1) |
| 836 | container_kill(c); |
| 837 | ContainerTeardown(c); |
| 838 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 839 | |
| 840 | } // namespace |
| 841 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 842 | struct container_config* container_config_create() { |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 843 | return new (std::nothrow) struct container_config(); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 846 | void container_config_destroy(struct container_config* c) { |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 847 | if (c == nullptr) |
| 848 | return; |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 849 | delete c; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 852 | int container_config_config_root(struct container_config* c, |
| 853 | const char* config_root) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 854 | c->config_root = base::FilePath(config_root); |
| 855 | return 0; |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame] | 856 | } |
| 857 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 858 | const char* container_config_get_config_root(const struct container_config* c) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 859 | return c->config_root.value().c_str(); |
Mike Frysinger | b22acdf | 2017-01-08 02:02:35 -0500 | [diff] [blame] | 860 | } |
| 861 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 862 | int container_config_rootfs(struct container_config* c, const char* rootfs) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 863 | c->rootfs = base::FilePath(rootfs); |
| 864 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 867 | const char* container_config_get_rootfs(const struct container_config* c) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 868 | return c->rootfs.value().c_str(); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 871 | void container_config_rootfs_mount_flags(struct container_config* c, |
| 872 | unsigned long rootfs_mount_flags) { |
| 873 | /* Since we are going to add MS_REMOUNT anyways, add it here so we can |
| 874 | * simply check against zero later. MS_BIND is also added to avoid |
| 875 | * re-mounting the original filesystem, since the rootfs is always |
| 876 | * bind-mounted. |
| 877 | */ |
| 878 | c->rootfs_mount_flags = MS_REMOUNT | MS_BIND | rootfs_mount_flags; |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 881 | unsigned long container_config_get_rootfs_mount_flags( |
| 882 | const struct container_config* c) { |
| 883 | return c->rootfs_mount_flags; |
Luis Hector Chavez | c240e7e | 2016-09-22 10:33:03 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 886 | int container_config_premounted_runfs(struct container_config* c, |
| 887 | const char* runfs) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 888 | c->premounted_runfs = base::FilePath(runfs); |
| 889 | return 0; |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 892 | const char* container_config_get_premounted_runfs( |
| 893 | const struct container_config* c) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 894 | return c->premounted_runfs.value().c_str(); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 897 | int container_config_pid_file(struct container_config* c, const char* path) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 898 | c->pid_file_path = base::FilePath(path); |
| 899 | return 0; |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 902 | const char* container_config_get_pid_file(const struct container_config* c) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 903 | return c->pid_file_path.value().c_str(); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 906 | int container_config_program_argv(struct container_config* c, |
| 907 | const char** argv, |
| 908 | size_t num_args) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 909 | if (num_args < 1) { |
| 910 | errno = EINVAL; |
| 911 | return -1; |
| 912 | } |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 913 | c->program_argv.clear(); |
| 914 | c->program_argv.reserve(num_args); |
| 915 | for (size_t i = 0; i < num_args; ++i) |
| 916 | c->program_argv.emplace_back(argv[i]); |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 917 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 920 | size_t container_config_get_num_program_args(const struct container_config* c) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 921 | return c->program_argv.size(); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 924 | const char* container_config_get_program_arg(const struct container_config* c, |
| 925 | size_t index) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 926 | if (index >= c->program_argv.size()) |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 927 | return nullptr; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 928 | return c->program_argv[index].c_str(); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 931 | void container_config_uid(struct container_config* c, uid_t uid) { |
| 932 | c->uid = uid; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 935 | uid_t container_config_get_uid(const struct container_config* c) { |
| 936 | return c->uid; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 939 | int container_config_uid_map(struct container_config* c, const char* uid_map) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 940 | c->uid_map = uid_map; |
| 941 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 944 | void container_config_gid(struct container_config* c, gid_t gid) { |
| 945 | c->gid = gid; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 948 | gid_t container_config_get_gid(const struct container_config* c) { |
| 949 | return c->gid; |
Dylan Reid | 1874feb | 2016-06-22 17:53:50 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 952 | int container_config_gid_map(struct container_config* c, const char* gid_map) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 953 | c->gid_map = gid_map; |
| 954 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 957 | int container_config_alt_syscall_table(struct container_config* c, |
| 958 | const char* alt_syscall_table) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 959 | c->alt_syscall_table = alt_syscall_table; |
| 960 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 961 | } |
| 962 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 963 | int container_config_add_rlimit(struct container_config* c, |
| 964 | int type, |
| 965 | uint32_t cur, |
| 966 | uint32_t max) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 967 | if (c->num_rlimits >= kMaxRlimits) { |
| 968 | errno = ENOMEM; |
| 969 | return -1; |
| 970 | } |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 971 | c->rlimits[c->num_rlimits].type = type; |
| 972 | c->rlimits[c->num_rlimits].cur = cur; |
| 973 | c->rlimits[c->num_rlimits].max = max; |
| 974 | c->num_rlimits++; |
| 975 | return 0; |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 978 | int container_config_add_mount(struct container_config* c, |
| 979 | const char* name, |
| 980 | const char* source, |
| 981 | const char* destination, |
| 982 | const char* type, |
| 983 | const char* data, |
| 984 | const char* verity, |
| 985 | int flags, |
| 986 | int uid, |
| 987 | int gid, |
| 988 | int mode, |
| 989 | int mount_in_ns, |
| 990 | int create, |
| 991 | int loopback) { |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 992 | if (name == nullptr || source == nullptr || destination == nullptr || |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 993 | type == nullptr) { |
| 994 | errno = EINVAL; |
| 995 | return -1; |
| 996 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 997 | |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 998 | c->mounts.emplace_back(Mount{name, |
| 999 | base::FilePath(source), |
| 1000 | base::FilePath(destination), |
| 1001 | type, |
| 1002 | data ? data : "", |
| 1003 | verity ? verity : "", |
| 1004 | flags, |
| 1005 | uid, |
| 1006 | gid, |
| 1007 | mode, |
| 1008 | mount_in_ns != 0, |
| 1009 | create != 0, |
| 1010 | loopback != 0}); |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1011 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1012 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1015 | int container_config_add_cgroup_device(struct container_config* c, |
| 1016 | int allow, |
| 1017 | char type, |
| 1018 | int major, |
| 1019 | int minor, |
| 1020 | int read, |
| 1021 | int write, |
| 1022 | int modify) { |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 1023 | c->cgroup_devices.emplace_back(CgroupDevice{ |
| 1024 | allow != 0, type, major, minor, read != 0, write != 0, modify != 0}); |
Dylan Reid | 4843d6b | 2017-03-31 18:14:30 -0700 | [diff] [blame] | 1025 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1026 | return 0; |
Dylan Reid | 4843d6b | 2017-03-31 18:14:30 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1029 | int container_config_add_device(struct container_config* c, |
| 1030 | char type, |
| 1031 | const char* path, |
| 1032 | int fs_permissions, |
| 1033 | int major, |
| 1034 | int minor, |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 1035 | int copy_major, |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1036 | int copy_minor, |
| 1037 | int uid, |
| 1038 | int gid, |
| 1039 | int read_allowed, |
| 1040 | int write_allowed, |
| 1041 | int modify_allowed) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1042 | if (path == nullptr) { |
| 1043 | errno = EINVAL; |
| 1044 | return -1; |
| 1045 | } |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 1046 | /* If using a dynamic major/minor number, ensure that major/minor is -1. */ |
| 1047 | if ((copy_major && (major != -1)) || (copy_minor && (minor != -1))) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1048 | errno = EINVAL; |
| 1049 | return -1; |
| 1050 | } |
Dylan Reid | 355d5e4 | 2016-04-29 16:53:31 -0700 | [diff] [blame] | 1051 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1052 | if (read_allowed || write_allowed || modify_allowed) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1053 | if (container_config_add_cgroup_device(c, 1, type, major, minor, |
| 1054 | read_allowed, write_allowed, |
| 1055 | modify_allowed) != 0) { |
| 1056 | errno = ENOMEM; |
| 1057 | return -1; |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 1058 | } |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1059 | } |
Luis Hector Chavez | 479b95f | 2016-06-06 08:01:05 -0700 | [diff] [blame] | 1060 | |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 1061 | c->devices.emplace_back(Device{ |
Stephen Barber | 7bae664 | 2017-11-30 10:47:12 -0800 | [diff] [blame] | 1062 | type, base::FilePath(path), fs_permissions, major, minor, copy_major != 0, |
| 1063 | copy_minor != 0, uid, gid, |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 1064 | }); |
| 1065 | |
| 1066 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1069 | int container_config_run_setfiles(struct container_config* c, |
| 1070 | const char* setfiles_cmd) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1071 | c->run_setfiles = setfiles_cmd; |
| 1072 | return 0; |
Dylan Reid | 2bd9ea9 | 2016-04-07 20:57:47 -0700 | [diff] [blame] | 1073 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1074 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1075 | const char* container_config_get_run_setfiles( |
| 1076 | const struct container_config* c) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1077 | return c->run_setfiles.c_str(); |
Dylan Reid | 1145672 | 2016-05-02 11:24:50 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1080 | int container_config_set_cpu_shares(struct container_config* c, int shares) { |
| 1081 | /* CPU shares must be 2 or higher. */ |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1082 | if (shares < 2) { |
| 1083 | errno = EINVAL; |
| 1084 | return -1; |
| 1085 | } |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1086 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1087 | c->cpu_cgparams.shares = shares; |
| 1088 | return 0; |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1089 | } |
| 1090 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1091 | int container_config_set_cpu_cfs_params(struct container_config* c, |
| 1092 | int quota, |
| 1093 | int period) { |
| 1094 | /* |
| 1095 | * quota could be set higher than period to utilize more than one CPU. |
| 1096 | * quota could also be set as -1 to indicate the cgroup does not adhere |
| 1097 | * to any CPU time restrictions. |
| 1098 | */ |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1099 | if (quota <= 0 && quota != -1) { |
| 1100 | errno = EINVAL; |
| 1101 | return -1; |
| 1102 | } |
| 1103 | if (period <= 0) { |
| 1104 | errno = EINVAL; |
| 1105 | return -1; |
| 1106 | } |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1107 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1108 | c->cpu_cgparams.quota = quota; |
| 1109 | c->cpu_cgparams.period = period; |
| 1110 | return 0; |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1113 | int container_config_set_cpu_rt_params(struct container_config* c, |
| 1114 | int rt_runtime, |
| 1115 | int rt_period) { |
| 1116 | /* |
| 1117 | * rt_runtime could be set as 0 to prevent the cgroup from using |
| 1118 | * realtime CPU. |
| 1119 | */ |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1120 | if (rt_runtime < 0 || rt_runtime >= rt_period) { |
| 1121 | errno = EINVAL; |
| 1122 | return -1; |
| 1123 | } |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1124 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1125 | c->cpu_cgparams.rt_runtime = rt_runtime; |
| 1126 | c->cpu_cgparams.rt_period = rt_period; |
| 1127 | return 0; |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1130 | int container_config_get_cpu_shares(struct container_config* c) { |
| 1131 | return c->cpu_cgparams.shares; |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1134 | int container_config_get_cpu_quota(struct container_config* c) { |
| 1135 | return c->cpu_cgparams.quota; |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 1136 | } |
| 1137 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1138 | int container_config_get_cpu_period(struct container_config* c) { |
| 1139 | return c->cpu_cgparams.period; |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1142 | int container_config_get_cpu_rt_runtime(struct container_config* c) { |
| 1143 | return c->cpu_cgparams.rt_runtime; |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1146 | int container_config_get_cpu_rt_period(struct container_config* c) { |
| 1147 | return c->cpu_cgparams.rt_period; |
Chinyue Chen | 4f3fd68 | 2016-07-01 14:11:42 +0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1150 | int container_config_set_cgroup_parent(struct container_config* c, |
| 1151 | const char* parent, |
| 1152 | uid_t cgroup_owner, |
| 1153 | gid_t cgroup_group) { |
| 1154 | c->cgroup_owner = cgroup_owner; |
| 1155 | c->cgroup_group = cgroup_group; |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1156 | c->cgroup_parent = base::FilePath(parent); |
| 1157 | return 0; |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1160 | const char* container_config_get_cgroup_parent(struct container_config* c) { |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1161 | return c->cgroup_parent.value().c_str(); |
Dylan Reid | 9e724af | 2016-07-21 09:58:07 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Stephen Barber | 771653f | 2017-10-04 23:48:57 -0700 | [diff] [blame] | 1164 | int container_config_namespaces(struct container_config* c, |
| 1165 | const char** namespaces, |
| 1166 | size_t num_ns) { |
| 1167 | if (num_ns < 1) |
| 1168 | return -EINVAL; |
| 1169 | c->namespaces.clear(); |
| 1170 | for (size_t i = 0; i < num_ns; ++i) |
| 1171 | c->namespaces.emplace(namespaces[i]); |
| 1172 | return 0; |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Stephen Barber | 771653f | 2017-10-04 23:48:57 -0700 | [diff] [blame] | 1175 | size_t container_config_get_num_namespaces(const struct container_config* c) { |
| 1176 | return c->namespaces.size(); |
| 1177 | } |
| 1178 | |
| 1179 | bool container_config_has_namespace(const struct container_config* c, |
| 1180 | const char* ns) { |
| 1181 | return c->namespaces.find(ns) != c->namespaces.end(); |
Keshav Santhanam | 1b6bf67 | 2016-08-10 18:35:12 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1184 | void container_config_keep_fds_open(struct container_config* c) { |
yusukes | f125f33 | 2017-12-08 13:45:15 -0800 | [diff] [blame] | 1185 | c->keep_fds_open = true; |
Dylan Reid | c433584 | 2016-11-11 10:24:52 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1188 | void container_config_set_capmask(struct container_config* c, |
| 1189 | uint64_t capmask, |
| 1190 | int ambient) { |
yusukes | f125f33 | 2017-12-08 13:45:15 -0800 | [diff] [blame] | 1191 | c->use_capmask = true; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1192 | c->capmask = capmask; |
| 1193 | c->use_capmask_ambient = ambient; |
Luis Hector Chavez | ff5978f | 2017-06-27 12:52:58 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1196 | void container_config_set_securebits_skip_mask(struct container_config* c, |
| 1197 | uint64_t securebits_skip_mask) { |
| 1198 | c->securebits_skip_mask = securebits_skip_mask; |
Luis Hector Chavez | cd44ba7 | 2017-06-30 13:01:38 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1201 | void container_config_set_run_as_init(struct container_config* c, |
| 1202 | int run_as_init) { |
| 1203 | c->do_init = !run_as_init; |
Luis Hector Chavez | dac65c3 | 2017-07-21 10:30:23 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1206 | int container_config_set_selinux_context(struct container_config* c, |
| 1207 | const char* context) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1208 | if (!context) { |
| 1209 | errno = EINVAL; |
| 1210 | return -1; |
| 1211 | } |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1212 | c->selinux_context = context; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1213 | return 0; |
Luis Hector Chavez | 15e8e67 | 2017-07-20 15:13:27 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1216 | void container_config_set_pre_execve_hook(struct container_config* c, |
| 1217 | int (*hook)(void*), |
| 1218 | void* payload) { |
| 1219 | c->pre_start_hook = hook; |
| 1220 | c->pre_start_hook_payload = payload; |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1223 | void container_config_add_hook(struct container_config* c, |
| 1224 | minijail_hook_event_t event, |
| 1225 | libcontainer::HookCallback callback) { |
| 1226 | auto it = c->hooks.insert( |
| 1227 | std::make_pair(event, std::vector<libcontainer::HookCallback>())); |
| 1228 | it.first->second.emplace_back(std::move(callback)); |
| 1229 | } |
| 1230 | |
Luis Hector Chavez | e03926a | 2017-09-28 17:28:49 -0700 | [diff] [blame] | 1231 | int container_config_add_hook(struct container_config* c, |
| 1232 | minijail_hook_event_t event, |
| 1233 | const char* filename, |
| 1234 | const char** argv, |
| 1235 | size_t num_args, |
| 1236 | int* pstdin_fd, |
| 1237 | int* pstdout_fd, |
| 1238 | int* pstderr_fd) { |
| 1239 | std::vector<std::string> args; |
| 1240 | args.reserve(num_args); |
| 1241 | for (size_t i = 0; i < num_args; ++i) |
| 1242 | args.emplace_back(argv[i]); |
| 1243 | |
| 1244 | // First element of the array belongs to the parent and the second one belongs |
| 1245 | // to the child. |
| 1246 | base::ScopedFD stdin_fds[2], stdout_fds[2], stderr_fds[2]; |
| 1247 | if (pstdin_fd) { |
| 1248 | if (!libcontainer::Pipe2(&stdin_fds[1], &stdin_fds[0], 0)) |
| 1249 | return -1; |
| 1250 | } |
| 1251 | if (pstdout_fd) { |
| 1252 | if (!libcontainer::Pipe2(&stdout_fds[0], &stdout_fds[0], 0)) |
| 1253 | return -1; |
| 1254 | } |
| 1255 | if (pstderr_fd) { |
| 1256 | if (!libcontainer::Pipe2(&stderr_fds[0], &stderr_fds[0], 0)) |
| 1257 | return -1; |
| 1258 | } |
| 1259 | |
| 1260 | // After this point the call has been successful, so we can now commit to |
| 1261 | // whatever pipes we have opened. |
| 1262 | if (pstdin_fd) { |
| 1263 | *pstdin_fd = stdin_fds[0].release(); |
| 1264 | c->inherited_fds.emplace_back(stdin_fds[1].get()); |
| 1265 | } |
| 1266 | if (pstdout_fd) { |
| 1267 | *pstdout_fd = stdout_fds[0].release(); |
| 1268 | c->inherited_fds.emplace_back(stdout_fds[1].get()); |
| 1269 | } |
| 1270 | if (pstderr_fd) { |
| 1271 | *pstderr_fd = stderr_fds[0].release(); |
| 1272 | c->inherited_fds.emplace_back(stderr_fds[1].get()); |
| 1273 | } |
| 1274 | container_config_add_hook( |
| 1275 | c, event, |
| 1276 | libcontainer::CreateExecveCallback( |
| 1277 | base::FilePath(filename), args, std::move(stdin_fds[1]), |
| 1278 | std::move(stdout_fds[1]), std::move(stderr_fds[1]))); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1282 | int container_config_inherit_fds(struct container_config* c, |
| 1283 | int* inherited_fds, |
| 1284 | size_t inherited_fd_count) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1285 | if (!c->inherited_fds.empty()) { |
| 1286 | errno = EINVAL; |
| 1287 | return -1; |
| 1288 | } |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1289 | for (size_t i = 0; i < inherited_fd_count; ++i) |
| 1290 | c->inherited_fds.emplace_back(inherited_fds[i]); |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1291 | return 0; |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1294 | struct container* container_new(const char* name, const char* rundir) { |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1295 | struct container* c = new (std::nothrow) container(); |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1296 | if (!c) |
| 1297 | return nullptr; |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1298 | c->rundir = base::FilePath(rundir); |
| 1299 | c->name = name; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1300 | return c; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1303 | void container_destroy(struct container* c) { |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1304 | delete c; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1307 | int container_start(struct container* c, |
| 1308 | const struct container_config* config) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1309 | if (!c) { |
| 1310 | errno = EINVAL; |
| 1311 | return -1; |
| 1312 | } |
| 1313 | if (!config) { |
| 1314 | errno = EINVAL; |
| 1315 | return -1; |
| 1316 | } |
| 1317 | if (config->program_argv.empty()) { |
| 1318 | errno = EINVAL; |
| 1319 | return -1; |
| 1320 | } |
Dylan Reid | e040c6b | 2016-05-02 18:49:02 -0700 | [diff] [blame] | 1321 | |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1322 | // This will run in all the error cases. |
| 1323 | base::ScopedClosureRunner teardown( |
Luis Hector Chavez | 15d0d1a | 2017-10-12 09:30:19 -0700 | [diff] [blame] | 1324 | base::Bind(&CancelContainerStart, base::Unretained(c))); |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1325 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1326 | if (!config->config_root.empty()) |
| 1327 | c->config_root = config->config_root; |
| 1328 | if (!config->premounted_runfs.empty()) { |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1329 | c->runfs.clear(); |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1330 | c->runfsroot = config->premounted_runfs; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1331 | } else { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1332 | if (!MountRunfs(c, config)) |
| 1333 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1334 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1335 | |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1336 | c->jail.reset(minijail_new()); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1337 | if (!c->jail) { |
| 1338 | errno = ENOMEM; |
| 1339 | return -1; |
| 1340 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1341 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1342 | if (!DoContainerMounts(c, config)) |
| 1343 | return -1; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1344 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1345 | int cgroup_uid; |
| 1346 | if (!GetUsernsOutsideId(config->uid_map, config->cgroup_owner, &cgroup_uid)) |
| 1347 | return -1; |
| 1348 | int cgroup_gid; |
| 1349 | if (!GetUsernsOutsideId(config->gid_map, config->cgroup_group, &cgroup_gid)) |
| 1350 | return -1; |
Stephen Barber | 1a398c7 | 2017-01-23 12:39:44 -0800 | [diff] [blame] | 1351 | |
Luis Hector Chavez | 76ae9ac | 2017-09-20 21:13:08 -0700 | [diff] [blame] | 1352 | c->cgroup = libcontainer::Cgroup::Create(c->name, |
| 1353 | base::FilePath("/sys/fs/cgroup"), |
| 1354 | config->cgroup_parent, |
| 1355 | cgroup_uid, |
| 1356 | cgroup_gid); |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1357 | if (!c->cgroup) |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1358 | return -1; |
Dylan Reid | a996642 | 2016-07-21 10:11:34 -0700 | [diff] [blame] | 1359 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1360 | // Must be root to modify device cgroup or mknod. |
| 1361 | std::map<minijail_hook_event_t, std::vector<libcontainer::HookCallback>> |
| 1362 | hook_callbacks; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1363 | if (getuid() == 0) { |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1364 | if (!config->devices.empty()) { |
| 1365 | // Create the devices in the mount namespace. |
| 1366 | auto it = hook_callbacks.insert( |
| 1367 | std::make_pair(MINIJAIL_HOOK_EVENT_PRE_CHROOT, |
| 1368 | std::vector<libcontainer::HookCallback>())); |
| 1369 | it.first->second.emplace_back( |
| 1370 | libcontainer::AdaptCallbackToRunInNamespaces( |
| 1371 | base::Bind(&CreateDeviceNodes, base::Unretained(c), |
| 1372 | base::Unretained(config)), |
| 1373 | {CLONE_NEWNS})); |
| 1374 | } |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1375 | if (!DeviceSetup(c, config)) |
| 1376 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1377 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1378 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1379 | // Potentially run setfiles on mounts configured outside of the jail. |
| 1380 | if (!config->run_setfiles.empty()) { |
| 1381 | const base::FilePath kDataPath("/data"); |
| 1382 | const base::FilePath kCachePath("/cache"); |
| 1383 | std::vector<base::FilePath> destinations; |
| 1384 | for (const auto& mnt : config->mounts) { |
| 1385 | if (mnt.mount_in_ns) |
| 1386 | continue; |
| 1387 | if (mnt.flags & MS_RDONLY) |
| 1388 | continue; |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1389 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1390 | // A hack to avoid setfiles on /data and /cache. |
| 1391 | if (mnt.destination == kDataPath || mnt.destination == kCachePath) |
| 1392 | continue; |
Yusuke Sato | 91f11f0 | 2016-12-02 16:15:13 -0800 | [diff] [blame] | 1393 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1394 | destinations.emplace_back( |
| 1395 | GetPathInOuterNamespace(c->runfsroot, mnt.destination)); |
| 1396 | } |
| 1397 | |
| 1398 | if (!destinations.empty()) { |
| 1399 | auto it = hook_callbacks.insert( |
| 1400 | std::make_pair(MINIJAIL_HOOK_EVENT_PRE_CHROOT, |
| 1401 | std::vector<libcontainer::HookCallback>())); |
| 1402 | it.first->second.emplace_back( |
| 1403 | libcontainer::AdaptCallbackToRunInNamespaces( |
| 1404 | base::Bind(&RunSetfilesCommand, base::Unretained(c), |
| 1405 | base::Unretained(config), destinations), |
| 1406 | {CLONE_NEWNS})); |
| 1407 | } |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1408 | } |
Dylan Reid | d722958 | 2016-04-27 17:08:40 -0700 | [diff] [blame] | 1409 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1410 | /* Setup CPU cgroup params. */ |
| 1411 | if (config->cpu_cgparams.shares) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1412 | if (!c->cgroup->SetCpuShares(config->cpu_cgparams.shares)) |
| 1413 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1414 | } |
| 1415 | if (config->cpu_cgparams.period) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1416 | if (!c->cgroup->SetCpuQuota(config->cpu_cgparams.quota)) |
| 1417 | return -1; |
| 1418 | if (!c->cgroup->SetCpuPeriod(config->cpu_cgparams.period)) |
| 1419 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1420 | } |
| 1421 | if (config->cpu_cgparams.rt_period) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1422 | if (!c->cgroup->SetCpuRtRuntime(config->cpu_cgparams.rt_runtime)) |
| 1423 | return -1; |
| 1424 | if (!c->cgroup->SetCpuRtPeriod(config->cpu_cgparams.rt_period)) |
| 1425 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1426 | } |
Chinyue Chen | fac909e | 2016-06-24 14:17:42 +0800 | [diff] [blame] | 1427 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1428 | /* Setup and start the container with libminijail. */ |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1429 | if (!config->pid_file_path.empty()) |
| 1430 | c->pid_file_path = config->pid_file_path; |
| 1431 | else if (!c->runfs.empty()) |
| 1432 | c->pid_file_path = c->runfs.Append("container.pid"); |
Keshav Santhanam | 0e4c328 | 2016-07-14 10:25:16 -0700 | [diff] [blame] | 1433 | |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1434 | if (!c->pid_file_path.empty()) |
| 1435 | minijail_write_pid_file(c->jail.get(), c->pid_file_path.value().c_str()); |
| 1436 | minijail_reset_signal_mask(c->jail.get()); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1437 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1438 | /* Setup container namespaces. */ |
Stephen Barber | 771653f | 2017-10-04 23:48:57 -0700 | [diff] [blame] | 1439 | if (container_config_has_namespace(config, "ipc")) |
| 1440 | minijail_namespace_ipc(c->jail.get()); |
| 1441 | if (container_config_has_namespace(config, "mount")) |
| 1442 | minijail_namespace_vfs(c->jail.get()); |
| 1443 | if (container_config_has_namespace(config, "network")) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1444 | minijail_namespace_net(c->jail.get()); |
Stephen Barber | 771653f | 2017-10-04 23:48:57 -0700 | [diff] [blame] | 1445 | if (container_config_has_namespace(config, "pid")) |
| 1446 | minijail_namespace_pids(c->jail.get()); |
| 1447 | |
| 1448 | if (container_config_has_namespace(config, "user")) { |
| 1449 | minijail_namespace_user(c->jail.get()); |
| 1450 | if (minijail_uidmap(c->jail.get(), config->uid_map.c_str()) != 0) |
| 1451 | return -1; |
| 1452 | if (minijail_gidmap(c->jail.get(), config->gid_map.c_str()) != 0) |
| 1453 | return -1; |
| 1454 | } |
| 1455 | |
| 1456 | if (container_config_has_namespace(config, "cgroup")) |
| 1457 | minijail_namespace_cgroups(c->jail.get()); |
| 1458 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1459 | if (getuid() != 0) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1460 | minijail_namespace_user_disable_setgroups(c->jail.get()); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1461 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1462 | /* Set the UID/GID inside the container if not 0. */ |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1463 | if (!GetUsernsOutsideId(config->uid_map, config->uid, nullptr)) |
| 1464 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1465 | else if (config->uid > 0) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1466 | minijail_change_uid(c->jail.get(), config->uid); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1467 | if (!GetUsernsOutsideId(config->gid_map, config->gid, nullptr)) |
| 1468 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1469 | else if (config->gid > 0) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1470 | minijail_change_gid(c->jail.get(), config->gid); |
Keshav Santhanam | 36485ff | 2016-08-02 16:21:02 -0700 | [diff] [blame] | 1471 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1472 | if (minijail_enter_pivot_root(c->jail.get(), c->runfsroot.value().c_str()) != |
| 1473 | 0) { |
| 1474 | return -1; |
| 1475 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1476 | |
Luis Hector Chavez | 76ae9ac | 2017-09-20 21:13:08 -0700 | [diff] [blame] | 1477 | // Add the cgroups configured above. |
| 1478 | for (int32_t i = 0; i < libcontainer::Cgroup::Type::NUM_TYPES; i++) { |
| 1479 | if (c->cgroup->has_tasks_path(i)) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1480 | if (minijail_add_to_cgroup( |
| 1481 | c->jail.get(), c->cgroup->tasks_path(i).value().c_str()) != 0) { |
| 1482 | return -1; |
| 1483 | } |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1484 | } |
| 1485 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1486 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1487 | if (!config->alt_syscall_table.empty()) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1488 | minijail_use_alt_syscall(c->jail.get(), config->alt_syscall_table.c_str()); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1489 | |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1490 | for (int i = 0; i < config->num_rlimits; i++) { |
Luis Hector Chavez | e1062e8 | 2017-09-18 09:57:37 -0700 | [diff] [blame] | 1491 | const Rlimit& lim = config->rlimits[i]; |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1492 | if (minijail_rlimit(c->jail.get(), lim.type, lim.cur, lim.max) != 0) |
| 1493 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1494 | } |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 1495 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1496 | if (!config->selinux_context.empty()) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1497 | if (minijail_add_hook(c->jail.get(), &Setexeccon, |
| 1498 | const_cast<char*>(config->selinux_context.c_str()), |
| 1499 | MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) { |
| 1500 | return -1; |
| 1501 | } |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1502 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1503 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1504 | if (config->pre_start_hook) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1505 | if (minijail_add_hook(c->jail.get(), config->pre_start_hook, |
| 1506 | config->pre_start_hook_payload, |
| 1507 | MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) { |
| 1508 | return -1; |
| 1509 | } |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1510 | } |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 1511 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1512 | // Now that all pre-requisite hooks are installed, copy the ones in the |
| 1513 | // container_config object in the correct order. |
| 1514 | for (const auto& config_hook : config->hooks) { |
| 1515 | auto it = hook_callbacks.insert(std::make_pair( |
| 1516 | config_hook.first, std::vector<libcontainer::HookCallback>())); |
| 1517 | it.first->second.insert(it.first->second.end(), config_hook.second.begin(), |
| 1518 | config_hook.second.end()); |
| 1519 | } |
| 1520 | |
| 1521 | c->hook_states.clear(); |
| 1522 | // Reserve enough memory to hold all the hooks, so that their addresses do not |
| 1523 | // get invalidated by reallocation. |
| 1524 | c->hook_states.reserve(MINIJAIL_HOOK_EVENT_MAX); |
| 1525 | for (minijail_hook_event_t event : {MINIJAIL_HOOK_EVENT_PRE_CHROOT, |
| 1526 | MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS, |
| 1527 | MINIJAIL_HOOK_EVENT_PRE_EXECVE}) { |
| 1528 | const auto& it = hook_callbacks.find(event); |
| 1529 | if (it == hook_callbacks.end()) |
| 1530 | continue; |
| 1531 | c->hook_states.emplace_back( |
| 1532 | std::make_pair(libcontainer::HookState(), it->second)); |
| 1533 | if (!c->hook_states.back().first.InstallHook(c->jail.get(), event)) |
| 1534 | return -1; |
| 1535 | } |
| 1536 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1537 | for (int fd : config->inherited_fds) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1538 | if (minijail_preserve_fd(c->jail.get(), fd, fd) != 0) |
| 1539 | return -1; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1540 | } |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 1541 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1542 | /* TODO(dgreid) - remove this once shared mounts are cleaned up. */ |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1543 | minijail_skip_remount_private(c->jail.get()); |
Dylan Reid | 3da683b | 2016-04-05 03:35:35 -0700 | [diff] [blame] | 1544 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1545 | if (!config->keep_fds_open) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1546 | minijail_close_open_fds(c->jail.get()); |
Luis Hector Chavez | e18e7d4 | 2016-10-12 07:35:32 -0700 | [diff] [blame] | 1547 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1548 | if (config->use_capmask) { |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1549 | minijail_use_caps(c->jail.get(), config->capmask); |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1550 | if (config->use_capmask_ambient) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1551 | minijail_set_ambient_caps(c->jail.get()); |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1552 | if (config->securebits_skip_mask) { |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1553 | minijail_skip_setting_securebits(c->jail.get(), |
| 1554 | config->securebits_skip_mask); |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1555 | } |
| 1556 | } |
Luis Hector Chavez | ff5978f | 2017-06-27 12:52:58 -0700 | [diff] [blame] | 1557 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1558 | if (!config->do_init) |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1559 | minijail_run_as_init(c->jail.get()); |
Luis Hector Chavez | dac65c3 | 2017-07-21 10:30:23 -0700 | [diff] [blame] | 1560 | |
Luis Hector Chavez | 9cde12a | 2017-09-18 10:53:38 -0700 | [diff] [blame] | 1561 | std::vector<char*> argv_cstr; |
| 1562 | argv_cstr.reserve(config->program_argv.size() + 1); |
| 1563 | for (const auto& arg : config->program_argv) |
| 1564 | argv_cstr.emplace_back(const_cast<char*>(arg.c_str())); |
| 1565 | argv_cstr.emplace_back(nullptr); |
| 1566 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1567 | if (minijail_run_pid_pipes_no_preload(c->jail.get(), argv_cstr[0], |
| 1568 | argv_cstr.data(), &c->init_pid, nullptr, |
| 1569 | nullptr, nullptr) != 0) { |
| 1570 | return -1; |
| 1571 | } |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1572 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 1573 | // |hook_states| is already sorted in the correct order. |
| 1574 | for (auto& hook_state : c->hook_states) { |
| 1575 | if (!hook_state.first.WaitForHookAndRun(hook_state.second, c->init_pid)) |
| 1576 | return -1; |
| 1577 | } |
| 1578 | |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1579 | // The container has started successfully, no need to tear it down anymore. |
| 1580 | ignore_result(teardown.Release()); |
| 1581 | return 0; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1582 | } |
| 1583 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1584 | const char* container_root(struct container* c) { |
Luis Hector Chavez | 5381d00 | 2017-09-16 12:54:24 -0700 | [diff] [blame] | 1585 | return c->runfs.value().c_str(); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1586 | } |
| 1587 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1588 | int container_pid(struct container* c) { |
| 1589 | return c->init_pid; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1590 | } |
| 1591 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1592 | int container_wait(struct container* c) { |
| 1593 | int rc; |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 1594 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1595 | do { |
Luis Hector Chavez | 626f5c8 | 2017-09-18 11:19:32 -0700 | [diff] [blame] | 1596 | rc = minijail_wait(c->jail.get()); |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1597 | } while (rc == -EINTR); |
Dylan Reid | cf745c5 | 2016-04-22 10:18:03 -0700 | [diff] [blame] | 1598 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1599 | // If the process had already been reaped, still perform teardown. |
| 1600 | if (rc == -ECHILD || rc >= 0) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1601 | if (!ContainerTeardown(c)) |
| 1602 | rc = -errno; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1603 | } |
| 1604 | return rc; |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1607 | int container_kill(struct container* c) { |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 1608 | if (kill(c->init_pid, SIGKILL) != 0 && errno != ESRCH) { |
Luis Hector Chavez | dc61f8d | 2017-10-02 11:12:46 -0700 | [diff] [blame] | 1609 | PLOG(ERROR) << "Failed to kill " << c->init_pid; |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1610 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 1611 | } |
Luis Hector Chavez | 31735bc | 2017-09-15 08:17:10 -0700 | [diff] [blame] | 1612 | return container_wait(c); |
Dylan Reid | 837c74a | 2016-01-22 17:25:21 -0800 | [diff] [blame] | 1613 | } |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 1614 | |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 1615 | char* container_config_dump(struct container_config* c, int sort_vectors) { |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 1616 | std::stringstream out; |
yusukes | 3262254 | 2018-01-05 18:59:52 -0800 | [diff] [blame^] | 1617 | DumpConfig(&out, c, sort_vectors); |
yusukes | bbc37a7 | 2017-11-21 09:51:54 -0800 | [diff] [blame] | 1618 | return strdup(out.str().c_str()); |
| 1619 | } |