Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -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. |
| 4 | |
Stephen Barber | 5f6dc9b | 2017-04-04 12:36:32 -0700 | [diff] [blame] | 5 | #include "run_oci/container_config_parser.h" |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 6 | |
Luis Hector Chavez | 8373b41 | 2017-07-10 12:51:07 -0700 | [diff] [blame] | 7 | #include <sys/capability.h> |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 8 | #include <sys/resource.h> |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | |
Luis Hector Chavez | 8e4dcc1 | 2017-06-27 12:54:47 -0700 | [diff] [blame] | 11 | #include <map> |
Ben Chan | f43980b | 2017-03-10 11:31:46 -0800 | [diff] [blame] | 12 | #include <regex> // NOLINT(build/c++11) |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 13 | #include <string> |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 14 | #include <utility> |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include <base/json/json_reader.h> |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 18 | #include <base/strings/string_split.h> |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 19 | #include <base/values.h> |
| 20 | |
Stephen Barber | 5f6dc9b | 2017-04-04 12:36:32 -0700 | [diff] [blame] | 21 | namespace run_oci { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 25 | // Gets a uint32 from the given dictionary. |
| 26 | bool ParseUint32FromDict(const base::DictionaryValue& dict, const char *name, |
| 27 | uint32_t* val_out) { |
| 28 | double double_val; |
| 29 | if (!dict.GetDouble(name, &double_val)) { |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 30 | return false; |
| 31 | } |
| 32 | *val_out = double_val; |
| 33 | return true; |
| 34 | } |
| 35 | |
Dylan Reid | b0a7277 | 2016-11-03 16:27:50 +0000 | [diff] [blame] | 36 | // Gets a uint64 from the given dictionary. |
| 37 | bool ParseUint64FromDict(const base::DictionaryValue& dict, const char *name, |
| 38 | uint64_t* val_out) { |
| 39 | double double_val; |
| 40 | if (!dict.GetDouble(name, &double_val)) { |
Dylan Reid | b0a7277 | 2016-11-03 16:27:50 +0000 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | *val_out = double_val; |
| 44 | return true; |
| 45 | } |
| 46 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 47 | // Parses basic platform configuration. |
| 48 | bool ParsePlatformConfig(const base::DictionaryValue& config_root_dict, |
| 49 | OciConfigPtr const& config_out) { |
| 50 | // |platform_dict| stays owned by |config_root_dict| |
| 51 | const base::DictionaryValue* platform_dict = nullptr; |
| 52 | if (!config_root_dict.GetDictionary("platform", &platform_dict)) { |
| 53 | LOG(ERROR) << "Fail to parse platform dictionary from config"; |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | if (!platform_dict->GetString("os", &config_out->platform.os)) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | if (!platform_dict->GetString("arch", &config_out->platform.arch)) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | // Parses root fs info. |
| 69 | bool ParseRootFileSystemConfig(const base::DictionaryValue& config_root_dict, |
| 70 | OciConfigPtr const& config_out) { |
| 71 | // |rootfs_dict| stays owned by |config_root_dict| |
| 72 | const base::DictionaryValue* rootfs_dict = nullptr; |
| 73 | if (!config_root_dict.GetDictionary("root", &rootfs_dict)) { |
| 74 | LOG(ERROR) << "Fail to parse rootfs dictionary from config"; |
| 75 | return false; |
| 76 | } |
Luis Hector Chavez | 2a90f29 | 2017-10-02 09:51:28 -0700 | [diff] [blame^] | 77 | std::string path; |
| 78 | if (!rootfs_dict->GetString("path", &path)) { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 79 | LOG(ERROR) << "Fail to get rootfs path from config"; |
| 80 | return false; |
| 81 | } |
Luis Hector Chavez | 2a90f29 | 2017-10-02 09:51:28 -0700 | [diff] [blame^] | 82 | config_out->root.path = base::FilePath(path); |
Dylan Reid | 6d65074 | 2016-11-02 23:10:38 -0700 | [diff] [blame] | 83 | rootfs_dict->GetBoolean("readonly", &config_out->root.readonly); |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Luis Hector Chavez | 8e4dcc1 | 2017-06-27 12:54:47 -0700 | [diff] [blame] | 87 | // Fills |config_out| with information about the capability sets in the |
| 88 | // container. |
| 89 | bool ParseCapabilitiesConfig(const base::DictionaryValue& capabilities_dict, |
| 90 | std::map<std::string, CapSet>* config_out) { |
| 91 | constexpr const char* kCapabilitySetNames[] = { |
| 92 | "effective", "bounding", "inheritable", "permitted", "ambient"}; |
| 93 | const std::string kAmbientCapabilitySetName = "ambient"; |
| 94 | |
| 95 | CapSet caps_superset; |
| 96 | for (const char* set_name : kCapabilitySetNames) { |
| 97 | // |capset_list| stays owned by |capabilities_dict|. |
| 98 | const base::ListValue* capset_list = nullptr; |
| 99 | if (!capabilities_dict.GetList(set_name, &capset_list)) |
| 100 | continue; |
| 101 | CapSet caps; |
Luis Hector Chavez | 8373b41 | 2017-07-10 12:51:07 -0700 | [diff] [blame] | 102 | cap_value_t cap_value; |
Luis Hector Chavez | 8e4dcc1 | 2017-06-27 12:54:47 -0700 | [diff] [blame] | 103 | for (const auto* cap_name_value : *capset_list) { |
| 104 | std::string cap_name; |
| 105 | if (!cap_name_value->GetAsString(&cap_name)) { |
| 106 | LOG(ERROR) << "Capability list " << set_name |
| 107 | << " contains a non-string"; |
| 108 | return false; |
| 109 | } |
Luis Hector Chavez | 8373b41 | 2017-07-10 12:51:07 -0700 | [diff] [blame] | 110 | if (cap_from_name(cap_name.c_str(), &cap_value) == -1) { |
Luis Hector Chavez | 8e4dcc1 | 2017-06-27 12:54:47 -0700 | [diff] [blame] | 111 | LOG(ERROR) << "Unrecognized capability name: " << cap_name; |
| 112 | return false; |
| 113 | } |
Luis Hector Chavez | 8373b41 | 2017-07-10 12:51:07 -0700 | [diff] [blame] | 114 | caps[cap_value] = true; |
Luis Hector Chavez | 8e4dcc1 | 2017-06-27 12:54:47 -0700 | [diff] [blame] | 115 | } |
| 116 | (*config_out)[set_name] = caps; |
| 117 | caps_superset = caps; |
| 118 | } |
| 119 | |
| 120 | // We currently only support sets that are identical, except that ambient is |
| 121 | // optional. |
| 122 | for (const char* set_name : kCapabilitySetNames) { |
| 123 | auto it = config_out->find(set_name); |
| 124 | if (it == config_out->end() && set_name == kAmbientCapabilitySetName) { |
| 125 | // Ambient capabilities are optional. |
| 126 | continue; |
| 127 | } |
| 128 | if (it == config_out->end()) { |
| 129 | LOG(ERROR) |
| 130 | << "If capabilities are set, all capability sets should be present"; |
| 131 | return false; |
| 132 | } |
| 133 | if (it->second != caps_superset) { |
| 134 | LOG(ERROR) |
| 135 | << "If capabilities are set, all capability sets should be identical"; |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 143 | const std::map<std::string, int> kRlimitMap = { |
| 144 | #define RLIMIT_MAP_ENTRY(limit) \ |
| 145 | { "RLIMIT_" #limit, RLIMIT_##limit } |
| 146 | RLIMIT_MAP_ENTRY(CPU), |
| 147 | RLIMIT_MAP_ENTRY(FSIZE), |
| 148 | RLIMIT_MAP_ENTRY(DATA), |
| 149 | RLIMIT_MAP_ENTRY(STACK), |
| 150 | RLIMIT_MAP_ENTRY(CORE), |
| 151 | RLIMIT_MAP_ENTRY(RSS), |
| 152 | RLIMIT_MAP_ENTRY(NPROC), |
| 153 | RLIMIT_MAP_ENTRY(NOFILE), |
| 154 | RLIMIT_MAP_ENTRY(MEMLOCK), |
| 155 | RLIMIT_MAP_ENTRY(AS), |
| 156 | RLIMIT_MAP_ENTRY(LOCKS), |
| 157 | RLIMIT_MAP_ENTRY(SIGPENDING), |
| 158 | RLIMIT_MAP_ENTRY(MSGQUEUE), |
| 159 | RLIMIT_MAP_ENTRY(NICE), |
| 160 | RLIMIT_MAP_ENTRY(RTPRIO), |
| 161 | RLIMIT_MAP_ENTRY(RTTIME), |
| 162 | #undef RLIMIT_MAP_ENTRY |
| 163 | }; |
| 164 | |
| 165 | // Fills |config_out| with information about the capability sets in the |
| 166 | // container. |
| 167 | bool ParseRlimitsConfig(const base::ListValue& rlimits_list, |
| 168 | std::vector<OciProcessRlimit>* rlimits_out) { |
| 169 | size_t num_limits = rlimits_list.GetSize(); |
| 170 | for (size_t i = 0; i < num_limits; ++i) { |
| 171 | const base::DictionaryValue* rlimits_dict; |
| 172 | if (!rlimits_list.GetDictionary(i, &rlimits_dict)) { |
| 173 | LOG(ERROR) << "Fail to get rlimit item " << i; |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | std::string rlimit_name; |
| 178 | if (!rlimits_dict->GetString("type", &rlimit_name)) { |
| 179 | LOG(ERROR) << "Fail to get type of rlimit " << i; |
| 180 | return false; |
| 181 | } |
| 182 | const auto it = kRlimitMap.find(rlimit_name); |
| 183 | if (it == kRlimitMap.end()) { |
| 184 | LOG(ERROR) << "Unrecognized rlimit name: " << rlimit_name; |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | OciProcessRlimit limit; |
| 189 | limit.type = it->second; |
| 190 | if (!ParseUint32FromDict(*rlimits_dict, "hard", &limit.hard)) { |
| 191 | LOG(ERROR) << "Fail to get hard limit of rlimit " << i; |
| 192 | return false; |
| 193 | } |
| 194 | if (!ParseUint32FromDict(*rlimits_dict, "soft", &limit.soft)) { |
| 195 | LOG(ERROR) << "Fail to get soft limit of rlimit " << i; |
| 196 | return false; |
| 197 | } |
| 198 | rlimits_out->push_back(limit); |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 204 | // Fills |config_out| with information about the main process to run in the |
| 205 | // container and the user it should be run as. |
| 206 | bool ParseProcessConfig(const base::DictionaryValue& config_root_dict, |
| 207 | OciConfigPtr const& config_out) { |
| 208 | // |process_dict| stays owned by |config_root_dict| |
| 209 | const base::DictionaryValue* process_dict = nullptr; |
| 210 | if (!config_root_dict.GetDictionary("process", &process_dict)) { |
| 211 | LOG(ERROR) << "Fail to get main process from config"; |
| 212 | return false; |
| 213 | } |
| 214 | process_dict->GetBoolean("terminal", &config_out->process.terminal); |
| 215 | // |user_dict| stays owned by |process_dict| |
| 216 | const base::DictionaryValue* user_dict = nullptr; |
| 217 | if (!process_dict->GetDictionary("user", &user_dict)) { |
| 218 | LOG(ERROR) << "Failed to get user info from config"; |
| 219 | return false; |
| 220 | } |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 221 | if (!ParseUint32FromDict(*user_dict, "uid", &config_out->process.user.uid)) |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 222 | return false; |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 223 | if (!ParseUint32FromDict(*user_dict, "gid", &config_out->process.user.gid)) |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 224 | return false; |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 225 | // |args_list| stays owned by |process_dict| |
| 226 | const base::ListValue* args_list = nullptr; |
| 227 | if (!process_dict->GetList("args", &args_list)) { |
| 228 | LOG(ERROR) << "Fail to get main process args from config"; |
| 229 | return false; |
| 230 | } |
| 231 | size_t num_args = args_list->GetSize(); |
| 232 | for (size_t i = 0; i < num_args; ++i) { |
| 233 | std::string arg; |
| 234 | if (!args_list->GetString(i, &arg)) { |
| 235 | LOG(ERROR) << "Fail to get process args from config"; |
| 236 | return false; |
| 237 | } |
| 238 | config_out->process.args.push_back(arg); |
| 239 | } |
| 240 | // |env_list| stays owned by |process_dict| |
| 241 | const base::ListValue* env_list = nullptr; |
| 242 | if (process_dict->GetList("env", &env_list)) { |
| 243 | size_t num_env = env_list->GetSize(); |
| 244 | for (size_t i = 0; i < num_env; ++i) { |
| 245 | std::string env; |
| 246 | if (!env_list->GetString(i, &env)) { |
| 247 | LOG(ERROR) << "Fail to get process env from config"; |
| 248 | return false; |
| 249 | } |
| 250 | config_out->process.env.push_back(env); |
| 251 | } |
| 252 | } |
| 253 | if (!process_dict->GetString("cwd", &config_out->process.cwd)) { |
| 254 | LOG(ERROR) << "failed to get cwd of process"; |
| 255 | return false; |
| 256 | } |
Luis Hector Chavez | 15e8e67 | 2017-07-20 15:13:27 -0700 | [diff] [blame] | 257 | // selinuxLabel is optional. |
| 258 | process_dict->GetString("selinuxLabel", &config_out->process.selinuxLabel); |
Luis Hector Chavez | 8e4dcc1 | 2017-06-27 12:54:47 -0700 | [diff] [blame] | 259 | // |capabilities_dict| stays owned by |process_dict| |
| 260 | const base::DictionaryValue* capabilities_dict = nullptr; |
| 261 | if (process_dict->GetDictionary("capabilities", &capabilities_dict)) { |
| 262 | if (!ParseCapabilitiesConfig(*capabilities_dict, |
| 263 | &config_out->process.capabilities)) { |
| 264 | return false; |
| 265 | } |
| 266 | } |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 267 | |
Dylan Reid | 93fa460 | 2017-06-06 13:39:31 -0700 | [diff] [blame] | 268 | // |rlimit_list| stays owned by |process_dict| |
| 269 | const base::ListValue* rlimits_list = nullptr; |
| 270 | if (process_dict->GetList("rlimits", &rlimits_list)) { |
| 271 | if (!ParseRlimitsConfig(*rlimits_list, &config_out->process.rlimits)) { |
| 272 | return false; |
| 273 | } |
| 274 | } |
| 275 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
| 279 | // Parses the 'mounts' field. The necessary mounts for running the container |
| 280 | // are specified here. |
| 281 | bool ParseMounts(const base::DictionaryValue& config_root_dict, |
| 282 | OciConfigPtr const& config_out) { |
| 283 | // |config_mounts_list| stays owned by |config_root_dict| |
| 284 | const base::ListValue* config_mounts_list = nullptr; |
| 285 | if (!config_root_dict.GetList("mounts", &config_mounts_list)) { |
| 286 | LOG(ERROR) << "Fail to get mounts from config dictionary"; |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | for (size_t i = 0; i < config_mounts_list->GetSize(); ++i) { |
| 291 | const base::DictionaryValue* mount_dict; |
| 292 | if (!config_mounts_list->GetDictionary(i, &mount_dict)) { |
| 293 | LOG(ERROR) << "Fail to get mount item " << i; |
| 294 | return false; |
| 295 | } |
| 296 | OciMount mount; |
Luis Hector Chavez | 88ce939 | 2017-09-01 09:53:13 -0700 | [diff] [blame] | 297 | std::string value; |
| 298 | if (!mount_dict->GetString("destination", &value)) { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 299 | LOG(ERROR) << "Fail to get mount path for mount " << i; |
| 300 | return false; |
| 301 | } |
Luis Hector Chavez | 88ce939 | 2017-09-01 09:53:13 -0700 | [diff] [blame] | 302 | mount.destination = base::FilePath(value); |
Dylan Reid | 6d65074 | 2016-11-02 23:10:38 -0700 | [diff] [blame] | 303 | if (!mount_dict->GetString("type", &mount.type)) { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 304 | LOG(ERROR) << "Fail to get mount type for mount " << i; |
| 305 | return false; |
| 306 | } |
Luis Hector Chavez | 88ce939 | 2017-09-01 09:53:13 -0700 | [diff] [blame] | 307 | if (!mount_dict->GetString("source", &value)) { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 308 | LOG(ERROR) << "Fail to get mount source for mount " << i; |
| 309 | return false; |
| 310 | } |
Luis Hector Chavez | 88ce939 | 2017-09-01 09:53:13 -0700 | [diff] [blame] | 311 | mount.source = base::FilePath(value); |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 312 | |
| 313 | // |options| are owned by |mount_dict| |
| 314 | const base::ListValue* options = nullptr; |
| 315 | if (mount_dict->GetList("options", &options)) { |
| 316 | for (size_t j = 0; j < options->GetSize(); ++j) { |
| 317 | std::string this_opt; |
| 318 | if (!options->GetString(j, &this_opt)) { |
| 319 | LOG(ERROR) << "Fail to get option " << j << " from mount options"; |
| 320 | return false; |
| 321 | } |
| 322 | mount.options.push_back(this_opt); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | config_out->mounts.push_back(mount); |
| 327 | } |
| 328 | return true; |
| 329 | } |
| 330 | |
Dylan Reid | 6985e3b | 2017-03-31 19:39:16 -0700 | [diff] [blame] | 331 | // Parses the linux resource list |
| 332 | bool ParseResources(const base::DictionaryValue& resources_dict, |
| 333 | OciLinuxResources* resources_out) { |
| 334 | // |device_list| is owned by |resources_dict| |
| 335 | const base::ListValue* device_list = nullptr; |
| 336 | if (!resources_dict.GetList("devices", &device_list)) { |
| 337 | // The device list is optional. |
| 338 | return true; |
| 339 | } |
| 340 | size_t num_devices = device_list->GetSize(); |
| 341 | for (size_t i = 0; i < num_devices; ++i) { |
| 342 | OciLinuxCgroupDevice device; |
| 343 | |
| 344 | const base::DictionaryValue* dev; |
| 345 | if (!device_list->GetDictionary(i, &dev)) { |
| 346 | LOG(ERROR) << "Fail to get device " << i; |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | if (!dev->GetBoolean("allow", &device.allow)) { |
| 351 | LOG(ERROR) << "Fail to get allow value for device " << i; |
| 352 | return false; |
| 353 | } |
| 354 | if (!dev->GetString("access", &device.access)) |
| 355 | device.access = "rwm"; // Optional, default to all perms. |
| 356 | if (!dev->GetString("type", &device.type)) |
| 357 | device.type = "a"; // Optional, default to both a means all. |
| 358 | if (!ParseUint32FromDict(*dev, "major", &device.major)) |
| 359 | device.major = -1; // Optional, -1 will map to all devices. |
| 360 | if (!ParseUint32FromDict(*dev, "minor", &device.minor)) |
| 361 | device.minor = -1; // Optional, -1 will map to all devices. |
| 362 | |
| 363 | resources_out->devices.push_back(device); |
| 364 | } |
| 365 | |
| 366 | return true; |
| 367 | } |
| 368 | |
Stephen Barber | 3c0a202 | 2017-09-08 14:17:57 -0700 | [diff] [blame] | 369 | // Parses the list of namespaces and fills |namespaces_out| with them. |
| 370 | bool ParseNamespaces(const base::ListValue* namespaces_list, |
| 371 | std::vector<OciNamespace>* namespaces_out) { |
| 372 | for (size_t i = 0; i < namespaces_list->GetSize(); ++i) { |
| 373 | OciNamespace new_namespace; |
| 374 | const base::DictionaryValue* ns; |
| 375 | if (!namespaces_list->GetDictionary(i, &ns)) { |
| 376 | LOG(ERROR) << "Failed to get namespace " << i; |
| 377 | return false; |
| 378 | } |
| 379 | if (!ns->GetString("type", &new_namespace.type)) { |
| 380 | LOG(ERROR) << "Namespace " << i << " missing type"; |
| 381 | return false; |
| 382 | } |
| 383 | ns->GetString("path", &new_namespace.path); |
| 384 | namespaces_out->push_back(new_namespace); |
| 385 | } |
| 386 | return true; |
| 387 | } |
| 388 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 389 | // Parse the list of device nodes that the container needs to run. |
| 390 | bool ParseDeviceList(const base::DictionaryValue& linux_dict, |
| 391 | OciConfigPtr const& config_out) { |
| 392 | // |device_list| is owned by |linux_dict| |
| 393 | const base::ListValue* device_list = nullptr; |
| 394 | if (!linux_dict.GetList("devices", &device_list)) { |
Dylan Reid | a5ed127 | 2016-11-11 16:43:39 -0800 | [diff] [blame] | 395 | // The device list is optional. |
| 396 | return true; |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 397 | } |
| 398 | size_t num_devices = device_list->GetSize(); |
| 399 | for (size_t i = 0; i < num_devices; ++i) { |
| 400 | OciLinuxDevice device; |
| 401 | |
| 402 | const base::DictionaryValue* dev; |
| 403 | if (!device_list->GetDictionary(i, &dev)) { |
| 404 | LOG(ERROR) << "Fail to get device " << i; |
| 405 | return false; |
| 406 | } |
| 407 | std::string path; |
| 408 | if (!dev->GetString("path", &device.path)) { |
| 409 | LOG(ERROR) << "Fail to get path for dev"; |
| 410 | return false; |
| 411 | } |
Dylan Reid | 6d65074 | 2016-11-02 23:10:38 -0700 | [diff] [blame] | 412 | if (!dev->GetString("type", &device.type)) { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 413 | LOG(ERROR) << "Fail to get type for " << device.path; |
| 414 | return false; |
| 415 | } |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 416 | if (!ParseUint32FromDict(*dev, "major", &device.major)) |
| 417 | return false; |
| 418 | if (!ParseUint32FromDict(*dev, "minor", &device.minor)) |
| 419 | return false; |
| 420 | if (!ParseUint32FromDict(*dev, "fileMode", &device.fileMode)) |
| 421 | return false; |
| 422 | if (!ParseUint32FromDict(*dev, "uid", &device.uid)) |
| 423 | return false; |
| 424 | if (!ParseUint32FromDict(*dev, "gid", &device.gid)) |
| 425 | return false; |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 426 | |
| 427 | config_out->linux_config.devices.push_back(device); |
| 428 | } |
| 429 | |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | // Parses the list of ID mappings and fills |mappings_out| with them. |
| 434 | bool ParseLinuxIdMappings(const base::ListValue* id_map_list, |
Ben Chan | f43980b | 2017-03-10 11:31:46 -0800 | [diff] [blame] | 435 | std::vector<OciLinuxNamespaceMapping>* mappings_out) { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 436 | for (size_t i = 0; i < id_map_list->GetSize(); ++i) { |
| 437 | OciLinuxNamespaceMapping new_map; |
| 438 | const base::DictionaryValue* map; |
| 439 | if (!id_map_list->GetDictionary(i, &map)) { |
| 440 | LOG(ERROR) << "Fail to get id map " << i; |
| 441 | return false; |
| 442 | } |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 443 | if (!ParseUint32FromDict(*map, "hostID", &new_map.hostID)) |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 444 | return false; |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 445 | if (!ParseUint32FromDict(*map, "containerID", &new_map.containerID)) |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 446 | return false; |
Dylan Reid | c0c2850 | 2016-11-04 10:51:30 -0700 | [diff] [blame] | 447 | if (!ParseUint32FromDict(*map, "size", &new_map.size)) |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 448 | return false; |
Ben Chan | f43980b | 2017-03-10 11:31:46 -0800 | [diff] [blame] | 449 | mappings_out->push_back(new_map); |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | |
Dylan Reid | b0a7277 | 2016-11-03 16:27:50 +0000 | [diff] [blame] | 454 | // Parses seccomp syscall args. |
| 455 | bool ParseSeccompArgs(const base::DictionaryValue& syscall_dict, |
| 456 | OciSeccompSyscall* syscall_out) { |
| 457 | const base::ListValue* args = nullptr; |
| 458 | if (syscall_dict.GetList("args", &args)) { |
| 459 | for (size_t i = 0; i < args->GetSize(); ++i) { |
| 460 | const base::DictionaryValue* args_dict = nullptr; |
| 461 | if (!args->GetDictionary(i, &args_dict)) { |
| 462 | LOG(ERROR) << "Failed to pars args dict for " << syscall_out->name; |
| 463 | return false; |
| 464 | } |
| 465 | OciSeccompArg this_arg; |
| 466 | if (!ParseUint32FromDict(*args_dict, "index", &this_arg.index)) |
| 467 | return false; |
| 468 | if (!ParseUint64FromDict(*args_dict, "value", &this_arg.value)) |
| 469 | return false; |
| 470 | if (!ParseUint64FromDict(*args_dict, "value2", &this_arg.value2)) |
| 471 | return false; |
| 472 | if (!args_dict->GetString("op", &this_arg.op)) { |
| 473 | LOG(ERROR) << "Failed to parse op for arg " << this_arg.index |
| 474 | << " of " << syscall_out->name; |
| 475 | return false; |
| 476 | } |
| 477 | syscall_out->args.push_back(this_arg); |
| 478 | } |
| 479 | } |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | // Parses the seccomp node if it is present. |
| 484 | bool ParseSeccompInfo(const base::DictionaryValue& seccomp_dict, |
| 485 | OciSeccomp* seccomp_out) { |
| 486 | if (!seccomp_dict.GetString("defaultAction", |
| 487 | &seccomp_out->defaultAction)) |
| 488 | return false; |
| 489 | |
| 490 | // Gets the list of architectures. |
| 491 | const base::ListValue* architectures = nullptr; |
| 492 | if (!seccomp_dict.GetList("architectures", &architectures)) { |
| 493 | LOG(ERROR) << "Fail to read seccomp architectures"; |
| 494 | return false; |
| 495 | } |
| 496 | for (size_t i = 0; i < architectures->GetSize(); ++i) { |
| 497 | std::string this_arch; |
| 498 | if (!architectures->GetString(i, &this_arch)) { |
| 499 | LOG(ERROR) << "Fail to parse seccomp architecture list"; |
| 500 | return false; |
| 501 | } |
| 502 | seccomp_out->architectures.push_back(this_arch); |
| 503 | } |
| 504 | |
| 505 | // Gets the list of syscalls. |
| 506 | const base::ListValue* syscalls = nullptr; |
| 507 | if (!seccomp_dict.GetList("syscalls", &syscalls)) { |
| 508 | LOG(ERROR) << "Fail to read seccomp syscalls"; |
| 509 | return false; |
| 510 | } |
| 511 | for (size_t i = 0; i < syscalls->GetSize(); ++i) { |
| 512 | const base::DictionaryValue* syscall_dict = nullptr; |
| 513 | if (!syscalls->GetDictionary(i, &syscall_dict)) { |
| 514 | LOG(ERROR) << "Fail to parse seccomp syscalls list"; |
| 515 | return false; |
| 516 | } |
| 517 | OciSeccompSyscall this_syscall; |
| 518 | if (!syscall_dict->GetString("name", &this_syscall.name)) { |
| 519 | LOG(ERROR) << "Fail to parse syscall name " << i; |
| 520 | return false; |
| 521 | } |
| 522 | if (!syscall_dict->GetString("action", &this_syscall.action)) { |
| 523 | LOG(ERROR) << "Fail to parse syscall action for " << this_syscall.name; |
| 524 | return false; |
| 525 | } |
| 526 | if (!ParseSeccompArgs(*syscall_dict, &this_syscall)) |
| 527 | return false; |
| 528 | seccomp_out->syscalls.push_back(this_syscall); |
| 529 | } |
| 530 | |
| 531 | return true; |
| 532 | } |
| 533 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 534 | // Parses the linux node which has information about setting up a user |
| 535 | // namespace, and the list of devices for the container. |
| 536 | bool ParseLinuxConfigDict(const base::DictionaryValue& runtime_root_dict, |
| 537 | OciConfigPtr const& config_out) { |
| 538 | // |linux_dict| is owned by |runtime_root_dict| |
| 539 | const base::DictionaryValue* linux_dict = nullptr; |
| 540 | if (!runtime_root_dict.GetDictionary("linux", &linux_dict)) { |
| 541 | LOG(ERROR) << "Fail to get linux dictionary from the runtime dictionary"; |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | // |uid_map_list| is owned by |linux_dict| |
| 546 | const base::ListValue* uid_map_list = nullptr; |
| 547 | if (!linux_dict->GetList("uidMappings", &uid_map_list)) { |
| 548 | LOG(ERROR) << "Fail to get uid mappings list"; |
| 549 | return false; |
| 550 | } |
Ben Chan | f43980b | 2017-03-10 11:31:46 -0800 | [diff] [blame] | 551 | ParseLinuxIdMappings(uid_map_list, &config_out->linux_config.uidMappings); |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 552 | |
| 553 | // |gid_map_list| is owned by |linux_dict| |
| 554 | const base::ListValue* gid_map_list = nullptr; |
| 555 | if (!linux_dict->GetList("gidMappings", &gid_map_list)) { |
| 556 | LOG(ERROR) << "Fail to get gid mappings list"; |
| 557 | return false; |
| 558 | } |
Ben Chan | f43980b | 2017-03-10 11:31:46 -0800 | [diff] [blame] | 559 | ParseLinuxIdMappings(gid_map_list, &config_out->linux_config.gidMappings); |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 560 | |
| 561 | if (!ParseDeviceList(*linux_dict, config_out)) |
| 562 | return false; |
| 563 | |
Dylan Reid | 6985e3b | 2017-03-31 19:39:16 -0700 | [diff] [blame] | 564 | const base::DictionaryValue* resources_dict = nullptr; |
| 565 | if (linux_dict->GetDictionary("resources", &resources_dict)) { |
| 566 | if (!ParseResources(*resources_dict, &config_out->linux_config.resources)) |
| 567 | return false; |
| 568 | } |
| 569 | |
Stephen Barber | 3c0a202 | 2017-09-08 14:17:57 -0700 | [diff] [blame] | 570 | const base::ListValue* namespaces_list = nullptr; |
| 571 | if (linux_dict->GetList("namespaces", &namespaces_list)) { |
| 572 | if (!ParseNamespaces(namespaces_list, &config_out->linux_config.namespaces)) |
| 573 | return false; |
| 574 | } |
| 575 | |
Dylan Reid | b0a7277 | 2016-11-03 16:27:50 +0000 | [diff] [blame] | 576 | const base::DictionaryValue* seccomp_dict = nullptr; |
| 577 | if (linux_dict->GetDictionary("seccomp", &seccomp_dict)) { |
| 578 | if (!ParseSeccompInfo(*seccomp_dict, &config_out->linux_config.seccomp)) |
| 579 | return false; |
| 580 | } |
| 581 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 582 | return true; |
| 583 | } |
| 584 | |
Dylan Reid | 45e34fe | 2016-12-02 15:11:53 -0800 | [diff] [blame] | 585 | bool HostnameValid(const std::string& hostname) { |
| 586 | if (hostname.length() > 255) |
| 587 | return false; |
| 588 | |
| 589 | const std::regex name("^[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?$"); |
| 590 | if (!std::regex_match(hostname, name)) |
| 591 | return false; |
| 592 | |
| 593 | const std::regex double_dash("--"); |
| 594 | if (std::regex_match(hostname, double_dash)) |
| 595 | return false; |
| 596 | |
| 597 | return true; |
| 598 | } |
| 599 | |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 600 | bool ParseHooksList(const base::ListValue& hooks_list, |
| 601 | std::vector<OciHook>* hooks_out, |
| 602 | const std::string& hook_type) { |
| 603 | size_t num_hooks = hooks_list.GetSize(); |
| 604 | for (size_t i = 0; i < num_hooks; ++i) { |
| 605 | OciHook hook; |
| 606 | const base::DictionaryValue* hook_dict; |
| 607 | if (!hooks_list.GetDictionary(i, &hook_dict)) { |
| 608 | LOG(ERROR) << "Fail to get " << hook_type << " hook item " << i; |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | if (!hook_dict->GetString("path", &hook.path)) { |
| 613 | LOG(ERROR) << "Fail to get path of " << hook_type << " hook " << i; |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | const base::ListValue* hook_args; |
| 618 | // args are optional. |
| 619 | if (hook_dict->GetList("args", &hook_args)) { |
| 620 | size_t num_args = hook_args->GetSize(); |
| 621 | for (size_t j = 0; j < num_args; ++j) { |
| 622 | std::string arg; |
| 623 | if (!hook_args->GetString(j, &arg)) { |
| 624 | LOG(ERROR) << "Fail to get arg " << j << " of " << hook_type |
| 625 | << " hook " << i; |
| 626 | return false; |
| 627 | } |
| 628 | hook.args.push_back(arg); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | const base::ListValue* hook_envs; |
| 633 | // envs are optional. |
| 634 | if (hook_dict->GetList("env", &hook_envs)) { |
| 635 | size_t num_env = hook_envs->GetSize(); |
| 636 | for (size_t j = 0; j < num_env; ++j) { |
| 637 | std::string env; |
| 638 | if (!hook_envs->GetString(j, &env)) { |
| 639 | LOG(ERROR) << "Fail to get env " << j << " of " << hook_type |
| 640 | << " hook " << i; |
| 641 | return false; |
| 642 | } |
| 643 | std::vector<std::string> kvp = base::SplitString( |
| 644 | env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 645 | if (kvp.size() != 2) { |
| 646 | LOG(ERROR) << "Fail to parse env \"" << env |
| 647 | << "\". Must be in name=value format."; |
| 648 | return false; |
| 649 | } |
| 650 | hook.env.insert(std::make_pair(kvp[0], kvp[1])); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | int timeout_seconds; |
| 655 | // timeout is optional. |
| 656 | if (hook_dict->GetInteger("timeout", &timeout_seconds)) { |
| 657 | hook.timeout = base::TimeDelta::FromSeconds(timeout_seconds); |
| 658 | } else { |
| 659 | hook.timeout = base::TimeDelta::Max(); |
| 660 | } |
| 661 | |
| 662 | hooks_out->emplace_back(std::move(hook)); |
| 663 | } |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | bool ParseHooks(const base::DictionaryValue& config_root_dict, |
| 668 | OciConfigPtr const& config_out) { |
| 669 | const base::DictionaryValue* hooks_config_dict; |
| 670 | if (!config_root_dict.GetDictionary("hooks", &hooks_config_dict)) { |
| 671 | // Hooks are optional. |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | const base::ListValue* hooks_list; |
| 676 | if (hooks_config_dict->GetList("prestart", &hooks_list)) { |
| 677 | if (!ParseHooksList(*hooks_list, &config_out->pre_start_hooks, "prestart")) |
| 678 | return false; |
| 679 | } |
| 680 | if (hooks_config_dict->GetList("poststart", &hooks_list)) { |
| 681 | if (!ParseHooksList( |
| 682 | *hooks_list, &config_out->post_start_hooks, "poststart")) |
| 683 | return false; |
| 684 | } |
| 685 | if (hooks_config_dict->GetList("poststop", &hooks_list)) { |
| 686 | if (!ParseHooksList(*hooks_list, &config_out->post_stop_hooks, "poststop")) |
| 687 | return false; |
| 688 | } |
| 689 | return true; |
| 690 | } |
| 691 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 692 | // Parses the configuration file for the container. The config file specifies |
| 693 | // basic filesystem info and details about the process to be run. namespace, |
| 694 | // cgroup, and syscall configurations are also specified |
| 695 | bool ParseConfigDict(const base::DictionaryValue& config_root_dict, |
| 696 | OciConfigPtr const& config_out) { |
Dylan Reid | 6d65074 | 2016-11-02 23:10:38 -0700 | [diff] [blame] | 697 | if (!config_root_dict.GetString("ociVersion", &config_out->ociVersion)) { |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 698 | LOG(ERROR) << "Failed to parse ociVersion"; |
| 699 | return false; |
| 700 | } |
| 701 | if (!config_root_dict.GetString("hostname", &config_out->hostname)) { |
| 702 | LOG(ERROR) << "Failed to parse hostname"; |
| 703 | return false; |
| 704 | } |
Dylan Reid | 45e34fe | 2016-12-02 15:11:53 -0800 | [diff] [blame] | 705 | if (!HostnameValid(config_out->hostname)) { |
| 706 | LOG(ERROR) << "Invalid hostname " << config_out->hostname; |
| 707 | return false; |
| 708 | } |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 709 | |
| 710 | // Platform info |
| 711 | if (!ParsePlatformConfig(config_root_dict, config_out)) { |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | // Root fs info |
| 716 | if (!ParseRootFileSystemConfig(config_root_dict, config_out)) { |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | // Process info |
| 721 | if (!ParseProcessConfig(config_root_dict, config_out)) { |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | // Get a list of mount points and mounts. |
| 726 | if (!ParseMounts(config_root_dict, config_out)) { |
| 727 | LOG(ERROR) << "Failed to parse mounts"; |
| 728 | return false; |
| 729 | } |
| 730 | |
Luis Hector Chavez | f8e8f4c | 2017-08-01 01:09:39 -0700 | [diff] [blame] | 731 | // Hooks info |
| 732 | if (!ParseHooks(config_root_dict, config_out)) { |
| 733 | return false; |
| 734 | } |
| 735 | |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 736 | // Parse linux node. |
| 737 | if (!ParseLinuxConfigDict(config_root_dict, config_out)) { |
| 738 | LOG(ERROR) << "Failed to parse the linux node"; |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | return true; |
| 743 | } |
| 744 | |
Ben Chan | f43980b | 2017-03-10 11:31:46 -0800 | [diff] [blame] | 745 | } // anonymous namespace |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 746 | |
| 747 | bool ParseContainerConfig(const std::string& config_json_data, |
| 748 | OciConfigPtr const& config_out) { |
Luis Hector Chavez | d63f1ba | 2017-06-28 15:58:12 -0700 | [diff] [blame] | 749 | std::string error_msg; |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 750 | std::unique_ptr<const base::Value> config_root_val = |
Luis Hector Chavez | d63f1ba | 2017-06-28 15:58:12 -0700 | [diff] [blame] | 751 | base::JSONReader::ReadAndReturnError(config_json_data, |
| 752 | base::JSON_PARSE_RFC, |
| 753 | nullptr /* error_code_out */, |
| 754 | &error_msg, |
| 755 | nullptr /* error_line_out */, |
| 756 | nullptr /* error_column_out */); |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 757 | if (!config_root_val) { |
Luis Hector Chavez | d63f1ba | 2017-06-28 15:58:12 -0700 | [diff] [blame] | 758 | LOG(ERROR) << "Fail to parse config.json: " << error_msg; |
Dylan Reid | 6b590e6 | 2016-10-27 19:10:53 -0700 | [diff] [blame] | 759 | return false; |
| 760 | } |
| 761 | const base::DictionaryValue* config_dict = nullptr; |
| 762 | if (!config_root_val->GetAsDictionary(&config_dict)) { |
| 763 | LOG(ERROR) << "Fail to parse root dictionary from config.json"; |
| 764 | return false; |
| 765 | } |
| 766 | if (!ParseConfigDict(*config_dict, config_out)) { |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | return true; |
| 771 | } |
| 772 | |
Stephen Barber | 5f6dc9b | 2017-04-04 12:36:32 -0700 | [diff] [blame] | 773 | } // namespace run_oci |