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