blob: 7e32aa5852954c7cc2b781480a5c31ece9cc0fef [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 }
77 if (!rootfs_dict->GetString("path", &config_out->root.path)) {
78 LOG(ERROR) << "Fail to get rootfs path from config";
79 return false;
80 }
Dylan Reid6d650742016-11-02 23:10:38 -070081 rootfs_dict->GetBoolean("readonly", &config_out->root.readonly);
Dylan Reid6b590e62016-10-27 19:10:53 -070082 return true;
83}
84
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070085// Fills |config_out| with information about the capability sets in the
86// container.
87bool ParseCapabilitiesConfig(const base::DictionaryValue& capabilities_dict,
88 std::map<std::string, CapSet>* config_out) {
89 constexpr const char* kCapabilitySetNames[] = {
90 "effective", "bounding", "inheritable", "permitted", "ambient"};
91 const std::string kAmbientCapabilitySetName = "ambient";
92
93 CapSet caps_superset;
94 for (const char* set_name : kCapabilitySetNames) {
95 // |capset_list| stays owned by |capabilities_dict|.
96 const base::ListValue* capset_list = nullptr;
97 if (!capabilities_dict.GetList(set_name, &capset_list))
98 continue;
99 CapSet caps;
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700100 cap_value_t cap_value;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700101 for (const auto* cap_name_value : *capset_list) {
102 std::string cap_name;
103 if (!cap_name_value->GetAsString(&cap_name)) {
104 LOG(ERROR) << "Capability list " << set_name
105 << " contains a non-string";
106 return false;
107 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700108 if (cap_from_name(cap_name.c_str(), &cap_value) == -1) {
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700109 LOG(ERROR) << "Unrecognized capability name: " << cap_name;
110 return false;
111 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700112 caps[cap_value] = true;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700113 }
114 (*config_out)[set_name] = caps;
115 caps_superset = caps;
116 }
117
118 // We currently only support sets that are identical, except that ambient is
119 // optional.
120 for (const char* set_name : kCapabilitySetNames) {
121 auto it = config_out->find(set_name);
122 if (it == config_out->end() && set_name == kAmbientCapabilitySetName) {
123 // Ambient capabilities are optional.
124 continue;
125 }
126 if (it == config_out->end()) {
127 LOG(ERROR)
128 << "If capabilities are set, all capability sets should be present";
129 return false;
130 }
131 if (it->second != caps_superset) {
132 LOG(ERROR)
133 << "If capabilities are set, all capability sets should be identical";
134 return false;
135 }
136 }
137
138 return true;
139}
140
Dylan Reid93fa4602017-06-06 13:39:31 -0700141const std::map<std::string, int> kRlimitMap = {
142#define RLIMIT_MAP_ENTRY(limit) \
143 { "RLIMIT_" #limit, RLIMIT_##limit }
144 RLIMIT_MAP_ENTRY(CPU),
145 RLIMIT_MAP_ENTRY(FSIZE),
146 RLIMIT_MAP_ENTRY(DATA),
147 RLIMIT_MAP_ENTRY(STACK),
148 RLIMIT_MAP_ENTRY(CORE),
149 RLIMIT_MAP_ENTRY(RSS),
150 RLIMIT_MAP_ENTRY(NPROC),
151 RLIMIT_MAP_ENTRY(NOFILE),
152 RLIMIT_MAP_ENTRY(MEMLOCK),
153 RLIMIT_MAP_ENTRY(AS),
154 RLIMIT_MAP_ENTRY(LOCKS),
155 RLIMIT_MAP_ENTRY(SIGPENDING),
156 RLIMIT_MAP_ENTRY(MSGQUEUE),
157 RLIMIT_MAP_ENTRY(NICE),
158 RLIMIT_MAP_ENTRY(RTPRIO),
159 RLIMIT_MAP_ENTRY(RTTIME),
160#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;
188 if (!ParseUint32FromDict(*rlimits_dict, "hard", &limit.hard)) {
189 LOG(ERROR) << "Fail to get hard limit of rlimit " << i;
190 return false;
191 }
192 if (!ParseUint32FromDict(*rlimits_dict, "soft", &limit.soft)) {
193 LOG(ERROR) << "Fail to get soft limit of rlimit " << i;
194 return false;
195 }
196 rlimits_out->push_back(limit);
197 }
198
199 return true;
200}
201
Dylan 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 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700219 if (!ParseUint32FromDict(*user_dict, "uid", &config_out->process.user.uid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700220 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700221 if (!ParseUint32FromDict(*user_dict, "gid", &config_out->process.user.gid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700222 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700223 // |args_list| stays owned by |process_dict|
224 const base::ListValue* args_list = nullptr;
225 if (!process_dict->GetList("args", &args_list)) {
226 LOG(ERROR) << "Fail to get main process args from config";
227 return false;
228 }
229 size_t num_args = args_list->GetSize();
230 for (size_t i = 0; i < num_args; ++i) {
231 std::string arg;
232 if (!args_list->GetString(i, &arg)) {
233 LOG(ERROR) << "Fail to get process args from config";
234 return false;
235 }
236 config_out->process.args.push_back(arg);
237 }
238 // |env_list| stays owned by |process_dict|
239 const base::ListValue* env_list = nullptr;
240 if (process_dict->GetList("env", &env_list)) {
241 size_t num_env = env_list->GetSize();
242 for (size_t i = 0; i < num_env; ++i) {
243 std::string env;
244 if (!env_list->GetString(i, &env)) {
245 LOG(ERROR) << "Fail to get process env from config";
246 return false;
247 }
248 config_out->process.env.push_back(env);
249 }
250 }
251 if (!process_dict->GetString("cwd", &config_out->process.cwd)) {
252 LOG(ERROR) << "failed to get cwd of process";
253 return false;
254 }
Luis Hector Chavez15e8e672017-07-20 15:13:27 -0700255 // selinuxLabel is optional.
256 process_dict->GetString("selinuxLabel", &config_out->process.selinuxLabel);
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700257 // |capabilities_dict| stays owned by |process_dict|
258 const base::DictionaryValue* capabilities_dict = nullptr;
259 if (process_dict->GetDictionary("capabilities", &capabilities_dict)) {
260 if (!ParseCapabilitiesConfig(*capabilities_dict,
261 &config_out->process.capabilities)) {
262 return false;
263 }
264 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700265
Dylan Reid93fa4602017-06-06 13:39:31 -0700266 // |rlimit_list| stays owned by |process_dict|
267 const base::ListValue* rlimits_list = nullptr;
268 if (process_dict->GetList("rlimits", &rlimits_list)) {
269 if (!ParseRlimitsConfig(*rlimits_list, &config_out->process.rlimits)) {
270 return false;
271 }
272 }
273
Dylan Reid6b590e62016-10-27 19:10:53 -0700274 return true;
275}
276
277// Parses the 'mounts' field. The necessary mounts for running the container
278// are specified here.
279bool ParseMounts(const base::DictionaryValue& config_root_dict,
280 OciConfigPtr const& config_out) {
281 // |config_mounts_list| stays owned by |config_root_dict|
282 const base::ListValue* config_mounts_list = nullptr;
283 if (!config_root_dict.GetList("mounts", &config_mounts_list)) {
284 LOG(ERROR) << "Fail to get mounts from config dictionary";
285 return false;
286 }
287
288 for (size_t i = 0; i < config_mounts_list->GetSize(); ++i) {
289 const base::DictionaryValue* mount_dict;
290 if (!config_mounts_list->GetDictionary(i, &mount_dict)) {
291 LOG(ERROR) << "Fail to get mount item " << i;
292 return false;
293 }
294 OciMount mount;
Luis Hector Chavez88ce9392017-09-01 09:53:13 -0700295 std::string value;
296 if (!mount_dict->GetString("destination", &value)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700297 LOG(ERROR) << "Fail to get mount path for mount " << i;
298 return false;
299 }
Luis Hector Chavez88ce9392017-09-01 09:53:13 -0700300 mount.destination = base::FilePath(value);
Dylan Reid6d650742016-11-02 23:10:38 -0700301 if (!mount_dict->GetString("type", &mount.type)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700302 LOG(ERROR) << "Fail to get mount type for mount " << i;
303 return false;
304 }
Luis Hector Chavez88ce9392017-09-01 09:53:13 -0700305 if (!mount_dict->GetString("source", &value)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700306 LOG(ERROR) << "Fail to get mount source for mount " << i;
307 return false;
308 }
Luis Hector Chavez88ce9392017-09-01 09:53:13 -0700309 mount.source = base::FilePath(value);
Dylan Reid6b590e62016-10-27 19:10:53 -0700310
311 // |options| are owned by |mount_dict|
312 const base::ListValue* options = nullptr;
313 if (mount_dict->GetList("options", &options)) {
314 for (size_t j = 0; j < options->GetSize(); ++j) {
315 std::string this_opt;
316 if (!options->GetString(j, &this_opt)) {
317 LOG(ERROR) << "Fail to get option " << j << " from mount options";
318 return false;
319 }
320 mount.options.push_back(this_opt);
321 }
322 }
323
324 config_out->mounts.push_back(mount);
325 }
326 return true;
327}
328
Dylan Reid6985e3b2017-03-31 19:39:16 -0700329// Parses the linux resource list
330bool ParseResources(const base::DictionaryValue& resources_dict,
331 OciLinuxResources* resources_out) {
332 // |device_list| is owned by |resources_dict|
333 const base::ListValue* device_list = nullptr;
334 if (!resources_dict.GetList("devices", &device_list)) {
335 // The device list is optional.
336 return true;
337 }
338 size_t num_devices = device_list->GetSize();
339 for (size_t i = 0; i < num_devices; ++i) {
340 OciLinuxCgroupDevice device;
341
342 const base::DictionaryValue* dev;
343 if (!device_list->GetDictionary(i, &dev)) {
344 LOG(ERROR) << "Fail to get device " << i;
345 return false;
346 }
347
348 if (!dev->GetBoolean("allow", &device.allow)) {
349 LOG(ERROR) << "Fail to get allow value for device " << i;
350 return false;
351 }
352 if (!dev->GetString("access", &device.access))
353 device.access = "rwm"; // Optional, default to all perms.
354 if (!dev->GetString("type", &device.type))
355 device.type = "a"; // Optional, default to both a means all.
356 if (!ParseUint32FromDict(*dev, "major", &device.major))
357 device.major = -1; // Optional, -1 will map to all devices.
358 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
359 device.minor = -1; // Optional, -1 will map to all devices.
360
361 resources_out->devices.push_back(device);
362 }
363
364 return true;
365}
366
Stephen Barber3c0a2022017-09-08 14:17:57 -0700367// Parses the list of namespaces and fills |namespaces_out| with them.
368bool ParseNamespaces(const base::ListValue* namespaces_list,
369 std::vector<OciNamespace>* namespaces_out) {
370 for (size_t i = 0; i < namespaces_list->GetSize(); ++i) {
371 OciNamespace new_namespace;
372 const base::DictionaryValue* ns;
373 if (!namespaces_list->GetDictionary(i, &ns)) {
374 LOG(ERROR) << "Failed to get namespace " << i;
375 return false;
376 }
377 if (!ns->GetString("type", &new_namespace.type)) {
378 LOG(ERROR) << "Namespace " << i << " missing type";
379 return false;
380 }
381 ns->GetString("path", &new_namespace.path);
382 namespaces_out->push_back(new_namespace);
383 }
384 return true;
385}
386
Dylan Reid6b590e62016-10-27 19:10:53 -0700387// Parse the list of device nodes that the container needs to run.
388bool ParseDeviceList(const base::DictionaryValue& linux_dict,
389 OciConfigPtr const& config_out) {
390 // |device_list| is owned by |linux_dict|
391 const base::ListValue* device_list = nullptr;
392 if (!linux_dict.GetList("devices", &device_list)) {
Dylan Reida5ed1272016-11-11 16:43:39 -0800393 // The device list is optional.
394 return true;
Dylan Reid6b590e62016-10-27 19:10:53 -0700395 }
396 size_t num_devices = device_list->GetSize();
397 for (size_t i = 0; i < num_devices; ++i) {
398 OciLinuxDevice device;
399
400 const base::DictionaryValue* dev;
401 if (!device_list->GetDictionary(i, &dev)) {
402 LOG(ERROR) << "Fail to get device " << i;
403 return false;
404 }
405 std::string path;
406 if (!dev->GetString("path", &device.path)) {
407 LOG(ERROR) << "Fail to get path for dev";
408 return false;
409 }
Dylan Reid6d650742016-11-02 23:10:38 -0700410 if (!dev->GetString("type", &device.type)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700411 LOG(ERROR) << "Fail to get type for " << device.path;
412 return false;
413 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700414 if (!ParseUint32FromDict(*dev, "major", &device.major))
415 return false;
416 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
417 return false;
418 if (!ParseUint32FromDict(*dev, "fileMode", &device.fileMode))
419 return false;
420 if (!ParseUint32FromDict(*dev, "uid", &device.uid))
421 return false;
422 if (!ParseUint32FromDict(*dev, "gid", &device.gid))
423 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700424
425 config_out->linux_config.devices.push_back(device);
426 }
427
428 return true;
429}
430
431// Parses the list of ID mappings and fills |mappings_out| with them.
432bool ParseLinuxIdMappings(const base::ListValue* id_map_list,
Ben Chanf43980b2017-03-10 11:31:46 -0800433 std::vector<OciLinuxNamespaceMapping>* mappings_out) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700434 for (size_t i = 0; i < id_map_list->GetSize(); ++i) {
435 OciLinuxNamespaceMapping new_map;
436 const base::DictionaryValue* map;
437 if (!id_map_list->GetDictionary(i, &map)) {
438 LOG(ERROR) << "Fail to get id map " << i;
439 return false;
440 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700441 if (!ParseUint32FromDict(*map, "hostID", &new_map.hostID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700442 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700443 if (!ParseUint32FromDict(*map, "containerID", &new_map.containerID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700444 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700445 if (!ParseUint32FromDict(*map, "size", &new_map.size))
Dylan Reid6b590e62016-10-27 19:10:53 -0700446 return false;
Ben Chanf43980b2017-03-10 11:31:46 -0800447 mappings_out->push_back(new_map);
Dylan Reid6b590e62016-10-27 19:10:53 -0700448 }
449 return true;
450}
451
Dylan Reidb0a72772016-11-03 16:27:50 +0000452// Parses seccomp syscall args.
453bool ParseSeccompArgs(const base::DictionaryValue& syscall_dict,
454 OciSeccompSyscall* syscall_out) {
455 const base::ListValue* args = nullptr;
456 if (syscall_dict.GetList("args", &args)) {
457 for (size_t i = 0; i < args->GetSize(); ++i) {
458 const base::DictionaryValue* args_dict = nullptr;
459 if (!args->GetDictionary(i, &args_dict)) {
460 LOG(ERROR) << "Failed to pars args dict for " << syscall_out->name;
461 return false;
462 }
463 OciSeccompArg this_arg;
464 if (!ParseUint32FromDict(*args_dict, "index", &this_arg.index))
465 return false;
466 if (!ParseUint64FromDict(*args_dict, "value", &this_arg.value))
467 return false;
468 if (!ParseUint64FromDict(*args_dict, "value2", &this_arg.value2))
469 return false;
470 if (!args_dict->GetString("op", &this_arg.op)) {
471 LOG(ERROR) << "Failed to parse op for arg " << this_arg.index
472 << " of " << syscall_out->name;
473 return false;
474 }
475 syscall_out->args.push_back(this_arg);
476 }
477 }
478 return true;
479}
480
481// Parses the seccomp node if it is present.
482bool ParseSeccompInfo(const base::DictionaryValue& seccomp_dict,
483 OciSeccomp* seccomp_out) {
484 if (!seccomp_dict.GetString("defaultAction",
485 &seccomp_out->defaultAction))
486 return false;
487
488 // Gets the list of architectures.
489 const base::ListValue* architectures = nullptr;
490 if (!seccomp_dict.GetList("architectures", &architectures)) {
491 LOG(ERROR) << "Fail to read seccomp architectures";
492 return false;
493 }
494 for (size_t i = 0; i < architectures->GetSize(); ++i) {
495 std::string this_arch;
496 if (!architectures->GetString(i, &this_arch)) {
497 LOG(ERROR) << "Fail to parse seccomp architecture list";
498 return false;
499 }
500 seccomp_out->architectures.push_back(this_arch);
501 }
502
503 // Gets the list of syscalls.
504 const base::ListValue* syscalls = nullptr;
505 if (!seccomp_dict.GetList("syscalls", &syscalls)) {
506 LOG(ERROR) << "Fail to read seccomp syscalls";
507 return false;
508 }
509 for (size_t i = 0; i < syscalls->GetSize(); ++i) {
510 const base::DictionaryValue* syscall_dict = nullptr;
511 if (!syscalls->GetDictionary(i, &syscall_dict)) {
512 LOG(ERROR) << "Fail to parse seccomp syscalls list";
513 return false;
514 }
515 OciSeccompSyscall this_syscall;
516 if (!syscall_dict->GetString("name", &this_syscall.name)) {
517 LOG(ERROR) << "Fail to parse syscall name " << i;
518 return false;
519 }
520 if (!syscall_dict->GetString("action", &this_syscall.action)) {
521 LOG(ERROR) << "Fail to parse syscall action for " << this_syscall.name;
522 return false;
523 }
524 if (!ParseSeccompArgs(*syscall_dict, &this_syscall))
525 return false;
526 seccomp_out->syscalls.push_back(this_syscall);
527 }
528
529 return true;
530}
531
Dylan Reid6b590e62016-10-27 19:10:53 -0700532// Parses the linux node which has information about setting up a user
533// namespace, and the list of devices for the container.
534bool ParseLinuxConfigDict(const base::DictionaryValue& runtime_root_dict,
535 OciConfigPtr const& config_out) {
536 // |linux_dict| is owned by |runtime_root_dict|
537 const base::DictionaryValue* linux_dict = nullptr;
538 if (!runtime_root_dict.GetDictionary("linux", &linux_dict)) {
539 LOG(ERROR) << "Fail to get linux dictionary from the runtime dictionary";
540 return false;
541 }
542
543 // |uid_map_list| is owned by |linux_dict|
544 const base::ListValue* uid_map_list = nullptr;
545 if (!linux_dict->GetList("uidMappings", &uid_map_list)) {
546 LOG(ERROR) << "Fail to get uid mappings list";
547 return false;
548 }
Ben Chanf43980b2017-03-10 11:31:46 -0800549 ParseLinuxIdMappings(uid_map_list, &config_out->linux_config.uidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700550
551 // |gid_map_list| is owned by |linux_dict|
552 const base::ListValue* gid_map_list = nullptr;
553 if (!linux_dict->GetList("gidMappings", &gid_map_list)) {
554 LOG(ERROR) << "Fail to get gid mappings list";
555 return false;
556 }
Ben Chanf43980b2017-03-10 11:31:46 -0800557 ParseLinuxIdMappings(gid_map_list, &config_out->linux_config.gidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700558
559 if (!ParseDeviceList(*linux_dict, config_out))
560 return false;
561
Dylan Reid6985e3b2017-03-31 19:39:16 -0700562 const base::DictionaryValue* resources_dict = nullptr;
563 if (linux_dict->GetDictionary("resources", &resources_dict)) {
564 if (!ParseResources(*resources_dict, &config_out->linux_config.resources))
565 return false;
566 }
567
Stephen Barber3c0a2022017-09-08 14:17:57 -0700568 const base::ListValue* namespaces_list = nullptr;
569 if (linux_dict->GetList("namespaces", &namespaces_list)) {
570 if (!ParseNamespaces(namespaces_list, &config_out->linux_config.namespaces))
571 return false;
572 }
573
Dylan Reidb0a72772016-11-03 16:27:50 +0000574 const base::DictionaryValue* seccomp_dict = nullptr;
575 if (linux_dict->GetDictionary("seccomp", &seccomp_dict)) {
576 if (!ParseSeccompInfo(*seccomp_dict, &config_out->linux_config.seccomp))
577 return false;
578 }
579
Dylan Reid6b590e62016-10-27 19:10:53 -0700580 return true;
581}
582
Dylan Reid45e34fe2016-12-02 15:11:53 -0800583bool HostnameValid(const std::string& hostname) {
584 if (hostname.length() > 255)
585 return false;
586
587 const std::regex name("^[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?$");
588 if (!std::regex_match(hostname, name))
589 return false;
590
591 const std::regex double_dash("--");
592 if (std::regex_match(hostname, double_dash))
593 return false;
594
595 return true;
596}
597
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700598bool ParseHooksList(const base::ListValue& hooks_list,
599 std::vector<OciHook>* hooks_out,
600 const std::string& hook_type) {
601 size_t num_hooks = hooks_list.GetSize();
602 for (size_t i = 0; i < num_hooks; ++i) {
603 OciHook hook;
604 const base::DictionaryValue* hook_dict;
605 if (!hooks_list.GetDictionary(i, &hook_dict)) {
606 LOG(ERROR) << "Fail to get " << hook_type << " hook item " << i;
607 return false;
608 }
609
610 if (!hook_dict->GetString("path", &hook.path)) {
611 LOG(ERROR) << "Fail to get path of " << hook_type << " hook " << i;
612 return false;
613 }
614
615 const base::ListValue* hook_args;
616 // args are optional.
617 if (hook_dict->GetList("args", &hook_args)) {
618 size_t num_args = hook_args->GetSize();
619 for (size_t j = 0; j < num_args; ++j) {
620 std::string arg;
621 if (!hook_args->GetString(j, &arg)) {
622 LOG(ERROR) << "Fail to get arg " << j << " of " << hook_type
623 << " hook " << i;
624 return false;
625 }
626 hook.args.push_back(arg);
627 }
628 }
629
630 const base::ListValue* hook_envs;
631 // envs are optional.
632 if (hook_dict->GetList("env", &hook_envs)) {
633 size_t num_env = hook_envs->GetSize();
634 for (size_t j = 0; j < num_env; ++j) {
635 std::string env;
636 if (!hook_envs->GetString(j, &env)) {
637 LOG(ERROR) << "Fail to get env " << j << " of " << hook_type
638 << " hook " << i;
639 return false;
640 }
641 std::vector<std::string> kvp = base::SplitString(
642 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
643 if (kvp.size() != 2) {
644 LOG(ERROR) << "Fail to parse env \"" << env
645 << "\". Must be in name=value format.";
646 return false;
647 }
648 hook.env.insert(std::make_pair(kvp[0], kvp[1]));
649 }
650 }
651
652 int timeout_seconds;
653 // timeout is optional.
654 if (hook_dict->GetInteger("timeout", &timeout_seconds)) {
655 hook.timeout = base::TimeDelta::FromSeconds(timeout_seconds);
656 } else {
657 hook.timeout = base::TimeDelta::Max();
658 }
659
660 hooks_out->emplace_back(std::move(hook));
661 }
662 return true;
663}
664
665bool ParseHooks(const base::DictionaryValue& config_root_dict,
666 OciConfigPtr const& config_out) {
667 const base::DictionaryValue* hooks_config_dict;
668 if (!config_root_dict.GetDictionary("hooks", &hooks_config_dict)) {
669 // Hooks are optional.
670 return true;
671 }
672
673 const base::ListValue* hooks_list;
674 if (hooks_config_dict->GetList("prestart", &hooks_list)) {
675 if (!ParseHooksList(*hooks_list, &config_out->pre_start_hooks, "prestart"))
676 return false;
677 }
678 if (hooks_config_dict->GetList("poststart", &hooks_list)) {
679 if (!ParseHooksList(
680 *hooks_list, &config_out->post_start_hooks, "poststart"))
681 return false;
682 }
683 if (hooks_config_dict->GetList("poststop", &hooks_list)) {
684 if (!ParseHooksList(*hooks_list, &config_out->post_stop_hooks, "poststop"))
685 return false;
686 }
687 return true;
688}
689
Dylan Reid6b590e62016-10-27 19:10:53 -0700690// Parses the configuration file for the container. The config file specifies
691// basic filesystem info and details about the process to be run. namespace,
692// cgroup, and syscall configurations are also specified
693bool ParseConfigDict(const base::DictionaryValue& config_root_dict,
694 OciConfigPtr const& config_out) {
Dylan Reid6d650742016-11-02 23:10:38 -0700695 if (!config_root_dict.GetString("ociVersion", &config_out->ociVersion)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700696 LOG(ERROR) << "Failed to parse ociVersion";
697 return false;
698 }
699 if (!config_root_dict.GetString("hostname", &config_out->hostname)) {
700 LOG(ERROR) << "Failed to parse hostname";
701 return false;
702 }
Dylan Reid45e34fe2016-12-02 15:11:53 -0800703 if (!HostnameValid(config_out->hostname)) {
704 LOG(ERROR) << "Invalid hostname " << config_out->hostname;
705 return false;
706 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700707
708 // Platform info
709 if (!ParsePlatformConfig(config_root_dict, config_out)) {
710 return false;
711 }
712
713 // Root fs info
714 if (!ParseRootFileSystemConfig(config_root_dict, config_out)) {
715 return false;
716 }
717
718 // Process info
719 if (!ParseProcessConfig(config_root_dict, config_out)) {
720 return false;
721 }
722
723 // Get a list of mount points and mounts.
724 if (!ParseMounts(config_root_dict, config_out)) {
725 LOG(ERROR) << "Failed to parse mounts";
726 return false;
727 }
728
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700729 // Hooks info
730 if (!ParseHooks(config_root_dict, config_out)) {
731 return false;
732 }
733
Dylan Reid6b590e62016-10-27 19:10:53 -0700734 // Parse linux node.
735 if (!ParseLinuxConfigDict(config_root_dict, config_out)) {
736 LOG(ERROR) << "Failed to parse the linux node";
737 return false;
738 }
739
740 return true;
741}
742
Ben Chanf43980b2017-03-10 11:31:46 -0800743} // anonymous namespace
Dylan Reid6b590e62016-10-27 19:10:53 -0700744
745bool ParseContainerConfig(const std::string& config_json_data,
746 OciConfigPtr const& config_out) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700747 std::string error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700748 std::unique_ptr<const base::Value> config_root_val =
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700749 base::JSONReader::ReadAndReturnError(config_json_data,
750 base::JSON_PARSE_RFC,
751 nullptr /* error_code_out */,
752 &error_msg,
753 nullptr /* error_line_out */,
754 nullptr /* error_column_out */);
Dylan Reid6b590e62016-10-27 19:10:53 -0700755 if (!config_root_val) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700756 LOG(ERROR) << "Fail to parse config.json: " << error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700757 return false;
758 }
759 const base::DictionaryValue* config_dict = nullptr;
760 if (!config_root_val->GetAsDictionary(&config_dict)) {
761 LOG(ERROR) << "Fail to parse root dictionary from config.json";
762 return false;
763 }
764 if (!ParseConfigDict(*config_dict, config_out)) {
765 return false;
766 }
767
768 return true;
769}
770
Stephen Barber5f6dc9b2017-04-04 12:36:32 -0700771} // namespace run_oci