blob: 19c63fe0c0331dc633ceb3307958ac4b4c0d32a1 [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.
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -070026bool ParseUint32FromDict(const base::DictionaryValue& dict,
27 const char* name,
Dylan Reidc0c28502016-11-04 10:51:30 -070028 uint32_t* val_out) {
29 double double_val;
30 if (!dict.GetDouble(name, &double_val)) {
Dylan Reidc0c28502016-11-04 10:51:30 -070031 return false;
32 }
33 *val_out = double_val;
34 return true;
35}
36
Dylan Reidb0a72772016-11-03 16:27:50 +000037// Gets a uint64 from the given dictionary.
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -070038bool ParseUint64FromDict(const base::DictionaryValue& dict,
39 const char* name,
Dylan Reidb0a72772016-11-03 16:27:50 +000040 uint64_t* val_out) {
41 double double_val;
42 if (!dict.GetDouble(name, &double_val)) {
Dylan Reidb0a72772016-11-03 16:27:50 +000043 return false;
44 }
45 *val_out = double_val;
46 return true;
47}
48
Dylan Reid6b590e62016-10-27 19:10:53 -070049// Parses basic platform configuration.
50bool ParsePlatformConfig(const base::DictionaryValue& config_root_dict,
51 OciConfigPtr const& config_out) {
52 // |platform_dict| stays owned by |config_root_dict|
53 const base::DictionaryValue* platform_dict = nullptr;
54 if (!config_root_dict.GetDictionary("platform", &platform_dict)) {
55 LOG(ERROR) << "Fail to parse platform dictionary from config";
56 return false;
57 }
58
59 if (!platform_dict->GetString("os", &config_out->platform.os)) {
60 return false;
61 }
62
63 if (!platform_dict->GetString("arch", &config_out->platform.arch)) {
64 return false;
65 }
66
67 return true;
68}
69
70// Parses root fs info.
71bool ParseRootFileSystemConfig(const base::DictionaryValue& config_root_dict,
72 OciConfigPtr const& config_out) {
73 // |rootfs_dict| stays owned by |config_root_dict|
74 const base::DictionaryValue* rootfs_dict = nullptr;
75 if (!config_root_dict.GetDictionary("root", &rootfs_dict)) {
76 LOG(ERROR) << "Fail to parse rootfs dictionary from config";
77 return false;
78 }
Luis Hector Chavez2a90f292017-10-02 09:51:28 -070079 std::string path;
80 if (!rootfs_dict->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -070081 LOG(ERROR) << "Fail to get rootfs path from config";
82 return false;
83 }
Luis Hector Chavez2a90f292017-10-02 09:51:28 -070084 config_out->root.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -070085 rootfs_dict->GetBoolean("readonly", &config_out->root.readonly);
Dylan Reid6b590e62016-10-27 19:10:53 -070086 return true;
87}
88
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070089// Fills |config_out| with information about the capability sets in the
90// container.
91bool ParseCapabilitiesConfig(const base::DictionaryValue& capabilities_dict,
92 std::map<std::string, CapSet>* config_out) {
93 constexpr const char* kCapabilitySetNames[] = {
94 "effective", "bounding", "inheritable", "permitted", "ambient"};
95 const std::string kAmbientCapabilitySetName = "ambient";
96
97 CapSet caps_superset;
98 for (const char* set_name : kCapabilitySetNames) {
99 // |capset_list| stays owned by |capabilities_dict|.
100 const base::ListValue* capset_list = nullptr;
101 if (!capabilities_dict.GetList(set_name, &capset_list))
102 continue;
103 CapSet caps;
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700104 cap_value_t cap_value;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700105 for (const auto* cap_name_value : *capset_list) {
106 std::string cap_name;
107 if (!cap_name_value->GetAsString(&cap_name)) {
108 LOG(ERROR) << "Capability list " << set_name
109 << " contains a non-string";
110 return false;
111 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700112 if (cap_from_name(cap_name.c_str(), &cap_value) == -1) {
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700113 LOG(ERROR) << "Unrecognized capability name: " << cap_name;
114 return false;
115 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700116 caps[cap_value] = true;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700117 }
118 (*config_out)[set_name] = caps;
119 caps_superset = caps;
120 }
121
122 // We currently only support sets that are identical, except that ambient is
123 // optional.
124 for (const char* set_name : kCapabilitySetNames) {
125 auto it = config_out->find(set_name);
126 if (it == config_out->end() && set_name == kAmbientCapabilitySetName) {
127 // Ambient capabilities are optional.
128 continue;
129 }
130 if (it == config_out->end()) {
131 LOG(ERROR)
132 << "If capabilities are set, all capability sets should be present";
133 return false;
134 }
135 if (it->second != caps_superset) {
136 LOG(ERROR)
137 << "If capabilities are set, all capability sets should be identical";
138 return false;
139 }
140 }
141
142 return true;
143}
144
Dylan Reid93fa4602017-06-06 13:39:31 -0700145const std::map<std::string, int> kRlimitMap = {
146#define RLIMIT_MAP_ENTRY(limit) \
147 { "RLIMIT_" #limit, RLIMIT_##limit }
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700148 RLIMIT_MAP_ENTRY(CPU), RLIMIT_MAP_ENTRY(FSIZE),
149 RLIMIT_MAP_ENTRY(DATA), RLIMIT_MAP_ENTRY(STACK),
150 RLIMIT_MAP_ENTRY(CORE), RLIMIT_MAP_ENTRY(RSS),
151 RLIMIT_MAP_ENTRY(NPROC), RLIMIT_MAP_ENTRY(NOFILE),
152 RLIMIT_MAP_ENTRY(MEMLOCK), RLIMIT_MAP_ENTRY(AS),
153 RLIMIT_MAP_ENTRY(LOCKS), RLIMIT_MAP_ENTRY(SIGPENDING),
154 RLIMIT_MAP_ENTRY(MSGQUEUE), RLIMIT_MAP_ENTRY(NICE),
155 RLIMIT_MAP_ENTRY(RTPRIO), RLIMIT_MAP_ENTRY(RTTIME),
Dylan Reid93fa4602017-06-06 13:39:31 -0700156#undef RLIMIT_MAP_ENTRY
157};
158
159// Fills |config_out| with information about the capability sets in the
160// container.
161bool ParseRlimitsConfig(const base::ListValue& rlimits_list,
162 std::vector<OciProcessRlimit>* rlimits_out) {
163 size_t num_limits = rlimits_list.GetSize();
164 for (size_t i = 0; i < num_limits; ++i) {
165 const base::DictionaryValue* rlimits_dict;
166 if (!rlimits_list.GetDictionary(i, &rlimits_dict)) {
167 LOG(ERROR) << "Fail to get rlimit item " << i;
168 return false;
169 }
170
171 std::string rlimit_name;
172 if (!rlimits_dict->GetString("type", &rlimit_name)) {
173 LOG(ERROR) << "Fail to get type of rlimit " << i;
174 return false;
175 }
176 const auto it = kRlimitMap.find(rlimit_name);
177 if (it == kRlimitMap.end()) {
178 LOG(ERROR) << "Unrecognized rlimit name: " << rlimit_name;
179 return false;
180 }
181
182 OciProcessRlimit limit;
183 limit.type = it->second;
184 if (!ParseUint32FromDict(*rlimits_dict, "hard", &limit.hard)) {
185 LOG(ERROR) << "Fail to get hard limit of rlimit " << i;
186 return false;
187 }
188 if (!ParseUint32FromDict(*rlimits_dict, "soft", &limit.soft)) {
189 LOG(ERROR) << "Fail to get soft limit of rlimit " << i;
190 return false;
191 }
192 rlimits_out->push_back(limit);
193 }
194
195 return true;
196}
197
Dylan Reid6b590e62016-10-27 19:10:53 -0700198// Fills |config_out| with information about the main process to run in the
199// container and the user it should be run as.
200bool ParseProcessConfig(const base::DictionaryValue& config_root_dict,
201 OciConfigPtr const& config_out) {
202 // |process_dict| stays owned by |config_root_dict|
203 const base::DictionaryValue* process_dict = nullptr;
204 if (!config_root_dict.GetDictionary("process", &process_dict)) {
205 LOG(ERROR) << "Fail to get main process from config";
206 return false;
207 }
208 process_dict->GetBoolean("terminal", &config_out->process.terminal);
209 // |user_dict| stays owned by |process_dict|
210 const base::DictionaryValue* user_dict = nullptr;
211 if (!process_dict->GetDictionary("user", &user_dict)) {
212 LOG(ERROR) << "Failed to get user info from config";
213 return false;
214 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700215 if (!ParseUint32FromDict(*user_dict, "uid", &config_out->process.user.uid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700216 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700217 if (!ParseUint32FromDict(*user_dict, "gid", &config_out->process.user.gid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700218 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700219 // |args_list| stays owned by |process_dict|
220 const base::ListValue* args_list = nullptr;
221 if (!process_dict->GetList("args", &args_list)) {
222 LOG(ERROR) << "Fail to get main process args from config";
223 return false;
224 }
225 size_t num_args = args_list->GetSize();
226 for (size_t i = 0; i < num_args; ++i) {
227 std::string arg;
228 if (!args_list->GetString(i, &arg)) {
229 LOG(ERROR) << "Fail to get process args from config";
230 return false;
231 }
232 config_out->process.args.push_back(arg);
233 }
234 // |env_list| stays owned by |process_dict|
235 const base::ListValue* env_list = nullptr;
236 if (process_dict->GetList("env", &env_list)) {
237 size_t num_env = env_list->GetSize();
238 for (size_t i = 0; i < num_env; ++i) {
239 std::string env;
240 if (!env_list->GetString(i, &env)) {
241 LOG(ERROR) << "Fail to get process env from config";
242 return false;
243 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700244 std::vector<std::string> kvp = base::SplitString(
245 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
246 if (kvp.size() != 2) {
247 LOG(ERROR) << "Fail to parse env \"" << env
248 << "\". Must be in name=value format.";
249 return false;
250 }
251 config_out->process.env.insert(std::make_pair(kvp[0], kvp[1]));
Dylan Reid6b590e62016-10-27 19:10:53 -0700252 }
253 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700254 std::string path;
255 if (!process_dict->GetString("cwd", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700256 LOG(ERROR) << "failed to get cwd of process";
257 return false;
258 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700259 config_out->process.cwd = base::FilePath(path);
260
Luis Hector Chavez15e8e672017-07-20 15:13:27 -0700261 // selinuxLabel is optional.
262 process_dict->GetString("selinuxLabel", &config_out->process.selinuxLabel);
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700263 // |capabilities_dict| stays owned by |process_dict|
264 const base::DictionaryValue* capabilities_dict = nullptr;
265 if (process_dict->GetDictionary("capabilities", &capabilities_dict)) {
266 if (!ParseCapabilitiesConfig(*capabilities_dict,
267 &config_out->process.capabilities)) {
268 return false;
269 }
270 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700271
Dylan Reid93fa4602017-06-06 13:39:31 -0700272 // |rlimit_list| stays owned by |process_dict|
273 const base::ListValue* rlimits_list = nullptr;
274 if (process_dict->GetList("rlimits", &rlimits_list)) {
275 if (!ParseRlimitsConfig(*rlimits_list, &config_out->process.rlimits)) {
276 return false;
277 }
278 }
279
Dylan Reid6b590e62016-10-27 19:10:53 -0700280 return true;
281}
282
283// Parses the 'mounts' field. The necessary mounts for running the container
284// are specified here.
285bool ParseMounts(const base::DictionaryValue& config_root_dict,
286 OciConfigPtr const& config_out) {
287 // |config_mounts_list| stays owned by |config_root_dict|
288 const base::ListValue* config_mounts_list = nullptr;
289 if (!config_root_dict.GetList("mounts", &config_mounts_list)) {
290 LOG(ERROR) << "Fail to get mounts from config dictionary";
291 return false;
292 }
293
294 for (size_t i = 0; i < config_mounts_list->GetSize(); ++i) {
295 const base::DictionaryValue* mount_dict;
296 if (!config_mounts_list->GetDictionary(i, &mount_dict)) {
297 LOG(ERROR) << "Fail to get mount item " << i;
298 return false;
299 }
300 OciMount mount;
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700301 std::string path;
302 if (!mount_dict->GetString("destination", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700303 LOG(ERROR) << "Fail to get mount path for mount " << i;
304 return false;
305 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700306 mount.destination = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700307 if (!mount_dict->GetString("type", &mount.type)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700308 LOG(ERROR) << "Fail to get mount type for mount " << i;
309 return false;
310 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700311 if (!mount_dict->GetString("source", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700312 LOG(ERROR) << "Fail to get mount source for mount " << i;
313 return false;
314 }
Luis Hector Chavez60f9bb12017-10-16 11:33:01 -0700315 if (!mount_dict->GetBoolean("performInIntermediateNamespace",
316 &mount.perform_in_intermediate_namespace)) {
317 mount.perform_in_intermediate_namespace = false; // Optional
318 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700319 mount.source = base::FilePath(path);
Dylan Reid6b590e62016-10-27 19:10:53 -0700320
321 // |options| are owned by |mount_dict|
322 const base::ListValue* options = nullptr;
323 if (mount_dict->GetList("options", &options)) {
324 for (size_t j = 0; j < options->GetSize(); ++j) {
325 std::string this_opt;
326 if (!options->GetString(j, &this_opt)) {
327 LOG(ERROR) << "Fail to get option " << j << " from mount options";
328 return false;
329 }
330 mount.options.push_back(this_opt);
331 }
332 }
333
334 config_out->mounts.push_back(mount);
335 }
336 return true;
337}
338
Dylan Reid6985e3b2017-03-31 19:39:16 -0700339// Parses the linux resource list
340bool ParseResources(const base::DictionaryValue& resources_dict,
341 OciLinuxResources* resources_out) {
342 // |device_list| is owned by |resources_dict|
343 const base::ListValue* device_list = nullptr;
344 if (!resources_dict.GetList("devices", &device_list)) {
345 // The device list is optional.
346 return true;
347 }
348 size_t num_devices = device_list->GetSize();
349 for (size_t i = 0; i < num_devices; ++i) {
350 OciLinuxCgroupDevice device;
351
352 const base::DictionaryValue* dev;
353 if (!device_list->GetDictionary(i, &dev)) {
354 LOG(ERROR) << "Fail to get device " << i;
355 return false;
356 }
357
358 if (!dev->GetBoolean("allow", &device.allow)) {
359 LOG(ERROR) << "Fail to get allow value for device " << i;
360 return false;
361 }
362 if (!dev->GetString("access", &device.access))
363 device.access = "rwm"; // Optional, default to all perms.
364 if (!dev->GetString("type", &device.type))
365 device.type = "a"; // Optional, default to both a means all.
366 if (!ParseUint32FromDict(*dev, "major", &device.major))
367 device.major = -1; // Optional, -1 will map to all devices.
368 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
369 device.minor = -1; // Optional, -1 will map to all devices.
370
371 resources_out->devices.push_back(device);
372 }
373
374 return true;
375}
376
Stephen Barber3c0a2022017-09-08 14:17:57 -0700377// Parses the list of namespaces and fills |namespaces_out| with them.
378bool ParseNamespaces(const base::ListValue* namespaces_list,
379 std::vector<OciNamespace>* namespaces_out) {
380 for (size_t i = 0; i < namespaces_list->GetSize(); ++i) {
381 OciNamespace new_namespace;
382 const base::DictionaryValue* ns;
383 if (!namespaces_list->GetDictionary(i, &ns)) {
384 LOG(ERROR) << "Failed to get namespace " << i;
385 return false;
386 }
387 if (!ns->GetString("type", &new_namespace.type)) {
388 LOG(ERROR) << "Namespace " << i << " missing type";
389 return false;
390 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700391 std::string path;
392 if (ns->GetString("path", &path))
393 new_namespace.path = base::FilePath(path);
Stephen Barber3c0a2022017-09-08 14:17:57 -0700394 namespaces_out->push_back(new_namespace);
395 }
396 return true;
397}
398
Dylan Reid6b590e62016-10-27 19:10:53 -0700399// Parse the list of device nodes that the container needs to run.
400bool ParseDeviceList(const base::DictionaryValue& linux_dict,
401 OciConfigPtr const& config_out) {
402 // |device_list| is owned by |linux_dict|
403 const base::ListValue* device_list = nullptr;
404 if (!linux_dict.GetList("devices", &device_list)) {
Dylan Reida5ed1272016-11-11 16:43:39 -0800405 // The device list is optional.
406 return true;
Dylan Reid6b590e62016-10-27 19:10:53 -0700407 }
408 size_t num_devices = device_list->GetSize();
409 for (size_t i = 0; i < num_devices; ++i) {
410 OciLinuxDevice device;
411
412 const base::DictionaryValue* dev;
413 if (!device_list->GetDictionary(i, &dev)) {
414 LOG(ERROR) << "Fail to get device " << i;
415 return false;
416 }
417 std::string path;
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700418 if (!dev->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700419 LOG(ERROR) << "Fail to get path for dev";
420 return false;
421 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700422 device.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700423 if (!dev->GetString("type", &device.type)) {
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700424 LOG(ERROR) << "Fail to get type for " << device.path.value();
Dylan Reid6b590e62016-10-27 19:10:53 -0700425 return false;
426 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700427 if (!ParseUint32FromDict(*dev, "major", &device.major))
428 return false;
Luis Hector Chavezac20fc52017-10-10 11:08:41 -0700429 dev->GetBoolean("dynamicMinor", &device.dynamicMinor);
430 if (device.dynamicMinor) {
431 if (dev->HasKey("minor")) {
432 LOG(WARNING)
433 << "Ignoring \"minor\" since \"dynamicMinor\" is specified for "
434 << device.path.value();
435 }
436 } else {
437 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
438 return false;
439 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700440 if (!ParseUint32FromDict(*dev, "fileMode", &device.fileMode))
441 return false;
442 if (!ParseUint32FromDict(*dev, "uid", &device.uid))
443 return false;
444 if (!ParseUint32FromDict(*dev, "gid", &device.gid))
445 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700446
447 config_out->linux_config.devices.push_back(device);
448 }
449
450 return true;
451}
452
453// Parses the list of ID mappings and fills |mappings_out| with them.
454bool ParseLinuxIdMappings(const base::ListValue* id_map_list,
Ben Chanf43980b2017-03-10 11:31:46 -0800455 std::vector<OciLinuxNamespaceMapping>* mappings_out) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700456 for (size_t i = 0; i < id_map_list->GetSize(); ++i) {
457 OciLinuxNamespaceMapping new_map;
458 const base::DictionaryValue* map;
459 if (!id_map_list->GetDictionary(i, &map)) {
460 LOG(ERROR) << "Fail to get id map " << i;
461 return false;
462 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700463 if (!ParseUint32FromDict(*map, "hostID", &new_map.hostID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700464 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700465 if (!ParseUint32FromDict(*map, "containerID", &new_map.containerID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700466 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700467 if (!ParseUint32FromDict(*map, "size", &new_map.size))
Dylan Reid6b590e62016-10-27 19:10:53 -0700468 return false;
Ben Chanf43980b2017-03-10 11:31:46 -0800469 mappings_out->push_back(new_map);
Dylan Reid6b590e62016-10-27 19:10:53 -0700470 }
471 return true;
472}
473
Dylan Reidb0a72772016-11-03 16:27:50 +0000474// Parses seccomp syscall args.
475bool ParseSeccompArgs(const base::DictionaryValue& syscall_dict,
476 OciSeccompSyscall* syscall_out) {
477 const base::ListValue* args = nullptr;
478 if (syscall_dict.GetList("args", &args)) {
479 for (size_t i = 0; i < args->GetSize(); ++i) {
480 const base::DictionaryValue* args_dict = nullptr;
481 if (!args->GetDictionary(i, &args_dict)) {
482 LOG(ERROR) << "Failed to pars args dict for " << syscall_out->name;
483 return false;
484 }
485 OciSeccompArg this_arg;
486 if (!ParseUint32FromDict(*args_dict, "index", &this_arg.index))
487 return false;
488 if (!ParseUint64FromDict(*args_dict, "value", &this_arg.value))
489 return false;
490 if (!ParseUint64FromDict(*args_dict, "value2", &this_arg.value2))
491 return false;
492 if (!args_dict->GetString("op", &this_arg.op)) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700493 LOG(ERROR) << "Failed to parse op for arg " << this_arg.index << " of "
494 << syscall_out->name;
Dylan Reidb0a72772016-11-03 16:27:50 +0000495 return false;
496 }
497 syscall_out->args.push_back(this_arg);
498 }
499 }
500 return true;
501}
502
503// Parses the seccomp node if it is present.
504bool ParseSeccompInfo(const base::DictionaryValue& seccomp_dict,
505 OciSeccomp* seccomp_out) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700506 if (!seccomp_dict.GetString("defaultAction", &seccomp_out->defaultAction))
Dylan Reidb0a72772016-11-03 16:27:50 +0000507 return false;
508
509 // Gets the list of architectures.
510 const base::ListValue* architectures = nullptr;
511 if (!seccomp_dict.GetList("architectures", &architectures)) {
512 LOG(ERROR) << "Fail to read seccomp architectures";
513 return false;
514 }
515 for (size_t i = 0; i < architectures->GetSize(); ++i) {
516 std::string this_arch;
517 if (!architectures->GetString(i, &this_arch)) {
518 LOG(ERROR) << "Fail to parse seccomp architecture list";
519 return false;
520 }
521 seccomp_out->architectures.push_back(this_arch);
522 }
523
524 // Gets the list of syscalls.
525 const base::ListValue* syscalls = nullptr;
526 if (!seccomp_dict.GetList("syscalls", &syscalls)) {
527 LOG(ERROR) << "Fail to read seccomp syscalls";
528 return false;
529 }
530 for (size_t i = 0; i < syscalls->GetSize(); ++i) {
531 const base::DictionaryValue* syscall_dict = nullptr;
532 if (!syscalls->GetDictionary(i, &syscall_dict)) {
533 LOG(ERROR) << "Fail to parse seccomp syscalls list";
534 return false;
535 }
536 OciSeccompSyscall this_syscall;
537 if (!syscall_dict->GetString("name", &this_syscall.name)) {
538 LOG(ERROR) << "Fail to parse syscall name " << i;
539 return false;
540 }
541 if (!syscall_dict->GetString("action", &this_syscall.action)) {
542 LOG(ERROR) << "Fail to parse syscall action for " << this_syscall.name;
543 return false;
544 }
545 if (!ParseSeccompArgs(*syscall_dict, &this_syscall))
546 return false;
547 seccomp_out->syscalls.push_back(this_syscall);
548 }
549
550 return true;
551}
552
Dylan Reid6b590e62016-10-27 19:10:53 -0700553// Parses the linux node which has information about setting up a user
554// namespace, and the list of devices for the container.
555bool ParseLinuxConfigDict(const base::DictionaryValue& runtime_root_dict,
556 OciConfigPtr const& config_out) {
557 // |linux_dict| is owned by |runtime_root_dict|
558 const base::DictionaryValue* linux_dict = nullptr;
559 if (!runtime_root_dict.GetDictionary("linux", &linux_dict)) {
560 LOG(ERROR) << "Fail to get linux dictionary from the runtime dictionary";
561 return false;
562 }
563
564 // |uid_map_list| is owned by |linux_dict|
565 const base::ListValue* uid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700566 if (linux_dict->GetList("uidMappings", &uid_map_list))
567 ParseLinuxIdMappings(uid_map_list, &config_out->linux_config.uidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700568
569 // |gid_map_list| is owned by |linux_dict|
570 const base::ListValue* gid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700571 if (linux_dict->GetList("gidMappings", &gid_map_list))
572 ParseLinuxIdMappings(gid_map_list, &config_out->linux_config.gidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700573
574 if (!ParseDeviceList(*linux_dict, config_out))
575 return false;
576
Dylan Reid6985e3b2017-03-31 19:39:16 -0700577 const base::DictionaryValue* resources_dict = nullptr;
578 if (linux_dict->GetDictionary("resources", &resources_dict)) {
579 if (!ParseResources(*resources_dict, &config_out->linux_config.resources))
580 return false;
581 }
582
Stephen Barber3c0a2022017-09-08 14:17:57 -0700583 const base::ListValue* namespaces_list = nullptr;
584 if (linux_dict->GetList("namespaces", &namespaces_list)) {
585 if (!ParseNamespaces(namespaces_list, &config_out->linux_config.namespaces))
586 return false;
587 }
588
Dylan Reidb0a72772016-11-03 16:27:50 +0000589 const base::DictionaryValue* seccomp_dict = nullptr;
590 if (linux_dict->GetDictionary("seccomp", &seccomp_dict)) {
591 if (!ParseSeccompInfo(*seccomp_dict, &config_out->linux_config.seccomp))
592 return false;
593 }
594
Dylan Reid6b590e62016-10-27 19:10:53 -0700595 return true;
596}
597
Dylan Reid45e34fe2016-12-02 15:11:53 -0800598bool HostnameValid(const std::string& hostname) {
599 if (hostname.length() > 255)
600 return false;
601
602 const std::regex name("^[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?$");
603 if (!std::regex_match(hostname, name))
604 return false;
605
606 const std::regex double_dash("--");
607 if (std::regex_match(hostname, double_dash))
608 return false;
609
610 return true;
611}
612
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700613bool ParseHooksList(const base::ListValue& hooks_list,
614 std::vector<OciHook>* hooks_out,
615 const std::string& hook_type) {
616 size_t num_hooks = hooks_list.GetSize();
617 for (size_t i = 0; i < num_hooks; ++i) {
618 OciHook hook;
619 const base::DictionaryValue* hook_dict;
620 if (!hooks_list.GetDictionary(i, &hook_dict)) {
621 LOG(ERROR) << "Fail to get " << hook_type << " hook item " << i;
622 return false;
623 }
624
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700625 std::string path;
626 if (!hook_dict->GetString("path", &path)) {
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700627 LOG(ERROR) << "Fail to get path of " << hook_type << " hook " << i;
628 return false;
629 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700630 hook.path = base::FilePath(path);
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700631
632 const base::ListValue* hook_args;
633 // args are optional.
634 if (hook_dict->GetList("args", &hook_args)) {
635 size_t num_args = hook_args->GetSize();
636 for (size_t j = 0; j < num_args; ++j) {
637 std::string arg;
638 if (!hook_args->GetString(j, &arg)) {
639 LOG(ERROR) << "Fail to get arg " << j << " of " << hook_type
640 << " hook " << i;
641 return false;
642 }
643 hook.args.push_back(arg);
644 }
645 }
646
647 const base::ListValue* hook_envs;
648 // envs are optional.
649 if (hook_dict->GetList("env", &hook_envs)) {
650 size_t num_env = hook_envs->GetSize();
651 for (size_t j = 0; j < num_env; ++j) {
652 std::string env;
653 if (!hook_envs->GetString(j, &env)) {
654 LOG(ERROR) << "Fail to get env " << j << " of " << hook_type
655 << " hook " << i;
656 return false;
657 }
658 std::vector<std::string> kvp = base::SplitString(
659 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
660 if (kvp.size() != 2) {
661 LOG(ERROR) << "Fail to parse env \"" << env
662 << "\". Must be in name=value format.";
663 return false;
664 }
665 hook.env.insert(std::make_pair(kvp[0], kvp[1]));
666 }
667 }
668
669 int timeout_seconds;
670 // timeout is optional.
671 if (hook_dict->GetInteger("timeout", &timeout_seconds)) {
672 hook.timeout = base::TimeDelta::FromSeconds(timeout_seconds);
673 } else {
674 hook.timeout = base::TimeDelta::Max();
675 }
676
677 hooks_out->emplace_back(std::move(hook));
678 }
679 return true;
680}
681
682bool ParseHooks(const base::DictionaryValue& config_root_dict,
683 OciConfigPtr const& config_out) {
684 const base::DictionaryValue* hooks_config_dict;
685 if (!config_root_dict.GetDictionary("hooks", &hooks_config_dict)) {
686 // Hooks are optional.
687 return true;
688 }
689
690 const base::ListValue* hooks_list;
Luis Hector Chavezbb515a02017-09-29 15:44:35 -0700691 if (hooks_config_dict->GetList("prechroot", &hooks_list)) {
692 if (!ParseHooksList(*hooks_list, &config_out->pre_chroot_hooks,
693 "prechroot")) {
694 return false;
695 }
696 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700697 if (hooks_config_dict->GetList("prestart", &hooks_list)) {
698 if (!ParseHooksList(*hooks_list, &config_out->pre_start_hooks, "prestart"))
699 return false;
700 }
701 if (hooks_config_dict->GetList("poststart", &hooks_list)) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700702 if (!ParseHooksList(*hooks_list, &config_out->post_start_hooks,
703 "poststart"))
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700704 return false;
705 }
706 if (hooks_config_dict->GetList("poststop", &hooks_list)) {
707 if (!ParseHooksList(*hooks_list, &config_out->post_stop_hooks, "poststop"))
708 return false;
709 }
710 return true;
711}
712
Dylan Reid6b590e62016-10-27 19:10:53 -0700713// Parses the configuration file for the container. The config file specifies
714// basic filesystem info and details about the process to be run. namespace,
715// cgroup, and syscall configurations are also specified
716bool ParseConfigDict(const base::DictionaryValue& config_root_dict,
717 OciConfigPtr const& config_out) {
Dylan Reid6d650742016-11-02 23:10:38 -0700718 if (!config_root_dict.GetString("ociVersion", &config_out->ociVersion)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700719 LOG(ERROR) << "Failed to parse ociVersion";
720 return false;
721 }
722 if (!config_root_dict.GetString("hostname", &config_out->hostname)) {
723 LOG(ERROR) << "Failed to parse hostname";
724 return false;
725 }
Dylan Reid45e34fe2016-12-02 15:11:53 -0800726 if (!HostnameValid(config_out->hostname)) {
727 LOG(ERROR) << "Invalid hostname " << config_out->hostname;
728 return false;
729 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700730
731 // Platform info
732 if (!ParsePlatformConfig(config_root_dict, config_out)) {
733 return false;
734 }
735
736 // Root fs info
737 if (!ParseRootFileSystemConfig(config_root_dict, config_out)) {
738 return false;
739 }
740
741 // Process info
742 if (!ParseProcessConfig(config_root_dict, config_out)) {
743 return false;
744 }
745
746 // Get a list of mount points and mounts.
747 if (!ParseMounts(config_root_dict, config_out)) {
748 LOG(ERROR) << "Failed to parse mounts";
749 return false;
750 }
751
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700752 // Hooks info
753 if (!ParseHooks(config_root_dict, config_out)) {
754 return false;
755 }
756
Dylan Reid6b590e62016-10-27 19:10:53 -0700757 // Parse linux node.
758 if (!ParseLinuxConfigDict(config_root_dict, config_out)) {
759 LOG(ERROR) << "Failed to parse the linux node";
760 return false;
761 }
762
763 return true;
764}
765
Ben Chanf43980b2017-03-10 11:31:46 -0800766} // anonymous namespace
Dylan Reid6b590e62016-10-27 19:10:53 -0700767
768bool ParseContainerConfig(const std::string& config_json_data,
769 OciConfigPtr const& config_out) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700770 std::string error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700771 std::unique_ptr<const base::Value> config_root_val =
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700772 base::JSONReader::ReadAndReturnError(
773 config_json_data, base::JSON_PARSE_RFC, nullptr /* error_code_out */,
774 &error_msg, nullptr /* error_line_out */,
775 nullptr /* error_column_out */);
Dylan Reid6b590e62016-10-27 19:10:53 -0700776 if (!config_root_val) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700777 LOG(ERROR) << "Fail to parse config.json: " << error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700778 return false;
779 }
780 const base::DictionaryValue* config_dict = nullptr;
781 if (!config_root_val->GetAsDictionary(&config_dict)) {
782 LOG(ERROR) << "Fail to parse root dictionary from config.json";
783 return false;
784 }
785 if (!ParseConfigDict(*config_dict, config_out)) {
786 return false;
787 }
788
789 return true;
790}
791
Stephen Barber5f6dc9b2017-04-04 12:36:32 -0700792} // namespace run_oci