blob: b263b3751288772b96c7a340ee71c57ee8cf075a [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 Chavez8373b412017-07-10 12:51:07 -07007#include <sys/capability.h>
Dylan Reid93fa4602017-06-06 13:39:31 -07008#include <sys/resource.h>
Dylan Reid6b590e62016-10-27 19:10:53 -07009#include <unistd.h>
10
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070011#include <map>
Ben Chanf43980b2017-03-10 11:31:46 -080012#include <regex> // NOLINT(build/c++11)
Dylan Reid6b590e62016-10-27 19:10:53 -070013#include <string>
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -070014#include <utility>
Dylan Reid6b590e62016-10-27 19:10:53 -070015#include <vector>
16
17#include <base/json/json_reader.h>
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -070018#include <base/strings/string_split.h>
Dylan Reid6b590e62016-10-27 19:10:53 -070019#include <base/values.h>
20
Stephen Barber5f6dc9b2017-04-04 12:36:32 -070021namespace run_oci {
Dylan Reid6b590e62016-10-27 19:10:53 -070022
23namespace {
24
Dylan Reidc0c28502016-11-04 10:51:30 -070025// Gets a uint32 from the given dictionary.
26bool ParseUint32FromDict(const base::DictionaryValue& dict, const char *name,
27 uint32_t* val_out) {
28 double double_val;
29 if (!dict.GetDouble(name, &double_val)) {
Dylan Reidc0c28502016-11-04 10:51:30 -070030 return false;
31 }
32 *val_out = double_val;
33 return true;
34}
35
Dylan Reidb0a72772016-11-03 16:27:50 +000036// Gets a uint64 from the given dictionary.
37bool ParseUint64FromDict(const base::DictionaryValue& dict, const char *name,
38 uint64_t* val_out) {
39 double double_val;
40 if (!dict.GetDouble(name, &double_val)) {
Dylan Reidb0a72772016-11-03 16:27:50 +000041 return false;
42 }
43 *val_out = double_val;
44 return true;
45}
46
Dylan Reid6b590e62016-10-27 19:10:53 -070047// Parses basic platform configuration.
48bool ParsePlatformConfig(const base::DictionaryValue& config_root_dict,
49 OciConfigPtr const& config_out) {
50 // |platform_dict| stays owned by |config_root_dict|
51 const base::DictionaryValue* platform_dict = nullptr;
52 if (!config_root_dict.GetDictionary("platform", &platform_dict)) {
53 LOG(ERROR) << "Fail to parse platform dictionary from config";
54 return false;
55 }
56
57 if (!platform_dict->GetString("os", &config_out->platform.os)) {
58 return false;
59 }
60
61 if (!platform_dict->GetString("arch", &config_out->platform.arch)) {
62 return false;
63 }
64
65 return true;
66}
67
68// Parses root fs info.
69bool ParseRootFileSystemConfig(const base::DictionaryValue& config_root_dict,
70 OciConfigPtr const& config_out) {
71 // |rootfs_dict| stays owned by |config_root_dict|
72 const base::DictionaryValue* rootfs_dict = nullptr;
73 if (!config_root_dict.GetDictionary("root", &rootfs_dict)) {
74 LOG(ERROR) << "Fail to parse rootfs dictionary from config";
75 return false;
76 }
Luis Hector Chavez2a90f292017-10-02 09:51:28 -070077 std::string path;
78 if (!rootfs_dict->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -070079 LOG(ERROR) << "Fail to get rootfs path from config";
80 return false;
81 }
Luis Hector Chavez2a90f292017-10-02 09:51:28 -070082 config_out->root.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -070083 rootfs_dict->GetBoolean("readonly", &config_out->root.readonly);
Dylan Reid6b590e62016-10-27 19:10:53 -070084 return true;
85}
86
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070087// Fills |config_out| with information about the capability sets in the
88// container.
89bool ParseCapabilitiesConfig(const base::DictionaryValue& capabilities_dict,
90 std::map<std::string, CapSet>* config_out) {
91 constexpr const char* kCapabilitySetNames[] = {
92 "effective", "bounding", "inheritable", "permitted", "ambient"};
93 const std::string kAmbientCapabilitySetName = "ambient";
94
95 CapSet caps_superset;
96 for (const char* set_name : kCapabilitySetNames) {
97 // |capset_list| stays owned by |capabilities_dict|.
98 const base::ListValue* capset_list = nullptr;
99 if (!capabilities_dict.GetList(set_name, &capset_list))
100 continue;
101 CapSet caps;
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700102 cap_value_t cap_value;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700103 for (const auto* cap_name_value : *capset_list) {
104 std::string cap_name;
105 if (!cap_name_value->GetAsString(&cap_name)) {
106 LOG(ERROR) << "Capability list " << set_name
107 << " contains a non-string";
108 return false;
109 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700110 if (cap_from_name(cap_name.c_str(), &cap_value) == -1) {
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700111 LOG(ERROR) << "Unrecognized capability name: " << cap_name;
112 return false;
113 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700114 caps[cap_value] = true;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700115 }
116 (*config_out)[set_name] = caps;
117 caps_superset = caps;
118 }
119
120 // We currently only support sets that are identical, except that ambient is
121 // optional.
122 for (const char* set_name : kCapabilitySetNames) {
123 auto it = config_out->find(set_name);
124 if (it == config_out->end() && set_name == kAmbientCapabilitySetName) {
125 // Ambient capabilities are optional.
126 continue;
127 }
128 if (it == config_out->end()) {
129 LOG(ERROR)
130 << "If capabilities are set, all capability sets should be present";
131 return false;
132 }
133 if (it->second != caps_superset) {
134 LOG(ERROR)
135 << "If capabilities are set, all capability sets should be identical";
136 return false;
137 }
138 }
139
140 return true;
141}
142
Dylan Reid93fa4602017-06-06 13:39:31 -0700143const std::map<std::string, int> kRlimitMap = {
144#define RLIMIT_MAP_ENTRY(limit) \
145 { "RLIMIT_" #limit, RLIMIT_##limit }
146 RLIMIT_MAP_ENTRY(CPU),
147 RLIMIT_MAP_ENTRY(FSIZE),
148 RLIMIT_MAP_ENTRY(DATA),
149 RLIMIT_MAP_ENTRY(STACK),
150 RLIMIT_MAP_ENTRY(CORE),
151 RLIMIT_MAP_ENTRY(RSS),
152 RLIMIT_MAP_ENTRY(NPROC),
153 RLIMIT_MAP_ENTRY(NOFILE),
154 RLIMIT_MAP_ENTRY(MEMLOCK),
155 RLIMIT_MAP_ENTRY(AS),
156 RLIMIT_MAP_ENTRY(LOCKS),
157 RLIMIT_MAP_ENTRY(SIGPENDING),
158 RLIMIT_MAP_ENTRY(MSGQUEUE),
159 RLIMIT_MAP_ENTRY(NICE),
160 RLIMIT_MAP_ENTRY(RTPRIO),
161 RLIMIT_MAP_ENTRY(RTTIME),
162#undef RLIMIT_MAP_ENTRY
163};
164
165// Fills |config_out| with information about the capability sets in the
166// container.
167bool ParseRlimitsConfig(const base::ListValue& rlimits_list,
168 std::vector<OciProcessRlimit>* rlimits_out) {
169 size_t num_limits = rlimits_list.GetSize();
170 for (size_t i = 0; i < num_limits; ++i) {
171 const base::DictionaryValue* rlimits_dict;
172 if (!rlimits_list.GetDictionary(i, &rlimits_dict)) {
173 LOG(ERROR) << "Fail to get rlimit item " << i;
174 return false;
175 }
176
177 std::string rlimit_name;
178 if (!rlimits_dict->GetString("type", &rlimit_name)) {
179 LOG(ERROR) << "Fail to get type of rlimit " << i;
180 return false;
181 }
182 const auto it = kRlimitMap.find(rlimit_name);
183 if (it == kRlimitMap.end()) {
184 LOG(ERROR) << "Unrecognized rlimit name: " << rlimit_name;
185 return false;
186 }
187
188 OciProcessRlimit limit;
189 limit.type = it->second;
190 if (!ParseUint32FromDict(*rlimits_dict, "hard", &limit.hard)) {
191 LOG(ERROR) << "Fail to get hard limit of rlimit " << i;
192 return false;
193 }
194 if (!ParseUint32FromDict(*rlimits_dict, "soft", &limit.soft)) {
195 LOG(ERROR) << "Fail to get soft limit of rlimit " << i;
196 return false;
197 }
198 rlimits_out->push_back(limit);
199 }
200
201 return true;
202}
203
Dylan Reid6b590e62016-10-27 19:10:53 -0700204// Fills |config_out| with information about the main process to run in the
205// container and the user it should be run as.
206bool ParseProcessConfig(const base::DictionaryValue& config_root_dict,
207 OciConfigPtr const& config_out) {
208 // |process_dict| stays owned by |config_root_dict|
209 const base::DictionaryValue* process_dict = nullptr;
210 if (!config_root_dict.GetDictionary("process", &process_dict)) {
211 LOG(ERROR) << "Fail to get main process from config";
212 return false;
213 }
214 process_dict->GetBoolean("terminal", &config_out->process.terminal);
215 // |user_dict| stays owned by |process_dict|
216 const base::DictionaryValue* user_dict = nullptr;
217 if (!process_dict->GetDictionary("user", &user_dict)) {
218 LOG(ERROR) << "Failed to get user info from config";
219 return false;
220 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700221 if (!ParseUint32FromDict(*user_dict, "uid", &config_out->process.user.uid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700222 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700223 if (!ParseUint32FromDict(*user_dict, "gid", &config_out->process.user.gid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700224 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700225 // |args_list| stays owned by |process_dict|
226 const base::ListValue* args_list = nullptr;
227 if (!process_dict->GetList("args", &args_list)) {
228 LOG(ERROR) << "Fail to get main process args from config";
229 return false;
230 }
231 size_t num_args = args_list->GetSize();
232 for (size_t i = 0; i < num_args; ++i) {
233 std::string arg;
234 if (!args_list->GetString(i, &arg)) {
235 LOG(ERROR) << "Fail to get process args from config";
236 return false;
237 }
238 config_out->process.args.push_back(arg);
239 }
240 // |env_list| stays owned by |process_dict|
241 const base::ListValue* env_list = nullptr;
242 if (process_dict->GetList("env", &env_list)) {
243 size_t num_env = env_list->GetSize();
244 for (size_t i = 0; i < num_env; ++i) {
245 std::string env;
246 if (!env_list->GetString(i, &env)) {
247 LOG(ERROR) << "Fail to get process env from config";
248 return false;
249 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700250 std::vector<std::string> kvp = base::SplitString(
251 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
252 if (kvp.size() != 2) {
253 LOG(ERROR) << "Fail to parse env \"" << env
254 << "\". Must be in name=value format.";
255 return false;
256 }
257 config_out->process.env.insert(std::make_pair(kvp[0], kvp[1]));
Dylan Reid6b590e62016-10-27 19:10:53 -0700258 }
259 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700260 std::string path;
261 if (!process_dict->GetString("cwd", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700262 LOG(ERROR) << "failed to get cwd of process";
263 return false;
264 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700265 config_out->process.cwd = base::FilePath(path);
266
Luis Hector Chavez15e8e672017-07-20 15:13:27 -0700267 // selinuxLabel is optional.
268 process_dict->GetString("selinuxLabel", &config_out->process.selinuxLabel);
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700269 // |capabilities_dict| stays owned by |process_dict|
270 const base::DictionaryValue* capabilities_dict = nullptr;
271 if (process_dict->GetDictionary("capabilities", &capabilities_dict)) {
272 if (!ParseCapabilitiesConfig(*capabilities_dict,
273 &config_out->process.capabilities)) {
274 return false;
275 }
276 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700277
Dylan Reid93fa4602017-06-06 13:39:31 -0700278 // |rlimit_list| stays owned by |process_dict|
279 const base::ListValue* rlimits_list = nullptr;
280 if (process_dict->GetList("rlimits", &rlimits_list)) {
281 if (!ParseRlimitsConfig(*rlimits_list, &config_out->process.rlimits)) {
282 return false;
283 }
284 }
285
Dylan Reid6b590e62016-10-27 19:10:53 -0700286 return true;
287}
288
289// Parses the 'mounts' field. The necessary mounts for running the container
290// are specified here.
291bool ParseMounts(const base::DictionaryValue& config_root_dict,
292 OciConfigPtr const& config_out) {
293 // |config_mounts_list| stays owned by |config_root_dict|
294 const base::ListValue* config_mounts_list = nullptr;
295 if (!config_root_dict.GetList("mounts", &config_mounts_list)) {
296 LOG(ERROR) << "Fail to get mounts from config dictionary";
297 return false;
298 }
299
300 for (size_t i = 0; i < config_mounts_list->GetSize(); ++i) {
301 const base::DictionaryValue* mount_dict;
302 if (!config_mounts_list->GetDictionary(i, &mount_dict)) {
303 LOG(ERROR) << "Fail to get mount item " << i;
304 return false;
305 }
306 OciMount mount;
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700307 std::string path;
308 if (!mount_dict->GetString("destination", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700309 LOG(ERROR) << "Fail to get mount path for mount " << i;
310 return false;
311 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700312 mount.destination = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700313 if (!mount_dict->GetString("type", &mount.type)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700314 LOG(ERROR) << "Fail to get mount type for mount " << i;
315 return false;
316 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700317 if (!mount_dict->GetString("source", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700318 LOG(ERROR) << "Fail to get mount source for mount " << i;
319 return false;
320 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700321 mount.source = base::FilePath(path);
Dylan Reid6b590e62016-10-27 19:10:53 -0700322
323 // |options| are owned by |mount_dict|
324 const base::ListValue* options = nullptr;
325 if (mount_dict->GetList("options", &options)) {
326 for (size_t j = 0; j < options->GetSize(); ++j) {
327 std::string this_opt;
328 if (!options->GetString(j, &this_opt)) {
329 LOG(ERROR) << "Fail to get option " << j << " from mount options";
330 return false;
331 }
332 mount.options.push_back(this_opt);
333 }
334 }
335
336 config_out->mounts.push_back(mount);
337 }
338 return true;
339}
340
Dylan Reid6985e3b2017-03-31 19:39:16 -0700341// Parses the linux resource list
342bool ParseResources(const base::DictionaryValue& resources_dict,
343 OciLinuxResources* resources_out) {
344 // |device_list| is owned by |resources_dict|
345 const base::ListValue* device_list = nullptr;
346 if (!resources_dict.GetList("devices", &device_list)) {
347 // The device list is optional.
348 return true;
349 }
350 size_t num_devices = device_list->GetSize();
351 for (size_t i = 0; i < num_devices; ++i) {
352 OciLinuxCgroupDevice device;
353
354 const base::DictionaryValue* dev;
355 if (!device_list->GetDictionary(i, &dev)) {
356 LOG(ERROR) << "Fail to get device " << i;
357 return false;
358 }
359
360 if (!dev->GetBoolean("allow", &device.allow)) {
361 LOG(ERROR) << "Fail to get allow value for device " << i;
362 return false;
363 }
364 if (!dev->GetString("access", &device.access))
365 device.access = "rwm"; // Optional, default to all perms.
366 if (!dev->GetString("type", &device.type))
367 device.type = "a"; // Optional, default to both a means all.
368 if (!ParseUint32FromDict(*dev, "major", &device.major))
369 device.major = -1; // Optional, -1 will map to all devices.
370 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
371 device.minor = -1; // Optional, -1 will map to all devices.
372
373 resources_out->devices.push_back(device);
374 }
375
376 return true;
377}
378
Stephen Barber3c0a2022017-09-08 14:17:57 -0700379// Parses the list of namespaces and fills |namespaces_out| with them.
380bool ParseNamespaces(const base::ListValue* namespaces_list,
381 std::vector<OciNamespace>* namespaces_out) {
382 for (size_t i = 0; i < namespaces_list->GetSize(); ++i) {
383 OciNamespace new_namespace;
384 const base::DictionaryValue* ns;
385 if (!namespaces_list->GetDictionary(i, &ns)) {
386 LOG(ERROR) << "Failed to get namespace " << i;
387 return false;
388 }
389 if (!ns->GetString("type", &new_namespace.type)) {
390 LOG(ERROR) << "Namespace " << i << " missing type";
391 return false;
392 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700393 std::string path;
394 if (ns->GetString("path", &path))
395 new_namespace.path = base::FilePath(path);
Stephen Barber3c0a2022017-09-08 14:17:57 -0700396 namespaces_out->push_back(new_namespace);
397 }
398 return true;
399}
400
Dylan Reid6b590e62016-10-27 19:10:53 -0700401// Parse the list of device nodes that the container needs to run.
402bool ParseDeviceList(const base::DictionaryValue& linux_dict,
403 OciConfigPtr const& config_out) {
404 // |device_list| is owned by |linux_dict|
405 const base::ListValue* device_list = nullptr;
406 if (!linux_dict.GetList("devices", &device_list)) {
Dylan Reida5ed1272016-11-11 16:43:39 -0800407 // The device list is optional.
408 return true;
Dylan Reid6b590e62016-10-27 19:10:53 -0700409 }
410 size_t num_devices = device_list->GetSize();
411 for (size_t i = 0; i < num_devices; ++i) {
412 OciLinuxDevice device;
413
414 const base::DictionaryValue* dev;
415 if (!device_list->GetDictionary(i, &dev)) {
416 LOG(ERROR) << "Fail to get device " << i;
417 return false;
418 }
419 std::string path;
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700420 if (!dev->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700421 LOG(ERROR) << "Fail to get path for dev";
422 return false;
423 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700424 device.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700425 if (!dev->GetString("type", &device.type)) {
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700426 LOG(ERROR) << "Fail to get type for " << device.path.value();
Dylan Reid6b590e62016-10-27 19:10:53 -0700427 return false;
428 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700429 if (!ParseUint32FromDict(*dev, "major", &device.major))
430 return false;
Luis Hector Chavezac20fc52017-10-10 11:08:41 -0700431 dev->GetBoolean("dynamicMinor", &device.dynamicMinor);
432 if (device.dynamicMinor) {
433 if (dev->HasKey("minor")) {
434 LOG(WARNING)
435 << "Ignoring \"minor\" since \"dynamicMinor\" is specified for "
436 << device.path.value();
437 }
438 } else {
439 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
440 return false;
441 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700442 if (!ParseUint32FromDict(*dev, "fileMode", &device.fileMode))
443 return false;
444 if (!ParseUint32FromDict(*dev, "uid", &device.uid))
445 return false;
446 if (!ParseUint32FromDict(*dev, "gid", &device.gid))
447 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700448
449 config_out->linux_config.devices.push_back(device);
450 }
451
452 return true;
453}
454
455// Parses the list of ID mappings and fills |mappings_out| with them.
456bool ParseLinuxIdMappings(const base::ListValue* id_map_list,
Ben Chanf43980b2017-03-10 11:31:46 -0800457 std::vector<OciLinuxNamespaceMapping>* mappings_out) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700458 for (size_t i = 0; i < id_map_list->GetSize(); ++i) {
459 OciLinuxNamespaceMapping new_map;
460 const base::DictionaryValue* map;
461 if (!id_map_list->GetDictionary(i, &map)) {
462 LOG(ERROR) << "Fail to get id map " << i;
463 return false;
464 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700465 if (!ParseUint32FromDict(*map, "hostID", &new_map.hostID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700466 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700467 if (!ParseUint32FromDict(*map, "containerID", &new_map.containerID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700468 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700469 if (!ParseUint32FromDict(*map, "size", &new_map.size))
Dylan Reid6b590e62016-10-27 19:10:53 -0700470 return false;
Ben Chanf43980b2017-03-10 11:31:46 -0800471 mappings_out->push_back(new_map);
Dylan Reid6b590e62016-10-27 19:10:53 -0700472 }
473 return true;
474}
475
Dylan Reidb0a72772016-11-03 16:27:50 +0000476// Parses seccomp syscall args.
477bool ParseSeccompArgs(const base::DictionaryValue& syscall_dict,
478 OciSeccompSyscall* syscall_out) {
479 const base::ListValue* args = nullptr;
480 if (syscall_dict.GetList("args", &args)) {
481 for (size_t i = 0; i < args->GetSize(); ++i) {
482 const base::DictionaryValue* args_dict = nullptr;
483 if (!args->GetDictionary(i, &args_dict)) {
484 LOG(ERROR) << "Failed to pars args dict for " << syscall_out->name;
485 return false;
486 }
487 OciSeccompArg this_arg;
488 if (!ParseUint32FromDict(*args_dict, "index", &this_arg.index))
489 return false;
490 if (!ParseUint64FromDict(*args_dict, "value", &this_arg.value))
491 return false;
492 if (!ParseUint64FromDict(*args_dict, "value2", &this_arg.value2))
493 return false;
494 if (!args_dict->GetString("op", &this_arg.op)) {
495 LOG(ERROR) << "Failed to parse op for arg " << this_arg.index
496 << " of " << syscall_out->name;
497 return false;
498 }
499 syscall_out->args.push_back(this_arg);
500 }
501 }
502 return true;
503}
504
505// Parses the seccomp node if it is present.
506bool ParseSeccompInfo(const base::DictionaryValue& seccomp_dict,
507 OciSeccomp* seccomp_out) {
508 if (!seccomp_dict.GetString("defaultAction",
509 &seccomp_out->defaultAction))
510 return false;
511
512 // Gets the list of architectures.
513 const base::ListValue* architectures = nullptr;
514 if (!seccomp_dict.GetList("architectures", &architectures)) {
515 LOG(ERROR) << "Fail to read seccomp architectures";
516 return false;
517 }
518 for (size_t i = 0; i < architectures->GetSize(); ++i) {
519 std::string this_arch;
520 if (!architectures->GetString(i, &this_arch)) {
521 LOG(ERROR) << "Fail to parse seccomp architecture list";
522 return false;
523 }
524 seccomp_out->architectures.push_back(this_arch);
525 }
526
527 // Gets the list of syscalls.
528 const base::ListValue* syscalls = nullptr;
529 if (!seccomp_dict.GetList("syscalls", &syscalls)) {
530 LOG(ERROR) << "Fail to read seccomp syscalls";
531 return false;
532 }
533 for (size_t i = 0; i < syscalls->GetSize(); ++i) {
534 const base::DictionaryValue* syscall_dict = nullptr;
535 if (!syscalls->GetDictionary(i, &syscall_dict)) {
536 LOG(ERROR) << "Fail to parse seccomp syscalls list";
537 return false;
538 }
539 OciSeccompSyscall this_syscall;
540 if (!syscall_dict->GetString("name", &this_syscall.name)) {
541 LOG(ERROR) << "Fail to parse syscall name " << i;
542 return false;
543 }
544 if (!syscall_dict->GetString("action", &this_syscall.action)) {
545 LOG(ERROR) << "Fail to parse syscall action for " << this_syscall.name;
546 return false;
547 }
548 if (!ParseSeccompArgs(*syscall_dict, &this_syscall))
549 return false;
550 seccomp_out->syscalls.push_back(this_syscall);
551 }
552
553 return true;
554}
555
Dylan Reid6b590e62016-10-27 19:10:53 -0700556// Parses the linux node which has information about setting up a user
557// namespace, and the list of devices for the container.
558bool ParseLinuxConfigDict(const base::DictionaryValue& runtime_root_dict,
559 OciConfigPtr const& config_out) {
560 // |linux_dict| is owned by |runtime_root_dict|
561 const base::DictionaryValue* linux_dict = nullptr;
562 if (!runtime_root_dict.GetDictionary("linux", &linux_dict)) {
563 LOG(ERROR) << "Fail to get linux dictionary from the runtime dictionary";
564 return false;
565 }
566
567 // |uid_map_list| is owned by |linux_dict|
568 const base::ListValue* uid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700569 if (linux_dict->GetList("uidMappings", &uid_map_list))
570 ParseLinuxIdMappings(uid_map_list, &config_out->linux_config.uidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700571
572 // |gid_map_list| is owned by |linux_dict|
573 const base::ListValue* gid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700574 if (linux_dict->GetList("gidMappings", &gid_map_list))
575 ParseLinuxIdMappings(gid_map_list, &config_out->linux_config.gidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700576
577 if (!ParseDeviceList(*linux_dict, config_out))
578 return false;
579
Dylan Reid6985e3b2017-03-31 19:39:16 -0700580 const base::DictionaryValue* resources_dict = nullptr;
581 if (linux_dict->GetDictionary("resources", &resources_dict)) {
582 if (!ParseResources(*resources_dict, &config_out->linux_config.resources))
583 return false;
584 }
585
Stephen Barber3c0a2022017-09-08 14:17:57 -0700586 const base::ListValue* namespaces_list = nullptr;
587 if (linux_dict->GetList("namespaces", &namespaces_list)) {
588 if (!ParseNamespaces(namespaces_list, &config_out->linux_config.namespaces))
589 return false;
590 }
591
Dylan Reidb0a72772016-11-03 16:27:50 +0000592 const base::DictionaryValue* seccomp_dict = nullptr;
593 if (linux_dict->GetDictionary("seccomp", &seccomp_dict)) {
594 if (!ParseSeccompInfo(*seccomp_dict, &config_out->linux_config.seccomp))
595 return false;
596 }
597
Dylan Reid6b590e62016-10-27 19:10:53 -0700598 return true;
599}
600
Dylan Reid45e34fe2016-12-02 15:11:53 -0800601bool HostnameValid(const std::string& hostname) {
602 if (hostname.length() > 255)
603 return false;
604
605 const std::regex name("^[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?$");
606 if (!std::regex_match(hostname, name))
607 return false;
608
609 const std::regex double_dash("--");
610 if (std::regex_match(hostname, double_dash))
611 return false;
612
613 return true;
614}
615
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700616bool ParseHooksList(const base::ListValue& hooks_list,
617 std::vector<OciHook>* hooks_out,
618 const std::string& hook_type) {
619 size_t num_hooks = hooks_list.GetSize();
620 for (size_t i = 0; i < num_hooks; ++i) {
621 OciHook hook;
622 const base::DictionaryValue* hook_dict;
623 if (!hooks_list.GetDictionary(i, &hook_dict)) {
624 LOG(ERROR) << "Fail to get " << hook_type << " hook item " << i;
625 return false;
626 }
627
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700628 std::string path;
629 if (!hook_dict->GetString("path", &path)) {
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700630 LOG(ERROR) << "Fail to get path of " << hook_type << " hook " << i;
631 return false;
632 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700633 hook.path = base::FilePath(path);
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700634
635 const base::ListValue* hook_args;
636 // args are optional.
637 if (hook_dict->GetList("args", &hook_args)) {
638 size_t num_args = hook_args->GetSize();
639 for (size_t j = 0; j < num_args; ++j) {
640 std::string arg;
641 if (!hook_args->GetString(j, &arg)) {
642 LOG(ERROR) << "Fail to get arg " << j << " of " << hook_type
643 << " hook " << i;
644 return false;
645 }
646 hook.args.push_back(arg);
647 }
648 }
649
650 const base::ListValue* hook_envs;
651 // envs are optional.
652 if (hook_dict->GetList("env", &hook_envs)) {
653 size_t num_env = hook_envs->GetSize();
654 for (size_t j = 0; j < num_env; ++j) {
655 std::string env;
656 if (!hook_envs->GetString(j, &env)) {
657 LOG(ERROR) << "Fail to get env " << j << " of " << hook_type
658 << " hook " << i;
659 return false;
660 }
661 std::vector<std::string> kvp = base::SplitString(
662 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
663 if (kvp.size() != 2) {
664 LOG(ERROR) << "Fail to parse env \"" << env
665 << "\". Must be in name=value format.";
666 return false;
667 }
668 hook.env.insert(std::make_pair(kvp[0], kvp[1]));
669 }
670 }
671
672 int timeout_seconds;
673 // timeout is optional.
674 if (hook_dict->GetInteger("timeout", &timeout_seconds)) {
675 hook.timeout = base::TimeDelta::FromSeconds(timeout_seconds);
676 } else {
677 hook.timeout = base::TimeDelta::Max();
678 }
679
680 hooks_out->emplace_back(std::move(hook));
681 }
682 return true;
683}
684
685bool ParseHooks(const base::DictionaryValue& config_root_dict,
686 OciConfigPtr const& config_out) {
687 const base::DictionaryValue* hooks_config_dict;
688 if (!config_root_dict.GetDictionary("hooks", &hooks_config_dict)) {
689 // Hooks are optional.
690 return true;
691 }
692
693 const base::ListValue* hooks_list;
Luis Hector Chavezbb515a02017-09-29 15:44:35 -0700694 if (hooks_config_dict->GetList("prechroot", &hooks_list)) {
695 if (!ParseHooksList(*hooks_list, &config_out->pre_chroot_hooks,
696 "prechroot")) {
697 return false;
698 }
699 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700700 if (hooks_config_dict->GetList("prestart", &hooks_list)) {
701 if (!ParseHooksList(*hooks_list, &config_out->pre_start_hooks, "prestart"))
702 return false;
703 }
704 if (hooks_config_dict->GetList("poststart", &hooks_list)) {
705 if (!ParseHooksList(
706 *hooks_list, &config_out->post_start_hooks, "poststart"))
707 return false;
708 }
709 if (hooks_config_dict->GetList("poststop", &hooks_list)) {
710 if (!ParseHooksList(*hooks_list, &config_out->post_stop_hooks, "poststop"))
711 return false;
712 }
713 return true;
714}
715
Dylan Reid6b590e62016-10-27 19:10:53 -0700716// Parses the configuration file for the container. The config file specifies
717// basic filesystem info and details about the process to be run. namespace,
718// cgroup, and syscall configurations are also specified
719bool ParseConfigDict(const base::DictionaryValue& config_root_dict,
720 OciConfigPtr const& config_out) {
Dylan Reid6d650742016-11-02 23:10:38 -0700721 if (!config_root_dict.GetString("ociVersion", &config_out->ociVersion)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700722 LOG(ERROR) << "Failed to parse ociVersion";
723 return false;
724 }
725 if (!config_root_dict.GetString("hostname", &config_out->hostname)) {
726 LOG(ERROR) << "Failed to parse hostname";
727 return false;
728 }
Dylan Reid45e34fe2016-12-02 15:11:53 -0800729 if (!HostnameValid(config_out->hostname)) {
730 LOG(ERROR) << "Invalid hostname " << config_out->hostname;
731 return false;
732 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700733
734 // Platform info
735 if (!ParsePlatformConfig(config_root_dict, config_out)) {
736 return false;
737 }
738
739 // Root fs info
740 if (!ParseRootFileSystemConfig(config_root_dict, config_out)) {
741 return false;
742 }
743
744 // Process info
745 if (!ParseProcessConfig(config_root_dict, config_out)) {
746 return false;
747 }
748
749 // Get a list of mount points and mounts.
750 if (!ParseMounts(config_root_dict, config_out)) {
751 LOG(ERROR) << "Failed to parse mounts";
752 return false;
753 }
754
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700755 // Hooks info
756 if (!ParseHooks(config_root_dict, config_out)) {
757 return false;
758 }
759
Dylan Reid6b590e62016-10-27 19:10:53 -0700760 // Parse linux node.
761 if (!ParseLinuxConfigDict(config_root_dict, config_out)) {
762 LOG(ERROR) << "Failed to parse the linux node";
763 return false;
764 }
765
766 return true;
767}
768
Ben Chanf43980b2017-03-10 11:31:46 -0800769} // anonymous namespace
Dylan Reid6b590e62016-10-27 19:10:53 -0700770
771bool ParseContainerConfig(const std::string& config_json_data,
772 OciConfigPtr const& config_out) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700773 std::string error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700774 std::unique_ptr<const base::Value> config_root_val =
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700775 base::JSONReader::ReadAndReturnError(config_json_data,
776 base::JSON_PARSE_RFC,
777 nullptr /* error_code_out */,
778 &error_msg,
779 nullptr /* error_line_out */,
780 nullptr /* error_column_out */);
Dylan Reid6b590e62016-10-27 19:10:53 -0700781 if (!config_root_val) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700782 LOG(ERROR) << "Fail to parse config.json: " << error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700783 return false;
784 }
785 const base::DictionaryValue* config_dict = nullptr;
786 if (!config_root_val->GetAsDictionary(&config_dict)) {
787 LOG(ERROR) << "Fail to parse root dictionary from config.json";
788 return false;
789 }
790 if (!ParseConfigDict(*config_dict, config_out)) {
791 return false;
792 }
793
794 return true;
795}
796
Stephen Barber5f6dc9b2017-04-04 12:36:32 -0700797} // namespace run_oci