blob: c00adc8e0399d4bbb05b36122c29e502e4ada5bd [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;
431 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
432 return false;
433 if (!ParseUint32FromDict(*dev, "fileMode", &device.fileMode))
434 return false;
435 if (!ParseUint32FromDict(*dev, "uid", &device.uid))
436 return false;
437 if (!ParseUint32FromDict(*dev, "gid", &device.gid))
438 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700439
440 config_out->linux_config.devices.push_back(device);
441 }
442
443 return true;
444}
445
446// Parses the list of ID mappings and fills |mappings_out| with them.
447bool ParseLinuxIdMappings(const base::ListValue* id_map_list,
Ben Chanf43980b2017-03-10 11:31:46 -0800448 std::vector<OciLinuxNamespaceMapping>* mappings_out) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700449 for (size_t i = 0; i < id_map_list->GetSize(); ++i) {
450 OciLinuxNamespaceMapping new_map;
451 const base::DictionaryValue* map;
452 if (!id_map_list->GetDictionary(i, &map)) {
453 LOG(ERROR) << "Fail to get id map " << i;
454 return false;
455 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700456 if (!ParseUint32FromDict(*map, "hostID", &new_map.hostID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700457 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700458 if (!ParseUint32FromDict(*map, "containerID", &new_map.containerID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700459 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700460 if (!ParseUint32FromDict(*map, "size", &new_map.size))
Dylan Reid6b590e62016-10-27 19:10:53 -0700461 return false;
Ben Chanf43980b2017-03-10 11:31:46 -0800462 mappings_out->push_back(new_map);
Dylan Reid6b590e62016-10-27 19:10:53 -0700463 }
464 return true;
465}
466
Dylan Reidb0a72772016-11-03 16:27:50 +0000467// Parses seccomp syscall args.
468bool ParseSeccompArgs(const base::DictionaryValue& syscall_dict,
469 OciSeccompSyscall* syscall_out) {
470 const base::ListValue* args = nullptr;
471 if (syscall_dict.GetList("args", &args)) {
472 for (size_t i = 0; i < args->GetSize(); ++i) {
473 const base::DictionaryValue* args_dict = nullptr;
474 if (!args->GetDictionary(i, &args_dict)) {
475 LOG(ERROR) << "Failed to pars args dict for " << syscall_out->name;
476 return false;
477 }
478 OciSeccompArg this_arg;
479 if (!ParseUint32FromDict(*args_dict, "index", &this_arg.index))
480 return false;
481 if (!ParseUint64FromDict(*args_dict, "value", &this_arg.value))
482 return false;
483 if (!ParseUint64FromDict(*args_dict, "value2", &this_arg.value2))
484 return false;
485 if (!args_dict->GetString("op", &this_arg.op)) {
486 LOG(ERROR) << "Failed to parse op for arg " << this_arg.index
487 << " of " << syscall_out->name;
488 return false;
489 }
490 syscall_out->args.push_back(this_arg);
491 }
492 }
493 return true;
494}
495
496// Parses the seccomp node if it is present.
497bool ParseSeccompInfo(const base::DictionaryValue& seccomp_dict,
498 OciSeccomp* seccomp_out) {
499 if (!seccomp_dict.GetString("defaultAction",
500 &seccomp_out->defaultAction))
501 return false;
502
503 // Gets the list of architectures.
504 const base::ListValue* architectures = nullptr;
505 if (!seccomp_dict.GetList("architectures", &architectures)) {
506 LOG(ERROR) << "Fail to read seccomp architectures";
507 return false;
508 }
509 for (size_t i = 0; i < architectures->GetSize(); ++i) {
510 std::string this_arch;
511 if (!architectures->GetString(i, &this_arch)) {
512 LOG(ERROR) << "Fail to parse seccomp architecture list";
513 return false;
514 }
515 seccomp_out->architectures.push_back(this_arch);
516 }
517
518 // Gets the list of syscalls.
519 const base::ListValue* syscalls = nullptr;
520 if (!seccomp_dict.GetList("syscalls", &syscalls)) {
521 LOG(ERROR) << "Fail to read seccomp syscalls";
522 return false;
523 }
524 for (size_t i = 0; i < syscalls->GetSize(); ++i) {
525 const base::DictionaryValue* syscall_dict = nullptr;
526 if (!syscalls->GetDictionary(i, &syscall_dict)) {
527 LOG(ERROR) << "Fail to parse seccomp syscalls list";
528 return false;
529 }
530 OciSeccompSyscall this_syscall;
531 if (!syscall_dict->GetString("name", &this_syscall.name)) {
532 LOG(ERROR) << "Fail to parse syscall name " << i;
533 return false;
534 }
535 if (!syscall_dict->GetString("action", &this_syscall.action)) {
536 LOG(ERROR) << "Fail to parse syscall action for " << this_syscall.name;
537 return false;
538 }
539 if (!ParseSeccompArgs(*syscall_dict, &this_syscall))
540 return false;
541 seccomp_out->syscalls.push_back(this_syscall);
542 }
543
544 return true;
545}
546
Dylan Reid6b590e62016-10-27 19:10:53 -0700547// Parses the linux node which has information about setting up a user
548// namespace, and the list of devices for the container.
549bool ParseLinuxConfigDict(const base::DictionaryValue& runtime_root_dict,
550 OciConfigPtr const& config_out) {
551 // |linux_dict| is owned by |runtime_root_dict|
552 const base::DictionaryValue* linux_dict = nullptr;
553 if (!runtime_root_dict.GetDictionary("linux", &linux_dict)) {
554 LOG(ERROR) << "Fail to get linux dictionary from the runtime dictionary";
555 return false;
556 }
557
558 // |uid_map_list| is owned by |linux_dict|
559 const base::ListValue* uid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700560 if (linux_dict->GetList("uidMappings", &uid_map_list))
561 ParseLinuxIdMappings(uid_map_list, &config_out->linux_config.uidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700562
563 // |gid_map_list| is owned by |linux_dict|
564 const base::ListValue* gid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700565 if (linux_dict->GetList("gidMappings", &gid_map_list))
566 ParseLinuxIdMappings(gid_map_list, &config_out->linux_config.gidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700567
568 if (!ParseDeviceList(*linux_dict, config_out))
569 return false;
570
Dylan Reid6985e3b2017-03-31 19:39:16 -0700571 const base::DictionaryValue* resources_dict = nullptr;
572 if (linux_dict->GetDictionary("resources", &resources_dict)) {
573 if (!ParseResources(*resources_dict, &config_out->linux_config.resources))
574 return false;
575 }
576
Stephen Barber3c0a2022017-09-08 14:17:57 -0700577 const base::ListValue* namespaces_list = nullptr;
578 if (linux_dict->GetList("namespaces", &namespaces_list)) {
579 if (!ParseNamespaces(namespaces_list, &config_out->linux_config.namespaces))
580 return false;
581 }
582
Dylan Reidb0a72772016-11-03 16:27:50 +0000583 const base::DictionaryValue* seccomp_dict = nullptr;
584 if (linux_dict->GetDictionary("seccomp", &seccomp_dict)) {
585 if (!ParseSeccompInfo(*seccomp_dict, &config_out->linux_config.seccomp))
586 return false;
587 }
588
Dylan Reid6b590e62016-10-27 19:10:53 -0700589 return true;
590}
591
Dylan Reid45e34fe2016-12-02 15:11:53 -0800592bool HostnameValid(const std::string& hostname) {
593 if (hostname.length() > 255)
594 return false;
595
596 const std::regex name("^[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?$");
597 if (!std::regex_match(hostname, name))
598 return false;
599
600 const std::regex double_dash("--");
601 if (std::regex_match(hostname, double_dash))
602 return false;
603
604 return true;
605}
606
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700607bool ParseHooksList(const base::ListValue& hooks_list,
608 std::vector<OciHook>* hooks_out,
609 const std::string& hook_type) {
610 size_t num_hooks = hooks_list.GetSize();
611 for (size_t i = 0; i < num_hooks; ++i) {
612 OciHook hook;
613 const base::DictionaryValue* hook_dict;
614 if (!hooks_list.GetDictionary(i, &hook_dict)) {
615 LOG(ERROR) << "Fail to get " << hook_type << " hook item " << i;
616 return false;
617 }
618
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700619 std::string path;
620 if (!hook_dict->GetString("path", &path)) {
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700621 LOG(ERROR) << "Fail to get path of " << hook_type << " hook " << i;
622 return false;
623 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700624 hook.path = base::FilePath(path);
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700625
626 const base::ListValue* hook_args;
627 // args are optional.
628 if (hook_dict->GetList("args", &hook_args)) {
629 size_t num_args = hook_args->GetSize();
630 for (size_t j = 0; j < num_args; ++j) {
631 std::string arg;
632 if (!hook_args->GetString(j, &arg)) {
633 LOG(ERROR) << "Fail to get arg " << j << " of " << hook_type
634 << " hook " << i;
635 return false;
636 }
637 hook.args.push_back(arg);
638 }
639 }
640
641 const base::ListValue* hook_envs;
642 // envs are optional.
643 if (hook_dict->GetList("env", &hook_envs)) {
644 size_t num_env = hook_envs->GetSize();
645 for (size_t j = 0; j < num_env; ++j) {
646 std::string env;
647 if (!hook_envs->GetString(j, &env)) {
648 LOG(ERROR) << "Fail to get env " << j << " of " << hook_type
649 << " hook " << i;
650 return false;
651 }
652 std::vector<std::string> kvp = base::SplitString(
653 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
654 if (kvp.size() != 2) {
655 LOG(ERROR) << "Fail to parse env \"" << env
656 << "\". Must be in name=value format.";
657 return false;
658 }
659 hook.env.insert(std::make_pair(kvp[0], kvp[1]));
660 }
661 }
662
663 int timeout_seconds;
664 // timeout is optional.
665 if (hook_dict->GetInteger("timeout", &timeout_seconds)) {
666 hook.timeout = base::TimeDelta::FromSeconds(timeout_seconds);
667 } else {
668 hook.timeout = base::TimeDelta::Max();
669 }
670
671 hooks_out->emplace_back(std::move(hook));
672 }
673 return true;
674}
675
676bool ParseHooks(const base::DictionaryValue& config_root_dict,
677 OciConfigPtr const& config_out) {
678 const base::DictionaryValue* hooks_config_dict;
679 if (!config_root_dict.GetDictionary("hooks", &hooks_config_dict)) {
680 // Hooks are optional.
681 return true;
682 }
683
684 const base::ListValue* hooks_list;
Luis Hector Chavezbb515a02017-09-29 15:44:35 -0700685 if (hooks_config_dict->GetList("prechroot", &hooks_list)) {
686 if (!ParseHooksList(*hooks_list, &config_out->pre_chroot_hooks,
687 "prechroot")) {
688 return false;
689 }
690 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700691 if (hooks_config_dict->GetList("prestart", &hooks_list)) {
692 if (!ParseHooksList(*hooks_list, &config_out->pre_start_hooks, "prestart"))
693 return false;
694 }
695 if (hooks_config_dict->GetList("poststart", &hooks_list)) {
696 if (!ParseHooksList(
697 *hooks_list, &config_out->post_start_hooks, "poststart"))
698 return false;
699 }
700 if (hooks_config_dict->GetList("poststop", &hooks_list)) {
701 if (!ParseHooksList(*hooks_list, &config_out->post_stop_hooks, "poststop"))
702 return false;
703 }
704 return true;
705}
706
Dylan Reid6b590e62016-10-27 19:10:53 -0700707// Parses the configuration file for the container. The config file specifies
708// basic filesystem info and details about the process to be run. namespace,
709// cgroup, and syscall configurations are also specified
710bool ParseConfigDict(const base::DictionaryValue& config_root_dict,
711 OciConfigPtr const& config_out) {
Dylan Reid6d650742016-11-02 23:10:38 -0700712 if (!config_root_dict.GetString("ociVersion", &config_out->ociVersion)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700713 LOG(ERROR) << "Failed to parse ociVersion";
714 return false;
715 }
716 if (!config_root_dict.GetString("hostname", &config_out->hostname)) {
717 LOG(ERROR) << "Failed to parse hostname";
718 return false;
719 }
Dylan Reid45e34fe2016-12-02 15:11:53 -0800720 if (!HostnameValid(config_out->hostname)) {
721 LOG(ERROR) << "Invalid hostname " << config_out->hostname;
722 return false;
723 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700724
725 // Platform info
726 if (!ParsePlatformConfig(config_root_dict, config_out)) {
727 return false;
728 }
729
730 // Root fs info
731 if (!ParseRootFileSystemConfig(config_root_dict, config_out)) {
732 return false;
733 }
734
735 // Process info
736 if (!ParseProcessConfig(config_root_dict, config_out)) {
737 return false;
738 }
739
740 // Get a list of mount points and mounts.
741 if (!ParseMounts(config_root_dict, config_out)) {
742 LOG(ERROR) << "Failed to parse mounts";
743 return false;
744 }
745
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700746 // Hooks info
747 if (!ParseHooks(config_root_dict, config_out)) {
748 return false;
749 }
750
Dylan Reid6b590e62016-10-27 19:10:53 -0700751 // Parse linux node.
752 if (!ParseLinuxConfigDict(config_root_dict, config_out)) {
753 LOG(ERROR) << "Failed to parse the linux node";
754 return false;
755 }
756
757 return true;
758}
759
Ben Chanf43980b2017-03-10 11:31:46 -0800760} // anonymous namespace
Dylan Reid6b590e62016-10-27 19:10:53 -0700761
762bool ParseContainerConfig(const std::string& config_json_data,
763 OciConfigPtr const& config_out) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700764 std::string error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700765 std::unique_ptr<const base::Value> config_root_val =
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700766 base::JSONReader::ReadAndReturnError(config_json_data,
767 base::JSON_PARSE_RFC,
768 nullptr /* error_code_out */,
769 &error_msg,
770 nullptr /* error_line_out */,
771 nullptr /* error_column_out */);
Dylan Reid6b590e62016-10-27 19:10:53 -0700772 if (!config_root_val) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700773 LOG(ERROR) << "Fail to parse config.json: " << error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700774 return false;
775 }
776 const base::DictionaryValue* config_dict = nullptr;
777 if (!config_root_val->GetAsDictionary(&config_dict)) {
778 LOG(ERROR) << "Fail to parse root dictionary from config.json";
779 return false;
780 }
781 if (!ParseConfigDict(*config_dict, config_out)) {
782 return false;
783 }
784
785 return true;
786}
787
Stephen Barber5f6dc9b2017-04-04 12:36:32 -0700788} // namespace run_oci