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