blob: efe6a9a9f357c4d5d63a10c2a5928090a97a8b0b [file] [log] [blame]
Dylan Reid6b590e62016-10-27 19:10:53 -07001// Copyright 2016 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Stephen Barber5f6dc9b2017-04-04 12:36:32 -07005#include "run_oci/container_config_parser.h"
Dylan Reid6b590e62016-10-27 19:10:53 -07006
Luis Hector Chavez6e9a5332017-10-26 14:41:49 -07007#include <linux/securebits.h>
Luis Hector Chavez8373b412017-07-10 12:51:07 -07008#include <sys/capability.h>
Dylan Reid93fa4602017-06-06 13:39:31 -07009#include <sys/resource.h>
Dylan Reid6b590e62016-10-27 19:10:53 -070010#include <unistd.h>
11
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070012#include <map>
Ben Chanf43980b2017-03-10 11:31:46 -080013#include <regex> // NOLINT(build/c++11)
Dylan Reid6b590e62016-10-27 19:10:53 -070014#include <string>
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -070015#include <utility>
Dylan Reid6b590e62016-10-27 19:10:53 -070016#include <vector>
17
18#include <base/json/json_reader.h>
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -070019#include <base/strings/string_split.h>
Dylan Reid6b590e62016-10-27 19:10:53 -070020#include <base/values.h>
21
Stephen Barber5f6dc9b2017-04-04 12:36:32 -070022namespace run_oci {
Dylan Reid6b590e62016-10-27 19:10:53 -070023
24namespace {
25
Dylan Reidc0c28502016-11-04 10:51:30 -070026// Gets a uint32 from the given dictionary.
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -070027bool ParseUint32FromDict(const base::DictionaryValue& dict,
28 const char* name,
Dylan Reidc0c28502016-11-04 10:51:30 -070029 uint32_t* val_out) {
30 double double_val;
31 if (!dict.GetDouble(name, &double_val)) {
Dylan Reidc0c28502016-11-04 10:51:30 -070032 return false;
33 }
34 *val_out = double_val;
35 return true;
36}
37
Dylan Reidb0a72772016-11-03 16:27:50 +000038// Gets a uint64 from the given dictionary.
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -070039bool ParseUint64FromDict(const base::DictionaryValue& dict,
40 const char* name,
Dylan Reidb0a72772016-11-03 16:27:50 +000041 uint64_t* val_out) {
42 double double_val;
43 if (!dict.GetDouble(name, &double_val)) {
Dylan Reidb0a72772016-11-03 16:27:50 +000044 return false;
45 }
46 *val_out = double_val;
47 return true;
48}
49
Dylan Reid6b590e62016-10-27 19:10:53 -070050// Parses basic platform configuration.
51bool ParsePlatformConfig(const base::DictionaryValue& config_root_dict,
52 OciConfigPtr const& config_out) {
53 // |platform_dict| stays owned by |config_root_dict|
54 const base::DictionaryValue* platform_dict = nullptr;
55 if (!config_root_dict.GetDictionary("platform", &platform_dict)) {
56 LOG(ERROR) << "Fail to parse platform dictionary from config";
57 return false;
58 }
59
60 if (!platform_dict->GetString("os", &config_out->platform.os)) {
61 return false;
62 }
63
64 if (!platform_dict->GetString("arch", &config_out->platform.arch)) {
65 return false;
66 }
67
68 return true;
69}
70
71// Parses root fs info.
72bool ParseRootFileSystemConfig(const base::DictionaryValue& config_root_dict,
73 OciConfigPtr const& config_out) {
74 // |rootfs_dict| stays owned by |config_root_dict|
75 const base::DictionaryValue* rootfs_dict = nullptr;
76 if (!config_root_dict.GetDictionary("root", &rootfs_dict)) {
77 LOG(ERROR) << "Fail to parse rootfs dictionary from config";
78 return false;
79 }
Luis Hector Chavez2a90f292017-10-02 09:51:28 -070080 std::string path;
81 if (!rootfs_dict->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -070082 LOG(ERROR) << "Fail to get rootfs path from config";
83 return false;
84 }
Luis Hector Chavez2a90f292017-10-02 09:51:28 -070085 config_out->root.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -070086 rootfs_dict->GetBoolean("readonly", &config_out->root.readonly);
Dylan Reid6b590e62016-10-27 19:10:53 -070087 return true;
88}
89
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -070090// Fills |config_out| with information about the capability sets in the
91// container.
92bool ParseCapabilitiesConfig(const base::DictionaryValue& capabilities_dict,
93 std::map<std::string, CapSet>* config_out) {
94 constexpr const char* kCapabilitySetNames[] = {
95 "effective", "bounding", "inheritable", "permitted", "ambient"};
96 const std::string kAmbientCapabilitySetName = "ambient";
97
98 CapSet caps_superset;
99 for (const char* set_name : kCapabilitySetNames) {
100 // |capset_list| stays owned by |capabilities_dict|.
101 const base::ListValue* capset_list = nullptr;
102 if (!capabilities_dict.GetList(set_name, &capset_list))
103 continue;
104 CapSet caps;
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700105 cap_value_t cap_value;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700106 for (const auto* cap_name_value : *capset_list) {
107 std::string cap_name;
108 if (!cap_name_value->GetAsString(&cap_name)) {
109 LOG(ERROR) << "Capability list " << set_name
110 << " contains a non-string";
111 return false;
112 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700113 if (cap_from_name(cap_name.c_str(), &cap_value) == -1) {
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700114 LOG(ERROR) << "Unrecognized capability name: " << cap_name;
115 return false;
116 }
Luis Hector Chavez8373b412017-07-10 12:51:07 -0700117 caps[cap_value] = true;
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700118 }
119 (*config_out)[set_name] = caps;
120 caps_superset = caps;
121 }
122
123 // We currently only support sets that are identical, except that ambient is
124 // optional.
125 for (const char* set_name : kCapabilitySetNames) {
126 auto it = config_out->find(set_name);
127 if (it == config_out->end() && set_name == kAmbientCapabilitySetName) {
128 // Ambient capabilities are optional.
129 continue;
130 }
131 if (it == config_out->end()) {
132 LOG(ERROR)
133 << "If capabilities are set, all capability sets should be present";
134 return false;
135 }
136 if (it->second != caps_superset) {
137 LOG(ERROR)
138 << "If capabilities are set, all capability sets should be identical";
139 return false;
140 }
141 }
142
143 return true;
144}
145
Dylan Reid93fa4602017-06-06 13:39:31 -0700146const std::map<std::string, int> kRlimitMap = {
147#define RLIMIT_MAP_ENTRY(limit) \
148 { "RLIMIT_" #limit, RLIMIT_##limit }
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700149 RLIMIT_MAP_ENTRY(CPU), RLIMIT_MAP_ENTRY(FSIZE),
150 RLIMIT_MAP_ENTRY(DATA), RLIMIT_MAP_ENTRY(STACK),
151 RLIMIT_MAP_ENTRY(CORE), RLIMIT_MAP_ENTRY(RSS),
152 RLIMIT_MAP_ENTRY(NPROC), RLIMIT_MAP_ENTRY(NOFILE),
153 RLIMIT_MAP_ENTRY(MEMLOCK), RLIMIT_MAP_ENTRY(AS),
154 RLIMIT_MAP_ENTRY(LOCKS), RLIMIT_MAP_ENTRY(SIGPENDING),
155 RLIMIT_MAP_ENTRY(MSGQUEUE), RLIMIT_MAP_ENTRY(NICE),
156 RLIMIT_MAP_ENTRY(RTPRIO), RLIMIT_MAP_ENTRY(RTTIME),
Dylan Reid93fa4602017-06-06 13:39:31 -0700157#undef RLIMIT_MAP_ENTRY
158};
159
160// Fills |config_out| with information about the capability sets in the
161// container.
162bool ParseRlimitsConfig(const base::ListValue& rlimits_list,
163 std::vector<OciProcessRlimit>* rlimits_out) {
164 size_t num_limits = rlimits_list.GetSize();
165 for (size_t i = 0; i < num_limits; ++i) {
166 const base::DictionaryValue* rlimits_dict;
167 if (!rlimits_list.GetDictionary(i, &rlimits_dict)) {
168 LOG(ERROR) << "Fail to get rlimit item " << i;
169 return false;
170 }
171
172 std::string rlimit_name;
173 if (!rlimits_dict->GetString("type", &rlimit_name)) {
174 LOG(ERROR) << "Fail to get type of rlimit " << i;
175 return false;
176 }
177 const auto it = kRlimitMap.find(rlimit_name);
178 if (it == kRlimitMap.end()) {
179 LOG(ERROR) << "Unrecognized rlimit name: " << rlimit_name;
180 return false;
181 }
182
183 OciProcessRlimit limit;
184 limit.type = it->second;
185 if (!ParseUint32FromDict(*rlimits_dict, "hard", &limit.hard)) {
186 LOG(ERROR) << "Fail to get hard limit of rlimit " << i;
187 return false;
188 }
189 if (!ParseUint32FromDict(*rlimits_dict, "soft", &limit.soft)) {
190 LOG(ERROR) << "Fail to get soft limit of rlimit " << i;
191 return false;
192 }
193 rlimits_out->push_back(limit);
194 }
195
196 return true;
197}
198
Dylan Reid6b590e62016-10-27 19:10:53 -0700199// Fills |config_out| with information about the main process to run in the
200// container and the user it should be run as.
201bool ParseProcessConfig(const base::DictionaryValue& config_root_dict,
202 OciConfigPtr const& config_out) {
203 // |process_dict| stays owned by |config_root_dict|
204 const base::DictionaryValue* process_dict = nullptr;
205 if (!config_root_dict.GetDictionary("process", &process_dict)) {
206 LOG(ERROR) << "Fail to get main process from config";
207 return false;
208 }
209 process_dict->GetBoolean("terminal", &config_out->process.terminal);
210 // |user_dict| stays owned by |process_dict|
211 const base::DictionaryValue* user_dict = nullptr;
212 if (!process_dict->GetDictionary("user", &user_dict)) {
213 LOG(ERROR) << "Failed to get user info from config";
214 return false;
215 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700216 if (!ParseUint32FromDict(*user_dict, "uid", &config_out->process.user.uid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700217 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700218 if (!ParseUint32FromDict(*user_dict, "gid", &config_out->process.user.gid))
Dylan Reid6b590e62016-10-27 19:10:53 -0700219 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700220 // |args_list| stays owned by |process_dict|
221 const base::ListValue* args_list = nullptr;
222 if (!process_dict->GetList("args", &args_list)) {
223 LOG(ERROR) << "Fail to get main process args from config";
224 return false;
225 }
226 size_t num_args = args_list->GetSize();
227 for (size_t i = 0; i < num_args; ++i) {
228 std::string arg;
229 if (!args_list->GetString(i, &arg)) {
230 LOG(ERROR) << "Fail to get process args from config";
231 return false;
232 }
233 config_out->process.args.push_back(arg);
234 }
235 // |env_list| stays owned by |process_dict|
236 const base::ListValue* env_list = nullptr;
237 if (process_dict->GetList("env", &env_list)) {
238 size_t num_env = env_list->GetSize();
239 for (size_t i = 0; i < num_env; ++i) {
240 std::string env;
241 if (!env_list->GetString(i, &env)) {
242 LOG(ERROR) << "Fail to get process env from config";
243 return false;
244 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700245 std::vector<std::string> kvp = base::SplitString(
246 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
247 if (kvp.size() != 2) {
248 LOG(ERROR) << "Fail to parse env \"" << env
249 << "\". Must be in name=value format.";
250 return false;
251 }
252 config_out->process.env.insert(std::make_pair(kvp[0], kvp[1]));
Dylan Reid6b590e62016-10-27 19:10:53 -0700253 }
254 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700255 std::string path;
256 if (!process_dict->GetString("cwd", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700257 LOG(ERROR) << "failed to get cwd of process";
258 return false;
259 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700260 config_out->process.cwd = base::FilePath(path);
261
Luis Hector Chavez15e8e672017-07-20 15:13:27 -0700262 // selinuxLabel is optional.
263 process_dict->GetString("selinuxLabel", &config_out->process.selinuxLabel);
Luis Hector Chavez8e4dcc12017-06-27 12:54:47 -0700264 // |capabilities_dict| stays owned by |process_dict|
265 const base::DictionaryValue* capabilities_dict = nullptr;
266 if (process_dict->GetDictionary("capabilities", &capabilities_dict)) {
267 if (!ParseCapabilitiesConfig(*capabilities_dict,
268 &config_out->process.capabilities)) {
269 return false;
270 }
271 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700272
Dylan Reid93fa4602017-06-06 13:39:31 -0700273 // |rlimit_list| stays owned by |process_dict|
274 const base::ListValue* rlimits_list = nullptr;
275 if (process_dict->GetList("rlimits", &rlimits_list)) {
276 if (!ParseRlimitsConfig(*rlimits_list, &config_out->process.rlimits)) {
277 return false;
278 }
279 }
280
Dylan Reid6b590e62016-10-27 19:10:53 -0700281 return true;
282}
283
284// Parses the 'mounts' field. The necessary mounts for running the container
285// are specified here.
286bool ParseMounts(const base::DictionaryValue& config_root_dict,
287 OciConfigPtr const& config_out) {
288 // |config_mounts_list| stays owned by |config_root_dict|
289 const base::ListValue* config_mounts_list = nullptr;
290 if (!config_root_dict.GetList("mounts", &config_mounts_list)) {
291 LOG(ERROR) << "Fail to get mounts from config dictionary";
292 return false;
293 }
294
295 for (size_t i = 0; i < config_mounts_list->GetSize(); ++i) {
296 const base::DictionaryValue* mount_dict;
297 if (!config_mounts_list->GetDictionary(i, &mount_dict)) {
298 LOG(ERROR) << "Fail to get mount item " << i;
299 return false;
300 }
301 OciMount mount;
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700302 std::string path;
303 if (!mount_dict->GetString("destination", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700304 LOG(ERROR) << "Fail to get mount path for mount " << i;
305 return false;
306 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700307 mount.destination = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700308 if (!mount_dict->GetString("type", &mount.type)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700309 LOG(ERROR) << "Fail to get mount type for mount " << i;
310 return false;
311 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700312 if (!mount_dict->GetString("source", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700313 LOG(ERROR) << "Fail to get mount source for mount " << i;
314 return false;
315 }
Luis Hector Chavez60f9bb12017-10-16 11:33:01 -0700316 if (!mount_dict->GetBoolean("performInIntermediateNamespace",
Luis Hector Chaveze2b8c5e2017-10-24 13:10:26 -0700317 &mount.performInIntermediateNamespace)) {
318 mount.performInIntermediateNamespace = false; // Optional
Luis Hector Chavez60f9bb12017-10-16 11:33:01 -0700319 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700320 mount.source = base::FilePath(path);
Dylan Reid6b590e62016-10-27 19:10:53 -0700321
322 // |options| are owned by |mount_dict|
323 const base::ListValue* options = nullptr;
324 if (mount_dict->GetList("options", &options)) {
325 for (size_t j = 0; j < options->GetSize(); ++j) {
326 std::string this_opt;
327 if (!options->GetString(j, &this_opt)) {
328 LOG(ERROR) << "Fail to get option " << j << " from mount options";
329 return false;
330 }
331 mount.options.push_back(this_opt);
332 }
333 }
334
335 config_out->mounts.push_back(mount);
336 }
337 return true;
338}
339
Dylan Reid6985e3b2017-03-31 19:39:16 -0700340// Parses the linux resource list
341bool ParseResources(const base::DictionaryValue& resources_dict,
342 OciLinuxResources* resources_out) {
343 // |device_list| is owned by |resources_dict|
344 const base::ListValue* device_list = nullptr;
345 if (!resources_dict.GetList("devices", &device_list)) {
346 // The device list is optional.
347 return true;
348 }
349 size_t num_devices = device_list->GetSize();
350 for (size_t i = 0; i < num_devices; ++i) {
351 OciLinuxCgroupDevice device;
352
353 const base::DictionaryValue* dev;
354 if (!device_list->GetDictionary(i, &dev)) {
355 LOG(ERROR) << "Fail to get device " << i;
356 return false;
357 }
358
359 if (!dev->GetBoolean("allow", &device.allow)) {
360 LOG(ERROR) << "Fail to get allow value for device " << i;
361 return false;
362 }
363 if (!dev->GetString("access", &device.access))
364 device.access = "rwm"; // Optional, default to all perms.
365 if (!dev->GetString("type", &device.type))
366 device.type = "a"; // Optional, default to both a means all.
367 if (!ParseUint32FromDict(*dev, "major", &device.major))
368 device.major = -1; // Optional, -1 will map to all devices.
369 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
370 device.minor = -1; // Optional, -1 will map to all devices.
371
372 resources_out->devices.push_back(device);
373 }
374
375 return true;
376}
377
Stephen Barber3c0a2022017-09-08 14:17:57 -0700378// Parses the list of namespaces and fills |namespaces_out| with them.
379bool ParseNamespaces(const base::ListValue* namespaces_list,
380 std::vector<OciNamespace>* namespaces_out) {
381 for (size_t i = 0; i < namespaces_list->GetSize(); ++i) {
382 OciNamespace new_namespace;
383 const base::DictionaryValue* ns;
384 if (!namespaces_list->GetDictionary(i, &ns)) {
385 LOG(ERROR) << "Failed to get namespace " << i;
386 return false;
387 }
388 if (!ns->GetString("type", &new_namespace.type)) {
389 LOG(ERROR) << "Namespace " << i << " missing type";
390 return false;
391 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700392 std::string path;
393 if (ns->GetString("path", &path))
394 new_namespace.path = base::FilePath(path);
Stephen Barber3c0a2022017-09-08 14:17:57 -0700395 namespaces_out->push_back(new_namespace);
396 }
397 return true;
398}
399
Dylan Reid6b590e62016-10-27 19:10:53 -0700400// Parse the list of device nodes that the container needs to run.
401bool ParseDeviceList(const base::DictionaryValue& linux_dict,
402 OciConfigPtr const& config_out) {
403 // |device_list| is owned by |linux_dict|
404 const base::ListValue* device_list = nullptr;
405 if (!linux_dict.GetList("devices", &device_list)) {
Dylan Reida5ed1272016-11-11 16:43:39 -0800406 // The device list is optional.
407 return true;
Dylan Reid6b590e62016-10-27 19:10:53 -0700408 }
409 size_t num_devices = device_list->GetSize();
410 for (size_t i = 0; i < num_devices; ++i) {
411 OciLinuxDevice device;
412
413 const base::DictionaryValue* dev;
414 if (!device_list->GetDictionary(i, &dev)) {
415 LOG(ERROR) << "Fail to get device " << i;
416 return false;
417 }
418 std::string path;
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700419 if (!dev->GetString("path", &path)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700420 LOG(ERROR) << "Fail to get path for dev";
421 return false;
422 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700423 device.path = base::FilePath(path);
Dylan Reid6d650742016-11-02 23:10:38 -0700424 if (!dev->GetString("type", &device.type)) {
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700425 LOG(ERROR) << "Fail to get type for " << device.path.value();
Dylan Reid6b590e62016-10-27 19:10:53 -0700426 return false;
427 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700428 if (!ParseUint32FromDict(*dev, "major", &device.major))
429 return false;
Luis Hector Chavezac20fc52017-10-10 11:08:41 -0700430 dev->GetBoolean("dynamicMinor", &device.dynamicMinor);
431 if (device.dynamicMinor) {
432 if (dev->HasKey("minor")) {
433 LOG(WARNING)
434 << "Ignoring \"minor\" since \"dynamicMinor\" is specified for "
435 << device.path.value();
436 }
437 } else {
438 if (!ParseUint32FromDict(*dev, "minor", &device.minor))
439 return false;
440 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700441 if (!ParseUint32FromDict(*dev, "fileMode", &device.fileMode))
442 return false;
443 if (!ParseUint32FromDict(*dev, "uid", &device.uid))
444 return false;
445 if (!ParseUint32FromDict(*dev, "gid", &device.gid))
446 return false;
Dylan Reid6b590e62016-10-27 19:10:53 -0700447
448 config_out->linux_config.devices.push_back(device);
449 }
450
451 return true;
452}
453
454// Parses the list of ID mappings and fills |mappings_out| with them.
455bool ParseLinuxIdMappings(const base::ListValue* id_map_list,
Ben Chanf43980b2017-03-10 11:31:46 -0800456 std::vector<OciLinuxNamespaceMapping>* mappings_out) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700457 for (size_t i = 0; i < id_map_list->GetSize(); ++i) {
458 OciLinuxNamespaceMapping new_map;
459 const base::DictionaryValue* map;
460 if (!id_map_list->GetDictionary(i, &map)) {
461 LOG(ERROR) << "Fail to get id map " << i;
462 return false;
463 }
Dylan Reidc0c28502016-11-04 10:51:30 -0700464 if (!ParseUint32FromDict(*map, "hostID", &new_map.hostID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700465 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700466 if (!ParseUint32FromDict(*map, "containerID", &new_map.containerID))
Dylan Reid6b590e62016-10-27 19:10:53 -0700467 return false;
Dylan Reidc0c28502016-11-04 10:51:30 -0700468 if (!ParseUint32FromDict(*map, "size", &new_map.size))
Dylan Reid6b590e62016-10-27 19:10:53 -0700469 return false;
Ben Chanf43980b2017-03-10 11:31:46 -0800470 mappings_out->push_back(new_map);
Dylan Reid6b590e62016-10-27 19:10:53 -0700471 }
472 return true;
473}
474
Dylan Reidb0a72772016-11-03 16:27:50 +0000475// Parses seccomp syscall args.
476bool ParseSeccompArgs(const base::DictionaryValue& syscall_dict,
477 OciSeccompSyscall* syscall_out) {
478 const base::ListValue* args = nullptr;
479 if (syscall_dict.GetList("args", &args)) {
480 for (size_t i = 0; i < args->GetSize(); ++i) {
481 const base::DictionaryValue* args_dict = nullptr;
482 if (!args->GetDictionary(i, &args_dict)) {
483 LOG(ERROR) << "Failed to pars args dict for " << syscall_out->name;
484 return false;
485 }
486 OciSeccompArg this_arg;
487 if (!ParseUint32FromDict(*args_dict, "index", &this_arg.index))
488 return false;
489 if (!ParseUint64FromDict(*args_dict, "value", &this_arg.value))
490 return false;
491 if (!ParseUint64FromDict(*args_dict, "value2", &this_arg.value2))
492 return false;
493 if (!args_dict->GetString("op", &this_arg.op)) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700494 LOG(ERROR) << "Failed to parse op for arg " << this_arg.index << " of "
495 << syscall_out->name;
Dylan Reidb0a72772016-11-03 16:27:50 +0000496 return false;
497 }
498 syscall_out->args.push_back(this_arg);
499 }
500 }
501 return true;
502}
503
504// Parses the seccomp node if it is present.
505bool ParseSeccompInfo(const base::DictionaryValue& seccomp_dict,
506 OciSeccomp* seccomp_out) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700507 if (!seccomp_dict.GetString("defaultAction", &seccomp_out->defaultAction))
Dylan Reidb0a72772016-11-03 16:27:50 +0000508 return false;
509
510 // Gets the list of architectures.
511 const base::ListValue* architectures = nullptr;
512 if (!seccomp_dict.GetList("architectures", &architectures)) {
513 LOG(ERROR) << "Fail to read seccomp architectures";
514 return false;
515 }
516 for (size_t i = 0; i < architectures->GetSize(); ++i) {
517 std::string this_arch;
518 if (!architectures->GetString(i, &this_arch)) {
519 LOG(ERROR) << "Fail to parse seccomp architecture list";
520 return false;
521 }
522 seccomp_out->architectures.push_back(this_arch);
523 }
524
525 // Gets the list of syscalls.
526 const base::ListValue* syscalls = nullptr;
527 if (!seccomp_dict.GetList("syscalls", &syscalls)) {
528 LOG(ERROR) << "Fail to read seccomp syscalls";
529 return false;
530 }
531 for (size_t i = 0; i < syscalls->GetSize(); ++i) {
532 const base::DictionaryValue* syscall_dict = nullptr;
533 if (!syscalls->GetDictionary(i, &syscall_dict)) {
534 LOG(ERROR) << "Fail to parse seccomp syscalls list";
535 return false;
536 }
537 OciSeccompSyscall this_syscall;
538 if (!syscall_dict->GetString("name", &this_syscall.name)) {
539 LOG(ERROR) << "Fail to parse syscall name " << i;
540 return false;
541 }
542 if (!syscall_dict->GetString("action", &this_syscall.action)) {
543 LOG(ERROR) << "Fail to parse syscall action for " << this_syscall.name;
544 return false;
545 }
546 if (!ParseSeccompArgs(*syscall_dict, &this_syscall))
547 return false;
548 seccomp_out->syscalls.push_back(this_syscall);
549 }
550
551 return true;
552}
553
Luis Hector Chavez6e9a5332017-10-26 14:41:49 -0700554constexpr std::pair<const char*, uint64_t> kSecurebitsMapping[] = {
555#define SECUREBIT_MAP_ENTRY(secbit) \
556 { #secbit, SECBIT_##secbit }
557 SECUREBIT_MAP_ENTRY(NOROOT), SECUREBIT_MAP_ENTRY(NOROOT_LOCKED),
558 SECUREBIT_MAP_ENTRY(NO_SETUID_FIXUP),
559 SECUREBIT_MAP_ENTRY(NO_SETUID_FIXUP_LOCKED), SECUREBIT_MAP_ENTRY(KEEP_CAPS),
560 SECUREBIT_MAP_ENTRY(KEEP_CAPS_LOCKED),
561#if defined(SECBIT_NO_CAP_AMBIENT_RAISE)
562 // Kernels < v4.4 do not have this.
563 SECUREBIT_MAP_ENTRY(NO_CAP_AMBIENT_RAISE),
564 SECUREBIT_MAP_ENTRY(NO_CAP_AMBIENT_RAISE_LOCKED),
565#endif // SECBIT_NO_CAP_AMBIENT_RAISE
566#undef SECUREBIT_MAP_ENTRY
567};
568
569bool ParseSecurebit(const std::string& securebit_name, uint64_t* mask_out) {
570 for (const auto& entry : kSecurebitsMapping) {
571 if (securebit_name == entry.first) {
572 *mask_out = entry.second;
573 return true;
574 }
575 }
576 LOG(ERROR) << "Unrecognized securebit name: " << securebit_name;
577 return false;
578}
579
580bool ParseSkipSecurebitsMask(const base::ListValue& skip_securebits_list,
581 uint64_t* securebits_mask_out) {
582 size_t num_securebits = skip_securebits_list.GetSize();
583 for (size_t i = 0; i < num_securebits; ++i) {
584 std::string securebit_name;
585 if (!skip_securebits_list.GetString(i, &securebit_name)) {
586 LOG(ERROR) << "Fail to get securebit name " << i;
587 return false;
588 }
589 uint64_t mask = 0;
590 if (!ParseSecurebit(securebit_name, &mask))
591 return false;
592 *securebits_mask_out |= mask;
593 }
594 return true;
595}
596
Dylan Reid6b590e62016-10-27 19:10:53 -0700597// Parses the linux node which has information about setting up a user
598// namespace, and the list of devices for the container.
599bool ParseLinuxConfigDict(const base::DictionaryValue& runtime_root_dict,
600 OciConfigPtr const& config_out) {
601 // |linux_dict| is owned by |runtime_root_dict|
602 const base::DictionaryValue* linux_dict = nullptr;
603 if (!runtime_root_dict.GetDictionary("linux", &linux_dict)) {
604 LOG(ERROR) << "Fail to get linux dictionary from the runtime dictionary";
605 return false;
606 }
607
608 // |uid_map_list| is owned by |linux_dict|
609 const base::ListValue* uid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700610 if (linux_dict->GetList("uidMappings", &uid_map_list))
611 ParseLinuxIdMappings(uid_map_list, &config_out->linux_config.uidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700612
613 // |gid_map_list| is owned by |linux_dict|
614 const base::ListValue* gid_map_list = nullptr;
Stephen Barber771653f2017-10-04 23:48:57 -0700615 if (linux_dict->GetList("gidMappings", &gid_map_list))
616 ParseLinuxIdMappings(gid_map_list, &config_out->linux_config.gidMappings);
Dylan Reid6b590e62016-10-27 19:10:53 -0700617
618 if (!ParseDeviceList(*linux_dict, config_out))
619 return false;
620
Dylan Reid6985e3b2017-03-31 19:39:16 -0700621 const base::DictionaryValue* resources_dict = nullptr;
622 if (linux_dict->GetDictionary("resources", &resources_dict)) {
623 if (!ParseResources(*resources_dict, &config_out->linux_config.resources))
624 return false;
625 }
626
Stephen Barber3c0a2022017-09-08 14:17:57 -0700627 const base::ListValue* namespaces_list = nullptr;
628 if (linux_dict->GetList("namespaces", &namespaces_list)) {
629 if (!ParseNamespaces(namespaces_list, &config_out->linux_config.namespaces))
630 return false;
631 }
632
Dylan Reidb0a72772016-11-03 16:27:50 +0000633 const base::DictionaryValue* seccomp_dict = nullptr;
634 if (linux_dict->GetDictionary("seccomp", &seccomp_dict)) {
635 if (!ParseSeccompInfo(*seccomp_dict, &config_out->linux_config.seccomp))
636 return false;
637 }
638
Luis Hector Chavez45ac1242017-10-26 13:21:16 -0700639 std::string cgroups_path_string;
640 if (linux_dict->GetString("cgroupsPath", &cgroups_path_string))
641 config_out->linux_config.cgroupsPath = base::FilePath(cgroups_path_string);
642
Luis Hector Chavez0f3d7a42017-10-26 10:48:30 -0700643 if (!linux_dict->GetString("altSyscall",
644 &config_out->linux_config.altSyscall)) {
645 config_out->linux_config.altSyscall = std::string(); // Optional
646 }
647
Luis Hector Chavez6e9a5332017-10-26 14:41:49 -0700648 const base::ListValue* skip_securebits_list;
649 if (linux_dict->GetList("skipSecurebits", &skip_securebits_list)) {
650 if (!ParseSkipSecurebitsMask(*skip_securebits_list,
651 &config_out->linux_config.skipSecurebits)) {
652 return false;
653 }
654 } else {
655 config_out->linux_config.skipSecurebits = 0; // Optional
656 }
657
Dylan Reid6b590e62016-10-27 19:10:53 -0700658 return true;
659}
660
Dylan Reid45e34fe2016-12-02 15:11:53 -0800661bool HostnameValid(const std::string& hostname) {
662 if (hostname.length() > 255)
663 return false;
664
665 const std::regex name("^[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?$");
666 if (!std::regex_match(hostname, name))
667 return false;
668
669 const std::regex double_dash("--");
670 if (std::regex_match(hostname, double_dash))
671 return false;
672
673 return true;
674}
675
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700676bool ParseHooksList(const base::ListValue& hooks_list,
677 std::vector<OciHook>* hooks_out,
678 const std::string& hook_type) {
679 size_t num_hooks = hooks_list.GetSize();
680 for (size_t i = 0; i < num_hooks; ++i) {
681 OciHook hook;
682 const base::DictionaryValue* hook_dict;
683 if (!hooks_list.GetDictionary(i, &hook_dict)) {
684 LOG(ERROR) << "Fail to get " << hook_type << " hook item " << i;
685 return false;
686 }
687
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700688 std::string path;
689 if (!hook_dict->GetString("path", &path)) {
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700690 LOG(ERROR) << "Fail to get path of " << hook_type << " hook " << i;
691 return false;
692 }
Luis Hector Chavez855e99e2017-10-10 10:27:33 -0700693 hook.path = base::FilePath(path);
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700694
695 const base::ListValue* hook_args;
696 // args are optional.
697 if (hook_dict->GetList("args", &hook_args)) {
698 size_t num_args = hook_args->GetSize();
699 for (size_t j = 0; j < num_args; ++j) {
700 std::string arg;
701 if (!hook_args->GetString(j, &arg)) {
702 LOG(ERROR) << "Fail to get arg " << j << " of " << hook_type
703 << " hook " << i;
704 return false;
705 }
706 hook.args.push_back(arg);
707 }
708 }
709
710 const base::ListValue* hook_envs;
711 // envs are optional.
712 if (hook_dict->GetList("env", &hook_envs)) {
713 size_t num_env = hook_envs->GetSize();
714 for (size_t j = 0; j < num_env; ++j) {
715 std::string env;
716 if (!hook_envs->GetString(j, &env)) {
717 LOG(ERROR) << "Fail to get env " << j << " of " << hook_type
718 << " hook " << i;
719 return false;
720 }
721 std::vector<std::string> kvp = base::SplitString(
722 env, "=", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
723 if (kvp.size() != 2) {
724 LOG(ERROR) << "Fail to parse env \"" << env
725 << "\". Must be in name=value format.";
726 return false;
727 }
728 hook.env.insert(std::make_pair(kvp[0], kvp[1]));
729 }
730 }
731
732 int timeout_seconds;
733 // timeout is optional.
734 if (hook_dict->GetInteger("timeout", &timeout_seconds)) {
735 hook.timeout = base::TimeDelta::FromSeconds(timeout_seconds);
736 } else {
737 hook.timeout = base::TimeDelta::Max();
738 }
739
740 hooks_out->emplace_back(std::move(hook));
741 }
742 return true;
743}
744
745bool ParseHooks(const base::DictionaryValue& config_root_dict,
746 OciConfigPtr const& config_out) {
747 const base::DictionaryValue* hooks_config_dict;
748 if (!config_root_dict.GetDictionary("hooks", &hooks_config_dict)) {
749 // Hooks are optional.
750 return true;
751 }
752
753 const base::ListValue* hooks_list;
Luis Hector Chavezbb515a02017-09-29 15:44:35 -0700754 if (hooks_config_dict->GetList("prechroot", &hooks_list)) {
755 if (!ParseHooksList(*hooks_list, &config_out->pre_chroot_hooks,
756 "prechroot")) {
757 return false;
758 }
759 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700760 if (hooks_config_dict->GetList("prestart", &hooks_list)) {
761 if (!ParseHooksList(*hooks_list, &config_out->pre_start_hooks, "prestart"))
762 return false;
763 }
764 if (hooks_config_dict->GetList("poststart", &hooks_list)) {
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700765 if (!ParseHooksList(*hooks_list, &config_out->post_start_hooks,
766 "poststart"))
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700767 return false;
768 }
769 if (hooks_config_dict->GetList("poststop", &hooks_list)) {
770 if (!ParseHooksList(*hooks_list, &config_out->post_stop_hooks, "poststop"))
771 return false;
772 }
773 return true;
774}
775
Dylan Reid6b590e62016-10-27 19:10:53 -0700776// Parses the configuration file for the container. The config file specifies
777// basic filesystem info and details about the process to be run. namespace,
778// cgroup, and syscall configurations are also specified
779bool ParseConfigDict(const base::DictionaryValue& config_root_dict,
780 OciConfigPtr const& config_out) {
Dylan Reid6d650742016-11-02 23:10:38 -0700781 if (!config_root_dict.GetString("ociVersion", &config_out->ociVersion)) {
Dylan Reid6b590e62016-10-27 19:10:53 -0700782 LOG(ERROR) << "Failed to parse ociVersion";
783 return false;
784 }
785 if (!config_root_dict.GetString("hostname", &config_out->hostname)) {
786 LOG(ERROR) << "Failed to parse hostname";
787 return false;
788 }
Dylan Reid45e34fe2016-12-02 15:11:53 -0800789 if (!HostnameValid(config_out->hostname)) {
790 LOG(ERROR) << "Invalid hostname " << config_out->hostname;
791 return false;
792 }
Dylan Reid6b590e62016-10-27 19:10:53 -0700793
794 // Platform info
795 if (!ParsePlatformConfig(config_root_dict, config_out)) {
796 return false;
797 }
798
799 // Root fs info
800 if (!ParseRootFileSystemConfig(config_root_dict, config_out)) {
801 return false;
802 }
803
804 // Process info
805 if (!ParseProcessConfig(config_root_dict, config_out)) {
806 return false;
807 }
808
809 // Get a list of mount points and mounts.
810 if (!ParseMounts(config_root_dict, config_out)) {
811 LOG(ERROR) << "Failed to parse mounts";
812 return false;
813 }
814
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -0700815 // Hooks info
816 if (!ParseHooks(config_root_dict, config_out)) {
817 return false;
818 }
819
Dylan Reid6b590e62016-10-27 19:10:53 -0700820 // Parse linux node.
821 if (!ParseLinuxConfigDict(config_root_dict, config_out)) {
822 LOG(ERROR) << "Failed to parse the linux node";
823 return false;
824 }
825
826 return true;
827}
828
Ben Chanf43980b2017-03-10 11:31:46 -0800829} // anonymous namespace
Dylan Reid6b590e62016-10-27 19:10:53 -0700830
831bool ParseContainerConfig(const std::string& config_json_data,
832 OciConfigPtr const& config_out) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700833 std::string error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700834 std::unique_ptr<const base::Value> config_root_val =
Luis Hector Chavez9f9f66e2017-10-16 14:54:32 -0700835 base::JSONReader::ReadAndReturnError(
836 config_json_data, base::JSON_PARSE_RFC, nullptr /* error_code_out */,
837 &error_msg, nullptr /* error_line_out */,
838 nullptr /* error_column_out */);
Dylan Reid6b590e62016-10-27 19:10:53 -0700839 if (!config_root_val) {
Luis Hector Chavezd63f1ba2017-06-28 15:58:12 -0700840 LOG(ERROR) << "Fail to parse config.json: " << error_msg;
Dylan Reid6b590e62016-10-27 19:10:53 -0700841 return false;
842 }
843 const base::DictionaryValue* config_dict = nullptr;
844 if (!config_root_val->GetAsDictionary(&config_dict)) {
845 LOG(ERROR) << "Fail to parse root dictionary from config.json";
846 return false;
847 }
848 if (!ParseConfigDict(*config_dict, config_out)) {
849 return false;
850 }
851
852 return true;
853}
854
Stephen Barber5f6dc9b2017-04-04 12:36:32 -0700855} // namespace run_oci