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