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