blob: 968614ae27b75a3a63cae45b53989a1b76246cd7 [file] [log] [blame]
Dylan Reid6b590e62016-10-27 19:10:53 -07001// 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 Barber5f6dc9b2017-04-04 12:36:32 -07005#include "run_oci/container_config_parser.h"
Dylan Reid6b590e62016-10-27 19:10:53 -07006
Luis Hector Chavez6e9a5332017-10-26 14:41:49 -07007#include <linux/securebits.h>
Luis Hector Chavez8373b412017-07-10 12:51:07 -07008#include <sys/capability.h>
Luis Hector Chavez7c6fddf2017-10-24 15:39:41 -07009#include <sys/mount.h>
Dylan Reid93fa4602017-06-06 13:39:31 -070010#include <sys/resource.h>
Dylan Reid6b590e62016-10-27 19:10:53 -070011#include <unistd.h>
12
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070013#include <map>
Ben Chanf43980b2017-03-10 11:31:46 -080014#include <regex> // NOLINT(build/c++11)
Dylan Reid6b590e62016-10-27 19:10:53 -070015#include <string>
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -070016#include <utility>
Dylan Reid6b590e62016-10-27 19:10:53 -070017#include <vector>
18
19#include <base/json/json_reader.h>
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -070020#include <base/strings/string_split.h>
Dylan Reid6b590e62016-10-27 19:10:53 -070021#include <base/values.h>
22
Stephen Barber5f6dc9b2017-04-04 12:36:32 -070023namespace run_oci {
Dylan Reid6b590e62016-10-27 19:10:53 -070024
25namespace {
26
yusukesfc412ea2018-01-10 08:27:24 -080027// Gets an integer from the given dictionary.
28template <typename T>
29bool ParseIntFromDict(const base::DictionaryValue& dict,
30 const char* name,
31 T* val_out) {
Dylan Reidb0a72772016-11-03 16:27:50 +000032 double double_val;
33 if (!dict.GetDouble(name, &double_val)) {
Dylan Reidb0a72772016-11-03 16:27:50 +000034 return false;
35 }
Luis Hector Chavezda352462018-01-30 09:10:00 -080036 *val_out = static_cast<T>(double_val);
Dylan Reidb0a72772016-11-03 16:27:50 +000037 return true;
38}
39
Risanfd41aee2018-08-15 14:03:38 +090040// Parse a ListValue structure as vector of integers.
41template <typename T>
42bool 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 Reid6b590e62016-10-27 19:10:53 -070053// Parses basic platform configuration.
54bool 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.
75bool 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 Chavez2a90f292017-10-02 09:51:28 -070083 std::string path;
84 if (!rootfs_dict->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -070085 LOG(ERROR) << "Fail to get rootfs path from config";
86 return false;
87 }
Luis Hector Chavez2a90f292017-10-02 09:51:28 -070088 config_out->root.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -070089 rootfs_dict->GetBoolean("readonly", &config_out->root.readonly);
Dylan Reid6b590e62016-10-27 19:10:53 -070090 return true;
91}
92
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070093// Fills |config_out| with information about the capability sets in the
94// container.
95bool 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 Chavez8373b412017-07-10 12:51:07 -0700108 cap_value_t cap_value;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700109 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 Chavez8373b412017-07-10 12:51:07 -0700116 if (cap_from_name(cap_name.c_str(), &cap_value) == -1) {
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700117 LOG(ERROR) << "Unrecognized capability name: " << cap_name;
118 return false;
119 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700120 caps[cap_value] = true;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700121 }
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 Reid93fa4602017-06-06 13:39:31 -0700149const std::map<std::string, int> kRlimitMap = {
150#define RLIMIT_MAP_ENTRY(limit) \
151 { "RLIMIT_" #limit, RLIMIT_##limit }
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700152 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 Reid93fa4602017-06-06 13:39:31 -0700160#undef RLIMIT_MAP_ENTRY
161};
162
163// Fills |config_out| with information about the capability sets in the
164// container.
165bool 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;
yusukesfc412ea2018-01-10 08:27:24 -0800188 if (!ParseIntFromDict(*rlimits_dict, "hard", &limit.hard)) {
Dylan Reid93fa4602017-06-06 13:39:31 -0700189 LOG(ERROR) << "Fail to get hard limit of rlimit " << i;
190 return false;
191 }
yusukesfc412ea2018-01-10 08:27:24 -0800192 if (!ParseIntFromDict(*rlimits_dict, "soft", &limit.soft)) {
Dylan Reid93fa4602017-06-06 13:39:31 -0700193 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 Reid6b590e62016-10-27 19:10:53 -0700202// Fills |config_out| with information about the main process to run in the
203// container and the user it should be run as.
204bool 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 }
yusukesfc412ea2018-01-10 08:27:24 -0800219 if (!ParseIntFromDict(*user_dict, "uid", &config_out->process.user.uid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700220 return false;
yusukesfc412ea2018-01-10 08:27:24 -0800221 if (!ParseIntFromDict(*user_dict, "gid", &config_out->process.user.gid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700222 return false;
Risanfd41aee2018-08-15 14:03:38 +0900223
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 Reid6b590e62016-10-27 19:10:53 -0700232 // |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 Chavez855e99e2017-10-10 10:27:33 -0700257 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 Reid6b590e62016-10-27 19:10:53 -0700265 }
266 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700267 std::string path;
268 if (!process_dict->GetString("cwd", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700269 LOG(ERROR) << "failed to get cwd of process";
270 return false;
271 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700272 config_out->process.cwd = base::FilePath(path);
Luis Hector Chavezce1b8282017-10-30 10:12:49 -0700273 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 Chavez855e99e2017-10-10 10:27:33 -0700278
Luis Hector Chavez15e8e672017-07-20 15:13:27 -0700279 // selinuxLabel is optional.
280 process_dict->GetString("selinuxLabel", &config_out->process.selinuxLabel);
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700281 // |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 Reid6b590e62016-10-27 19:10:53 -0700289
Dylan Reid93fa4602017-06-06 13:39:31 -0700290 // |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 Reid6b590e62016-10-27 19:10:53 -0700298 return true;
299}
300
301// Parses the 'mounts' field. The necessary mounts for running the container
302// are specified here.
303bool 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 Chavez855e99e2017-10-10 10:27:33 -0700319 std::string path;
320 if (!mount_dict->GetString("destination", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700321 LOG(ERROR) << "Fail to get mount path for mount " << i;
322 return false;
323 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700324 mount.destination = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700325 if (!mount_dict->GetString("type", &mount.type)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700326 LOG(ERROR) << "Fail to get mount type for mount " << i;
327 return false;
328 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700329 if (!mount_dict->GetString("source", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700330 LOG(ERROR) << "Fail to get mount source for mount " << i;
331 return false;
332 }
Luis Hector Chavez60f9bb12017-10-16 11:33:01 -0700333 if (!mount_dict->GetBoolean("performInIntermediateNamespace",
Luis Hector Chaveze2b8c5e2017-10-24 13:10:26 -0700334 &mount.performInIntermediateNamespace)) {
335 mount.performInIntermediateNamespace = false; // Optional
Luis Hector Chavez60f9bb12017-10-16 11:33:01 -0700336 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700337 mount.source = base::FilePath(path);
Dylan Reid6b590e62016-10-27 19:10:53 -0700338
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 Reid6985e3b2017-03-31 19:39:16 -0700357// Parses the linux resource list
358bool 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.
yusukesfc412ea2018-01-10 08:27:24 -0800384 if (!ParseIntFromDict(*dev, "major", &device.major))
Dylan Reid6985e3b2017-03-31 19:39:16 -0700385 device.major = -1; // Optional, -1 will map to all devices.
yusukesfc412ea2018-01-10 08:27:24 -0800386 if (!ParseIntFromDict(*dev, "minor", &device.minor))
Dylan Reid6985e3b2017-03-31 19:39:16 -0700387 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 Barber3c0a2022017-09-08 14:17:57 -0700395// Parses the list of namespaces and fills |namespaces_out| with them.
396bool 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 Chavez855e99e2017-10-10 10:27:33 -0700409 std::string path;
410 if (ns->GetString("path", &path))
411 new_namespace.path = base::FilePath(path);
Stephen Barber3c0a2022017-09-08 14:17:57 -0700412 namespaces_out->push_back(new_namespace);
413 }
414 return true;
415}
416
Dylan Reid6b590e62016-10-27 19:10:53 -0700417// Parse the list of device nodes that the container needs to run.
418bool 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 Reida5ed1272016-11-11 16:43:39 -0800423 // The device list is optional.
424 return true;
Dylan Reid6b590e62016-10-27 19:10:53 -0700425 }
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 Chavez855e99e2017-10-10 10:27:33 -0700436 if (!dev->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700437 LOG(ERROR) << "Fail to get path for dev";
438 return false;
439 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700440 device.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700441 if (!dev->GetString("type", &device.type)) {
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700442 LOG(ERROR) << "Fail to get type for " << device.path.value();
Dylan Reid6b590e62016-10-27 19:10:53 -0700443 return false;
444 }
Stephen Barber7bae6642017-11-30 10:47:12 -0800445 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 {
yusukesfc412ea2018-01-10 08:27:24 -0800453 if (!ParseIntFromDict(*dev, "major", &device.major))
Stephen Barber7bae6642017-11-30 10:47:12 -0800454 return false;
455 }
456
Luis Hector Chavezac20fc52017-10-10 11:08:41 -0700457 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 {
yusukesfc412ea2018-01-10 08:27:24 -0800465 if (!ParseIntFromDict(*dev, "minor", &device.minor))
Luis Hector Chavezac20fc52017-10-10 11:08:41 -0700466 return false;
467 }
yusukesfc412ea2018-01-10 08:27:24 -0800468 if (!ParseIntFromDict(*dev, "fileMode", &device.fileMode))
Dylan Reidc0c28502016-11-04 10:51:30 -0700469 return false;
yusukesfc412ea2018-01-10 08:27:24 -0800470 if (!ParseIntFromDict(*dev, "uid", &device.uid))
Dylan Reidc0c28502016-11-04 10:51:30 -0700471 return false;
yusukesfc412ea2018-01-10 08:27:24 -0800472 if (!ParseIntFromDict(*dev, "gid", &device.gid))
Dylan Reidc0c28502016-11-04 10:51:30 -0700473 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700474
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.
482bool ParseLinuxIdMappings(const base::ListValue* id_map_list,
Ben Chanf43980b2017-03-10 11:31:46 -0800483 std::vector<OciLinuxNamespaceMapping>* mappings_out) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700484 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 }
yusukesfc412ea2018-01-10 08:27:24 -0800491 if (!ParseIntFromDict(*map, "hostID", &new_map.hostID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700492 return false;
yusukesfc412ea2018-01-10 08:27:24 -0800493 if (!ParseIntFromDict(*map, "containerID", &new_map.containerID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700494 return false;
yusukesfc412ea2018-01-10 08:27:24 -0800495 if (!ParseIntFromDict(*map, "size", &new_map.size))
Dylan Reid6b590e62016-10-27 19:10:53 -0700496 return false;
Ben Chanf43980b2017-03-10 11:31:46 -0800497 mappings_out->push_back(new_map);
Dylan Reid6b590e62016-10-27 19:10:53 -0700498 }
499 return true;
500}
501
Dylan Reidb0a72772016-11-03 16:27:50 +0000502// Parses seccomp syscall args.
503bool 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;
yusukesfc412ea2018-01-10 08:27:24 -0800514 if (!ParseIntFromDict(*args_dict, "index", &this_arg.index))
Dylan Reidb0a72772016-11-03 16:27:50 +0000515 return false;
yusukesfc412ea2018-01-10 08:27:24 -0800516 if (!ParseIntFromDict(*args_dict, "value", &this_arg.value))
Dylan Reidb0a72772016-11-03 16:27:50 +0000517 return false;
yusukesfc412ea2018-01-10 08:27:24 -0800518 if (!ParseIntFromDict(*args_dict, "value2", &this_arg.value2))
Dylan Reidb0a72772016-11-03 16:27:50 +0000519 return false;
520 if (!args_dict->GetString("op", &this_arg.op)) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700521 LOG(ERROR) << "Failed to parse op for arg " << this_arg.index << " of "
522 << syscall_out->name;
Dylan Reidb0a72772016-11-03 16:27:50 +0000523 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.
532bool ParseSeccompInfo(const base::DictionaryValue& seccomp_dict,
533 OciSeccomp* seccomp_out) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700534 if (!seccomp_dict.GetString("defaultAction", &seccomp_out->defaultAction))
Dylan Reidb0a72772016-11-03 16:27:50 +0000535 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 Chavez7c6fddf2017-10-24 15:39:41 -0700581constexpr 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
588bool 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 Chavez6e9a5332017-10-26 14:41:49 -0700600constexpr 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
615bool 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
626bool 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
yusukesd9598352018-01-09 17:40:33 -0800643// Parses the cpu node if it is present.
644bool 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 Reid6b590e62016-10-27 19:10:53 -0700653// Parses the linux node which has information about setting up a user
654// namespace, and the list of devices for the container.
655bool 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 Barber771653f2017-10-04 23:48:57 -0700666 if (linux_dict->GetList("uidMappings", &uid_map_list))
667 ParseLinuxIdMappings(uid_map_list, &config_out->linux_config.uidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700668
669 // |gid_map_list| is owned by |linux_dict|
670 const base::ListValue* gid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700671 if (linux_dict->GetList("gidMappings", &gid_map_list))
672 ParseLinuxIdMappings(gid_map_list, &config_out->linux_config.gidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700673
674 if (!ParseDeviceList(*linux_dict, config_out))
675 return false;
676
Dylan Reid6985e3b2017-03-31 19:39:16 -0700677 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 Barber3c0a2022017-09-08 14:17:57 -0700683 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 Reidb0a72772016-11-03 16:27:50 +0000689 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 Chavez7c6fddf2017-10-24 15:39:41 -0700695 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 Chavez45ac1242017-10-26 13:21:16 -0700704 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 Chavez0f3d7a42017-10-26 10:48:30 -0700708 if (!linux_dict->GetString("altSyscall",
709 &config_out->linux_config.altSyscall)) {
710 config_out->linux_config.altSyscall = std::string(); // Optional
711 }
712
Luis Hector Chavez6e9a5332017-10-26 14:41:49 -0700713 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
yusukesd9598352018-01-09 17:40:33 -0800723 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 Reid6b590e62016-10-27 19:10:53 -0700729 return true;
730}
731
Dylan Reid45e34fe2016-12-02 15:11:53 -0800732bool 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 Chavezf8e8f4c2017-08-01 01:09:39 -0700747bool 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 Chavez855e99e2017-10-10 10:27:33 -0700759 std::string path;
760 if (!hook_dict->GetString("path", &path)) {
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700761 LOG(ERROR) << "Fail to get path of " << hook_type << " hook " << i;
762 return false;
763 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700764 hook.path = base::FilePath(path);
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700765
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
816bool 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;
yusukesa7b5e942018-04-10 13:48:35 -0700825 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 Chavezbb515a02017-09-29 15:44:35 -0700831 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 Chavezf8e8f4c2017-08-01 01:09:39 -0700837 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 Chavez9f9f66e2017-10-16 14:54:32 -0700842 if (!ParseHooksList(*hooks_list, &config_out->post_start_hooks,
843 "poststart"))
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700844 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 Reid6b590e62016-10-27 19:10:53 -0700853// 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
856bool ParseConfigDict(const base::DictionaryValue& config_root_dict,
857 OciConfigPtr const& config_out) {
Dylan Reid6d650742016-11-02 23:10:38 -0700858 if (!config_root_dict.GetString("ociVersion", &config_out->ociVersion)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700859 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 Reid45e34fe2016-12-02 15:11:53 -0800866 if (!HostnameValid(config_out->hostname)) {
867 LOG(ERROR) << "Invalid hostname " << config_out->hostname;
868 return false;
869 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700870
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 Chavezf8e8f4c2017-08-01 01:09:39 -0700892 // Hooks info
893 if (!ParseHooks(config_root_dict, config_out)) {
894 return false;
895 }
896
Dylan Reid6b590e62016-10-27 19:10:53 -0700897 // 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 Chanf43980b2017-03-10 11:31:46 -0800906} // anonymous namespace
Dylan Reid6b590e62016-10-27 19:10:53 -0700907
908bool ParseContainerConfig(const std::string& config_json_data,
909 OciConfigPtr const& config_out) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700910 std::string error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700911 std::unique_ptr<const base::Value> config_root_val =
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700912 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 Reid6b590e62016-10-27 19:10:53 -0700916 if (!config_root_val) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700917 LOG(ERROR) << "Fail to parse config.json: " << error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700918 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 Barber5f6dc9b2017-04-04 12:36:32 -0700932} // namespace run_oci