blob: 99c995228e348a77d4d2c560c2b114cd025362b3 [file] [log] [blame]
Luis Hector Chavez81efb332017-09-18 14:01:29 -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.
Dylan Reid837c74a2016-01-22 17:25:21 -08004
Dylan Reid837c74a2016-01-22 17:25:21 -08005#include <errno.h>
6#include <fcntl.h>
Dylan Reid837c74a2016-01-22 17:25:21 -08007#include <signal.h>
Luis Hector Chavezff5978f2017-06-27 12:52:58 -07008#include <stdint.h>
Dylan Reid837c74a2016-01-22 17:25:21 -08009#include <stdlib.h>
10#include <string.h>
11#include <sys/mount.h>
12#include <sys/stat.h>
13#include <sys/types.h>
Dylan Reid2bd9ea92016-04-07 20:57:47 -070014#include <sys/wait.h>
Luis Hector Chavez836d7b22017-09-14 15:11:15 -070015#include <syscall.h>
Dylan Reid837c74a2016-01-22 17:25:21 -080016#include <unistd.h>
17
yusukes32622542018-01-05 18:59:52 -080018#include <algorithm>
Luis Hector Chavez644d2042017-09-19 18:56:44 -070019#include <map>
Luis Hector Chavez5381d002017-09-16 12:54:24 -070020#include <memory>
yusukesbbc37a72017-11-21 09:51:54 -080021#include <ostream>
Stephen Barber771653f2017-10-04 23:48:57 -070022#include <set>
yusukesbbc37a72017-11-21 09:51:54 -080023#include <sstream>
Luis Hector Chavez5381d002017-09-16 12:54:24 -070024#include <string>
yusukes32622542018-01-05 18:59:52 -080025#include <tuple>
Luis Hector Chavez644d2042017-09-19 18:56:44 -070026#include <utility>
Luis Hector Chavez5381d002017-09-16 12:54:24 -070027#include <vector>
28
29#include <base/bind.h>
30#include <base/bind_helpers.h>
31#include <base/callback_helpers.h>
32#include <base/files/file_path.h>
33#include <base/files/file_util.h>
34#include <base/files/scoped_file.h>
Luis Hector Chavez835d39e2017-09-19 15:16:31 -070035#include <base/logging.h>
Luis Hector Chavez5381d002017-09-16 12:54:24 -070036#include <base/macros.h>
37#include <base/strings/string_util.h>
38#include <base/strings/stringprintf.h>
Luis Hector Chavez836d7b22017-09-14 15:11:15 -070039#include <libminijail.h>
Luis Hector Chavez626f5c82017-09-18 11:19:32 -070040#include <scoped_minijail.h>
Mike Frysinger412dbd22017-01-06 01:50:34 -050041
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -070042#include "libcontainer/cgroup.h"
Luis Hector Chavez644d2042017-09-19 18:56:44 -070043#include "libcontainer/config.h"
Luis Hector Chavez836d7b22017-09-14 15:11:15 -070044#include "libcontainer/libcontainer.h"
Luis Hector Chavez81efb332017-09-18 14:01:29 -070045#include "libcontainer/libcontainer_util.h"
Yusuke Sato91f11f02016-12-02 16:15:13 -080046
yusukesbbc37a72017-11-21 09:51:54 -080047#define QUOTE(s) ('"' + std::string(s) + '"')
48
Luis Hector Chavez5381d002017-09-16 12:54:24 -070049namespace {
50
Luis Hector Chavez81efb332017-09-18 14:01:29 -070051using libcontainer::DeviceMapperDetach;
52using libcontainer::DeviceMapperSetup;
53using libcontainer::GetUsernsOutsideId;
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -070054using libcontainer::Loopdev;
Luis Hector Chavez81efb332017-09-18 14:01:29 -070055using libcontainer::LoopdevDetach;
56using libcontainer::LoopdevSetup;
57using libcontainer::MakeDir;
58using libcontainer::MountExternal;
59using libcontainer::TouchFile;
Mike Frysinger412dbd22017-01-06 01:50:34 -050060
Luis Hector Chavez81efb332017-09-18 14:01:29 -070061constexpr size_t kMaxRlimits = 32; // Linux defines 15 at the time of writing.
Luis Hector Chavez479b95f2016-06-06 08:01:05 -070062
Luis Hector Chavez5381d002017-09-16 12:54:24 -070063struct Mount {
64 std::string name;
65 base::FilePath source;
66 base::FilePath destination;
67 std::string type;
68 std::string data;
69 std::string verity;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -070070 int flags;
71 int uid;
72 int gid;
73 int mode;
Luis Hector Chavez5381d002017-09-16 12:54:24 -070074
75 // True if mount should happen in new vfs ns.
76 bool mount_in_ns;
77
78 // True if target should be created if it doesn't exist.
79 bool create;
80
81 // True if target should be mounted via loopback.
82 bool loopback;
Dylan Reid837c74a2016-01-22 17:25:21 -080083};
84
Luis Hector Chaveze1062e82017-09-18 09:57:37 -070085struct Device {
86 // 'c' or 'b' for char or block
87 char type;
88 base::FilePath path;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -070089 int fs_permissions;
90 int major;
91 int minor;
Luis Hector Chaveze1062e82017-09-18 09:57:37 -070092
Stephen Barber7bae6642017-11-30 10:47:12 -080093 // Copy the major from existing node, ignores |major|.
94 bool copy_major;
Luis Hector Chaveze1062e82017-09-18 09:57:37 -070095 // Copy the minor from existing node, ignores |minor|.
96 bool copy_minor;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -070097 int uid;
98 int gid;
Dylan Reid4843d6b2017-03-31 18:14:30 -070099};
100
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700101struct CgroupDevice {
102 bool allow;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700103 char type;
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700104
105 // -1 for either major or minor means all.
106 int major;
107 int minor;
108
109 bool read;
110 bool write;
111 bool modify;
Dylan Reid837c74a2016-01-22 17:25:21 -0800112};
113
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700114struct CpuCgroup {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700115 int shares;
116 int quota;
117 int period;
118 int rt_runtime;
119 int rt_period;
Chinyue Chenfac909e2016-06-24 14:17:42 +0800120};
121
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700122struct Rlimit {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700123 int type;
Luis Hector Chavezda352462018-01-30 09:10:00 -0800124 rlim_t cur;
125 rlim_t max;
Dylan Reid93fa4602017-06-06 13:39:31 -0700126};
127
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700128} // namespace
129
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700130// Structure that configures how the container is run.
Dylan Reid837c74a2016-01-22 17:25:21 -0800131struct container_config {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700132 // Path to the root of the container itself.
133 base::FilePath config_root;
134
135 // Path to the root of the container's filesystem.
136 base::FilePath rootfs;
137
138 // Flags that will be passed to mount() for the rootfs.
yusukesb7b9a042017-12-08 13:14:25 -0800139 unsigned long rootfs_mount_flags = 0x0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700140
141 // Path to where the container will be run.
142 base::FilePath premounted_runfs;
143
144 // Path to the file where the pid should be written.
145 base::FilePath pid_file_path;
146
147 // The program to run and args, e.g. "/sbin/init".
148 std::vector<std::string> program_argv;
149
150 // The uid the container will run as.
yusukesb7b9a042017-12-08 13:14:25 -0800151 uid_t uid = 0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700152
153 // Mapping of UIDs in the container, e.g. "0 100000 1024"
154 std::string uid_map;
155
156 // The gid the container will run as.
yusukesb7b9a042017-12-08 13:14:25 -0800157 gid_t gid = 0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700158
159 // Mapping of GIDs in the container, e.g. "0 100000 1024"
160 std::string gid_map;
161
Risanfd41aee2018-08-15 14:03:38 +0900162 // The supplementary gids that attached to the container.
163 std::vector<gid_t> additional_gids;
164
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700165 // Syscall table to use or nullptr if none.
166 std::string alt_syscall_table;
167
168 // Filesystems to mount in the new namespace.
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700169 std::vector<Mount> mounts;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700170
Stephen Barber771653f2017-10-04 23:48:57 -0700171 // Namespaces that should be used for the container.
172 std::set<std::string> namespaces;
173
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700174 // Device nodes to create.
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700175 std::vector<Device> devices;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700176
177 // Device node cgroup permissions.
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700178 std::vector<CgroupDevice> cgroup_devices;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700179
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700180 // CPU cgroup params.
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700181 CpuCgroup cpu_cgparams;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700182
183 // Parent dir for cgroup creation
184 base::FilePath cgroup_parent;
185
186 // uid to own the created cgroups
yusukesb7b9a042017-12-08 13:14:25 -0800187 uid_t cgroup_owner = 0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700188
189 // gid to own the created cgroups
yusukesb7b9a042017-12-08 13:14:25 -0800190 gid_t cgroup_group = 0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700191
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700192 // Allow the child process to keep open FDs (for stdin/out/err).
yusukesf125f332017-12-08 13:45:15 -0800193 bool keep_fds_open = false;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700194
195 // Array of rlimits for the contained process.
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700196 Rlimit rlimits[kMaxRlimits];
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700197
198 // The number of elements in `rlimits`.
yusukesb7b9a042017-12-08 13:14:25 -0800199 int num_rlimits = 0;
yusukesf125f332017-12-08 13:45:15 -0800200 bool use_capmask = false;
201 bool use_capmask_ambient = false;
yusukesb7b9a042017-12-08 13:14:25 -0800202 uint64_t capmask = 0x0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700203
204 // The mask of securebits to skip when restricting caps.
yusukesb7b9a042017-12-08 13:14:25 -0800205 uint64_t securebits_skip_mask = 0x0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700206
207 // Whether the container needs an extra process to be run as init.
yusukesf125f332017-12-08 13:45:15 -0800208 bool do_init = false;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700209
210 // The SELinux context name the container will run under.
211 std::string selinux_context;
212
213 // A function pointer to be called prior to calling execve(2).
yusukesb7b9a042017-12-08 13:14:25 -0800214 minijail_hook_t pre_start_hook = nullptr;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700215
216 // Parameter that will be passed to pre_start_hook().
yusukesb7b9a042017-12-08 13:14:25 -0800217 void* pre_start_hook_payload = nullptr;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700218
Luis Hector Chaveze03926a2017-09-28 17:28:49 -0700219 // A list of file descriptors to inherit.
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700220 std::vector<int> inherited_fds;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700221
222 // A list of hooks that will be called upon minijail reaching various states
223 // of execution.
224 std::map<minijail_hook_event_t, std::vector<libcontainer::HookCallback>>
225 hooks;
Dylan Reid837c74a2016-01-22 17:25:21 -0800226};
227
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700228// Container manipulation
229struct container {
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -0700230 std::unique_ptr<libcontainer::Cgroup> cgroup;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700231 ScopedMinijail jail;
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700232 pid_t init_pid = -1;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700233 base::FilePath config_root;
234 base::FilePath runfs;
235 base::FilePath rundir;
236 base::FilePath runfsroot;
237 base::FilePath pid_file_path;
238
239 // Mounts made outside of the minijail.
240 std::vector<base::FilePath> ext_mounts;
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700241 std::vector<Loopdev> loopdevs;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700242 std::vector<std::string> device_mappers;
243 std::string name;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700244
245 std::vector<std::pair<libcontainer::HookState,
246 std::vector<libcontainer::HookCallback>>>
247 hook_states;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700248};
249
250namespace {
251
yusukes4d955472018-01-17 16:41:32 -0800252std::string GetMountFlagsAsString(int flags) {
253#define CHECK_MOUNT_FLAG(flag) \
254 do { \
255 if (flags & flag) \
256 result.push_back(#flag); \
257 } while (false)
258
259 std::vector<std::string> result;
260 CHECK_MOUNT_FLAG(MS_RDONLY);
261 CHECK_MOUNT_FLAG(MS_NOSUID);
262 CHECK_MOUNT_FLAG(MS_NODEV);
263 CHECK_MOUNT_FLAG(MS_NOEXEC);
264 CHECK_MOUNT_FLAG(MS_SYNCHRONOUS);
265 CHECK_MOUNT_FLAG(MS_REMOUNT);
266 CHECK_MOUNT_FLAG(MS_MANDLOCK);
267 CHECK_MOUNT_FLAG(MS_DIRSYNC);
268 CHECK_MOUNT_FLAG(MS_NOATIME);
269 CHECK_MOUNT_FLAG(MS_NODIRATIME);
270 CHECK_MOUNT_FLAG(MS_BIND);
271 CHECK_MOUNT_FLAG(MS_MOVE);
272 CHECK_MOUNT_FLAG(MS_REC);
273 CHECK_MOUNT_FLAG(MS_SILENT);
274 CHECK_MOUNT_FLAG(MS_POSIXACL);
275 CHECK_MOUNT_FLAG(MS_UNBINDABLE);
276 CHECK_MOUNT_FLAG(MS_PRIVATE);
277 CHECK_MOUNT_FLAG(MS_SLAVE);
278 CHECK_MOUNT_FLAG(MS_SHARED);
279 return result.empty() ? "no flags" : base::JoinString(result, " | ");
280
281#undef CHECK_MOUNT_FLAG
282}
283
yusukesbbc37a72017-11-21 09:51:54 -0800284std::ostream& operator<<(std::ostream& stream, const Mount& mount) {
285 stream << "mount:" << std::endl
286 << " name: " << QUOTE(mount.name) << std::endl
287 << " source: " << QUOTE(mount.source.value()) << std::endl
288 << " destination: " << QUOTE(mount.destination.value()) << std::endl
289 << " type: " << QUOTE(mount.type) << std::endl
290 << " data: " << QUOTE(mount.data) << std::endl
291 << " verity: " << QUOTE(mount.verity) << std::endl
yusukes4d955472018-01-17 16:41:32 -0800292 << " flags: 0x" << std::hex << mount.flags << std::dec << " ("
293 << GetMountFlagsAsString(mount.flags) << ")" << std::endl
yusukesbbc37a72017-11-21 09:51:54 -0800294 << " uid: " << mount.uid << std::endl
295 << " gid: " << mount.gid << std::endl
296 << " mode: 0" << std::oct << mount.mode << std::dec << std::endl
297 << " mount_in_ns: " << mount.mount_in_ns << std::endl
298 << " create: " << mount.create << std::endl
299 << " loopback: " << mount.loopback << std::endl;
300
301 return stream;
302}
303
304std::ostream& operator<<(std::ostream& stream, const Device& device) {
305 stream << "device:" << std::endl
306 << " type: " << device.type << std::endl
307 << " path: " << QUOTE(device.path.value()) << std::endl
308 << " fs_permissions: 0" << std::oct << device.fs_permissions
309 << std::dec << std::endl
310 << " major: " << device.major << std::endl
311 << " minor: " << device.minor << std::endl
312 << " copy_minor: " << device.copy_minor << std::endl
313 << " uid: " << device.uid << std::endl
314 << " gid: " << device.gid << std::endl;
315
316 return stream;
317}
318
319std::ostream& operator<<(std::ostream& stream,
320 const CgroupDevice& cgroup_device) {
321 stream << "cgroup_device:" << std::endl
322 << " allow: " << cgroup_device.allow << std::endl
323 << " type: " << cgroup_device.type << std::endl
324 << " major: " << cgroup_device.major << std::endl
325 << " minor: " << cgroup_device.minor << std::endl
326 << " read: " << cgroup_device.read << std::endl
327 << " write: " << cgroup_device.write << std::endl
328 << " modify: " << cgroup_device.modify << std::endl;
329
330 return stream;
331}
332
333std::ostream& operator<<(std::ostream& stream, const CpuCgroup& cpu_cgroup) {
334 stream << "cpu_cgroup:" << std::endl
335 << " shares: " << cpu_cgroup.shares << std::endl
336 << " quota: " << cpu_cgroup.quota << std::endl
337 << " period: " << cpu_cgroup.period << std::endl
338 << " rt_runtime: " << cpu_cgroup.rt_runtime << std::endl
339 << " rt_period: " << cpu_cgroup.rt_period << std::endl;
340
341 return stream;
342}
343
344std::ostream& operator<<(std::ostream& stream, const Rlimit& rlimit) {
345 stream << "rlimit:" << std::endl
346 << " type: " << rlimit.type << std::endl
347 << " cur: " << rlimit.cur << std::endl
348 << " max: " << rlimit.max << std::endl;
349
350 return stream;
351}
352
yusukes32622542018-01-05 18:59:52 -0800353void DumpConfig(std::ostream* stream,
354 const container_config* c,
355 bool sort_vectors) {
356 *stream << "config_root: " << QUOTE(c->config_root.value()) << std::endl
357 << "rootfs: " << QUOTE(c->rootfs.value()) << std::endl
358 << "rootfs_mount_flags: 0x" << std::hex << c->rootfs_mount_flags
yusukes4d955472018-01-17 16:41:32 -0800359 << std::dec << " (" << GetMountFlagsAsString(c->rootfs_mount_flags)
360 << ")" << std::endl
yusukes32622542018-01-05 18:59:52 -0800361 << "premounted_runfs: " << QUOTE(c->premounted_runfs.value())
362 << std::endl
363 << "pid_file_path: " << QUOTE(c->pid_file_path.value()) << std::endl
364 << "program_argv: size=" << c->program_argv.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800365
366 for (const std::string& argv : c->program_argv)
yusukes32622542018-01-05 18:59:52 -0800367 *stream << " " << QUOTE(argv) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800368
yusukes32622542018-01-05 18:59:52 -0800369 *stream << "uid: " << c->uid << std::endl
370 << "uid_map: " << QUOTE(c->uid_map) << std::endl
371 << "gid: " << c->gid << std::endl
372 << "gid_map: " << QUOTE(c->gid_map) << std::endl
373 << "alt_syscall_table: " << QUOTE(c->alt_syscall_table) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800374
yusukes32622542018-01-05 18:59:52 -0800375 auto mount_sorted = c->mounts;
376 if (sort_vectors) {
377 std::stable_sort(mount_sorted.begin(), mount_sorted.end(),
378 [](const Mount& lhs, const Mount& rhs) {
379 return std::make_tuple(lhs.destination.value(),
380 lhs.source.value(), lhs.flags) <
381 std::make_tuple(rhs.destination.value(),
382 rhs.source.value(), rhs.flags);
383 });
384 }
385 for (const auto& mount : mount_sorted)
386 *stream << mount;
yusukesbbc37a72017-11-21 09:51:54 -0800387
yusukes32622542018-01-05 18:59:52 -0800388 *stream << "namespaces: size=" << c->namespaces.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800389 for (const std::string& ns : c->namespaces)
yusukes32622542018-01-05 18:59:52 -0800390 *stream << " " << QUOTE(ns) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800391
yusukes32622542018-01-05 18:59:52 -0800392 auto devices_sorted = c->devices;
393 if (sort_vectors) {
394 std::stable_sort(devices_sorted.begin(), devices_sorted.end(),
395 [](const Device& lhs, const Device& rhs) {
396 return lhs.path.value() < rhs.path.value();
397 });
398 }
399 for (const auto& device : devices_sorted)
400 *stream << device;
yusukesbbc37a72017-11-21 09:51:54 -0800401
yusukes32622542018-01-05 18:59:52 -0800402 auto cgroup_devices_sorted = c->cgroup_devices;
403 if (sort_vectors) {
404 std::stable_sort(cgroup_devices_sorted.begin(), cgroup_devices_sorted.end(),
405 [](const CgroupDevice& lhs, const CgroupDevice& rhs) {
406 return std::make_tuple(lhs.type, lhs.major, lhs.minor) <
407 std::make_tuple(rhs.type, rhs.major, rhs.minor);
408 });
409 }
410 for (const auto& cgroup_device : cgroup_devices_sorted)
411 *stream << cgroup_device;
yusukesbbc37a72017-11-21 09:51:54 -0800412
yusukese67af442017-12-23 00:52:15 -0800413 *stream << c->cpu_cgparams
yusukes32622542018-01-05 18:59:52 -0800414 << "cgroup_parent: " << QUOTE(c->cgroup_parent.value()) << std::endl
415 << "cgroup_owner: " << c->cgroup_owner << std::endl
416 << "cgroup_group: " << c->cgroup_group << std::endl
417 << "keep_fds_open: " << c->keep_fds_open << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800418
yusukes32622542018-01-05 18:59:52 -0800419 *stream << "num_rlimits: " << c->num_rlimits << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800420 for (size_t i = 0; i < c->num_rlimits; ++i)
yusukes32622542018-01-05 18:59:52 -0800421 *stream << c->rlimits[i];
yusukesbbc37a72017-11-21 09:51:54 -0800422
yusukes32622542018-01-05 18:59:52 -0800423 *stream << "use_capmask: " << c->use_capmask << std::endl
424 << "use_capmask_ambient: " << c->use_capmask_ambient << std::endl
425 << "capmask: 0x" << std::hex << c->capmask << std::dec << std::endl
426 << "securebits_skip_mask: 0x" << std::hex << c->securebits_skip_mask
427 << std::dec << std::endl
428 << "do_init: " << c->do_init << std::endl
429 << "selinux_context: " << QUOTE(c->selinux_context) << std::endl
430 << "pre_start_hook: " << reinterpret_cast<void*>(c->pre_start_hook)
431 << std::endl
432 << "pre_start_hook_payload: " << c->pre_start_hook_payload
433 << std::endl
434 << "inherited_fds: size=" << c->inherited_fds.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800435
436 for (int fd : c->inherited_fds)
yusukes32622542018-01-05 18:59:52 -0800437 *stream << " " << fd << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800438
yusukes32622542018-01-05 18:59:52 -0800439 *stream << "hooks: size=" << c->hooks.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800440}
441
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700442// Returns the path for |path_in_container| in the outer namespace.
443base::FilePath GetPathInOuterNamespace(
444 const base::FilePath& root, const base::FilePath& path_in_container) {
445 if (path_in_container.IsAbsolute())
446 return base::FilePath(root.value() + path_in_container.value());
447 return root.Append(path_in_container);
448}
449
450// Make sure the mount target exists in the new rootfs. Create if needed and
451// possible.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700452bool SetupMountDestination(const struct container_config* config,
453 const Mount& mount,
454 const base::FilePath& source,
455 const base::FilePath& dest) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700456 struct stat st_buf;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700457 if (stat(dest.value().c_str(), &st_buf) == 0) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700458 // destination exists.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700459 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700460 }
461
462 // Try to create the destination. Either make directory or touch a file
463 // depending on the source type.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700464 int uid_userns;
465 if (!GetUsernsOutsideId(config->uid_map, mount.uid, &uid_userns))
466 return false;
467 int gid_userns;
468 if (!GetUsernsOutsideId(config->gid_map, mount.gid, &gid_userns))
469 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700470
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700471 if (stat(source.value().c_str(), &st_buf) != 0 || S_ISDIR(st_buf.st_mode) ||
472 S_ISBLK(st_buf.st_mode)) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700473 return MakeDir(dest, uid_userns, gid_userns, mount.mode);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700474 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700475
476 return TouchFile(dest, uid_userns, gid_userns, mount.mode);
477}
478
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700479// Unmounts anything we mounted in this mount namespace in the opposite order
480// that they were mounted.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700481bool UnmountExternalMounts(struct container* c) {
482 bool ret = true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700483
484 for (auto it = c->ext_mounts.rbegin(); it != c->ext_mounts.rend(); ++it) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700485 if (umount(it->value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700486 PLOG(ERROR) << "Failed to unmount " << it->value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700487 ret = false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700488 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700489 }
490 c->ext_mounts.clear();
491
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700492 for (auto it = c->loopdevs.rbegin(); it != c->loopdevs.rend(); ++it) {
493 if (!LoopdevDetach(&(*it)))
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700494 ret = false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700495 }
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700496 c->loopdevs.clear();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700497
498 for (auto it = c->device_mappers.rbegin(); it != c->device_mappers.rend();
499 ++it) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700500 if (!DeviceMapperDetach(*it))
501 ret = false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700502 }
503 c->device_mappers.clear();
504
505 return ret;
506}
507
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700508bool DoContainerMount(struct container* c,
509 const struct container_config* config,
510 const Mount& mount) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700511 base::FilePath dest =
512 GetPathInOuterNamespace(c->runfsroot, mount.destination);
513
514 // If it's a bind mount relative to rootfs, append source to
515 // rootfs path, otherwise source path is absolute.
516 base::FilePath source;
517 if ((mount.flags & MS_BIND) && !mount.source.IsAbsolute()) {
518 source = GetPathInOuterNamespace(c->runfsroot, mount.source);
519 } else if (mount.loopback && !mount.source.IsAbsolute() &&
520 !c->config_root.empty()) {
521 source = GetPathInOuterNamespace(c->config_root, mount.source);
522 } else {
523 source = mount.source;
524 }
525
526 // Only create the destinations for external mounts, minijail will take
527 // care of those mounted in the new namespace.
528 if (mount.create && !mount.mount_in_ns) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700529 if (!SetupMountDestination(config, mount, source, dest))
530 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700531 }
532 if (mount.loopback) {
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700533 Loopdev loopdev;
534 if (!LoopdevSetup(source, &loopdev))
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700535 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700536
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700537 // Replace the mount source with the loopback device path.
538 source = loopdev.path;
539
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700540 // Save this to cleanup when shutting down.
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700541 c->loopdevs.emplace_back(std::move(loopdev));
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700542 }
543 if (!mount.verity.empty()) {
544 // Set this device up via dm-verity.
545 std::string dm_name;
546 base::FilePath dm_source = source;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700547 if (!DeviceMapperSetup(dm_source, mount.verity, &source, &dm_name))
548 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700549
550 // Save this to cleanup when shutting down.
551 c->device_mappers.push_back(dm_name);
552 }
553 if (mount.mount_in_ns) {
554 // We can mount this with minijail.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700555 if (minijail_mount_with_data(
556 c->jail.get(), source.value().c_str(),
557 mount.destination.value().c_str(), mount.type.c_str(), mount.flags,
558 mount.data.empty() ? nullptr : mount.data.c_str()) != 0) {
559 return false;
560 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700561 } else {
562 // Mount this externally and unmount it on exit.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700563 if (!MountExternal(source.value(), dest.value(), mount.type, mount.flags,
564 mount.data)) {
565 return false;
566 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700567 // Save this to unmount when shutting down.
568 c->ext_mounts.push_back(dest);
569 }
570
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700571 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700572}
573
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700574bool DoContainerMounts(struct container* c,
575 const struct container_config* config) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700576 UnmountExternalMounts(c);
577
578 // This will run in all the error cases.
579 base::ScopedClosureRunner teardown(base::Bind(
580 base::IgnoreResult(&UnmountExternalMounts), base::Unretained(c)));
581
582 for (const auto& mount : config->mounts) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700583 if (!DoContainerMount(c, config, mount))
584 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700585 }
586
587 // The mounts have been done successfully, no need to tear them down anymore.
588 ignore_result(teardown.Release());
589
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700590 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700591}
592
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700593bool ContainerCreateDevice(const struct container* c,
594 const struct container_config* config,
595 const Device& dev,
Stephen Barber7bae6642017-11-30 10:47:12 -0800596 int major,
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700597 int minor) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700598 mode_t mode = dev.fs_permissions;
599 switch (dev.type) {
600 case 'b':
601 mode |= S_IFBLK;
602 break;
603 case 'c':
604 mode |= S_IFCHR;
605 break;
606 default:
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700607 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700608 }
609
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700610 int uid_userns;
611 if (!GetUsernsOutsideId(config->uid_map, dev.uid, &uid_userns))
612 return false;
613 int gid_userns;
614 if (!GetUsernsOutsideId(config->gid_map, dev.gid, &gid_userns))
615 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700616
617 base::FilePath path = GetPathInOuterNamespace(c->runfsroot, dev.path);
Luis Hector Chavez92278e82017-10-16 11:30:27 -0700618 if (!libcontainer::CreateDirectoryOwnedBy(path.DirName(), 0755, uid_userns,
619 gid_userns)) {
Luis Hector Chavez5d51abb2017-10-11 17:05:57 -0700620 PLOG(ERROR) << "Failed to create parent directory for " << path.value();
621 return false;
622 }
Stephen Barber7bae6642017-11-30 10:47:12 -0800623 if (mknod(path.value().c_str(), mode, makedev(major, minor)) != 0 &&
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700624 errno != EEXIST) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700625 PLOG(ERROR) << "Failed to mknod " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700626 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700627 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700628 if (chown(path.value().c_str(), uid_userns, gid_userns) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700629 PLOG(ERROR) << "Failed to chown " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700630 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700631 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700632 if (chmod(path.value().c_str(), dev.fs_permissions) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700633 PLOG(ERROR) << "Failed to chmod " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700634 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700635 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700636
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700637 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700638}
639
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700640bool MountRunfs(struct container* c, const struct container_config* config) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700641 {
642 std::string runfs_template = base::StringPrintf(
643 "%s/%s_XXXXXX", c->rundir.value().c_str(), c->name.c_str());
644 // TODO(lhchavez): Replace this with base::CreateTemporaryDirInDir().
645 char* runfs_path = mkdtemp(const_cast<char*>(runfs_template.c_str()));
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700646 if (!runfs_path) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700647 PLOG(ERROR) << "Failed to mkdtemp in " << c->rundir.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700648 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700649 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700650 c->runfs = base::FilePath(runfs_path);
651 }
652
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700653 int uid_userns;
654 if (!GetUsernsOutsideId(config->uid_map, config->uid, &uid_userns))
655 return false;
656 int gid_userns;
657 if (!GetUsernsOutsideId(config->gid_map, config->gid, &gid_userns))
658 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700659
660 // Make sure the container uid can access the rootfs.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700661 if (chmod(c->runfs.value().c_str(), 0700) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700662 PLOG(ERROR) << "Failed to chmod " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700663 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700664 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700665 if (chown(c->runfs.value().c_str(), uid_userns, gid_userns) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700666 PLOG(ERROR) << "Failed to chown " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700667 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700668 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700669
670 c->runfsroot = c->runfs.Append("root");
671
672 constexpr mode_t kRootDirMode = 0660;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700673 if (mkdir(c->runfsroot.value().c_str(), kRootDirMode) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700674 PLOG(ERROR) << "Failed to mkdir " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700675 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700676 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700677 if (chmod(c->runfsroot.value().c_str(), kRootDirMode) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700678 PLOG(ERROR) << "Failed to chmod " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700679 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700680 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700681
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700682 if (mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "",
683 MS_BIND | (config->rootfs_mount_flags & MS_REC), nullptr) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700684 PLOG(ERROR) << "Failed to bind-mount " << config->rootfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700685 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700686 }
687
688 // MS_BIND ignores any flags passed to it (except MS_REC). We need a
689 // second call to mount() to actually set them.
690 if (config->rootfs_mount_flags &&
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700691 mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "",
692 (config->rootfs_mount_flags & ~MS_REC), nullptr) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700693 PLOG(ERROR) << "Failed to remount " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700694 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700695 }
696
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700697 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700698}
699
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700700bool CreateDeviceNodes(struct container* c,
701 const struct container_config* config,
702 pid_t container_pid) {
703 for (const auto& dev : config->devices) {
Stephen Barber7bae6642017-11-30 10:47:12 -0800704 int major = dev.major;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700705 int minor = dev.minor;
706
Stephen Barber7bae6642017-11-30 10:47:12 -0800707 if (dev.copy_major || dev.copy_minor) {
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700708 struct stat st_buff;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700709 if (stat(dev.path.value().c_str(), &st_buff) != 0)
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700710 continue;
Stephen Barber7bae6642017-11-30 10:47:12 -0800711
712 if (dev.copy_major)
713 major = major(st_buff.st_rdev);
714 if (dev.copy_minor)
715 minor = minor(st_buff.st_rdev);
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700716 }
Stephen Barber7bae6642017-11-30 10:47:12 -0800717 if (major < 0 || minor < 0)
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700718 continue;
Stephen Barber7bae6642017-11-30 10:47:12 -0800719 if (!ContainerCreateDevice(c, config, dev, major, minor))
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700720 return false;
721 }
722
723 return true;
724}
725
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700726bool DeviceSetup(struct container* c, const struct container_config* config) {
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -0700727 c->cgroup->DenyAllDevices();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700728
729 for (const auto& dev : config->cgroup_devices) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700730 if (!c->cgroup->AddDevice(dev.allow, dev.major, dev.minor, dev.read,
731 dev.write, dev.modify, dev.type)) {
732 return false;
733 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700734 }
735
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700736 for (const auto& loopdev : c->loopdevs) {
737 if (!c->cgroup->AddDevice(1, major(loopdev.info.lo_rdevice),
738 minor(loopdev.info.lo_rdevice), 1, 0, 0, 'b')) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700739 return false;
740 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700741 }
742
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700743 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700744}
745
746int Setexeccon(void* payload) {
747 char* init_domain = reinterpret_cast<char*>(payload);
748 pid_t tid = syscall(SYS_gettid);
749
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700750 if (tid < 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700751 PLOG(ERROR) << "Failed to gettid";
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700752 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700753 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700754
755 std::string exec_path =
756 base::StringPrintf("/proc/self/task/%d/attr/exec", tid);
757
758 base::ScopedFD fd(open(exec_path.c_str(), O_WRONLY | O_CLOEXEC));
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700759 if (!fd.is_valid()) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700760 PLOG(ERROR) << "Failed to open " << exec_path;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700761 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700762 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700763
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700764 if (!base::WriteFileDescriptor(fd.get(), init_domain, strlen(init_domain))) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700765 PLOG(ERROR) << "Failed to write the SELinux label to " << exec_path;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700766 return -errno;
767 }
768
769 return 0;
770}
771
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700772bool ContainerTeardown(struct container* c) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700773 UnmountExternalMounts(c);
774 if (!c->runfsroot.empty() && !c->runfs.empty()) {
775 /* |c->runfsroot| may have been mounted recursively. Thus use
776 * MNT_DETACH to "immediately disconnect the filesystem and all
777 * filesystems mounted below it from each other and from the
778 * mount table". Otherwise one would need to unmount every
779 * single dependent mount before unmounting |c->runfsroot|
780 * itself.
781 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700782 if (umount2(c->runfsroot.value().c_str(), MNT_DETACH) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700783 PLOG(ERROR) << "Failed to detach " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700784 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700785 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700786 if (rmdir(c->runfsroot.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700787 PLOG(ERROR) << "Failed to rmdir " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700788 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700789 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700790 c->runfsroot = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700791 }
792 if (!c->pid_file_path.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700793 if (unlink(c->pid_file_path.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700794 PLOG(ERROR) << "Failed to unlink " << c->pid_file_path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700795 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700796 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700797 c->pid_file_path = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700798 }
799 if (!c->runfs.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700800 if (rmdir(c->runfs.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700801 PLOG(ERROR) << "Failed to rmdir " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700802 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700803 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700804 c->runfs = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700805 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700806 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700807}
808
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700809void CancelContainerStart(struct container* c) {
810 if (c->init_pid != -1)
811 container_kill(c);
812 ContainerTeardown(c);
813}
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700814
815} // namespace
816
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700817struct container_config* container_config_create() {
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700818 return new (std::nothrow) struct container_config();
Dylan Reid837c74a2016-01-22 17:25:21 -0800819}
820
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700821void container_config_destroy(struct container_config* c) {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700822 if (c == nullptr)
823 return;
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700824 delete c;
Dylan Reid837c74a2016-01-22 17:25:21 -0800825}
826
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700827int container_config_config_root(struct container_config* c,
828 const char* config_root) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700829 c->config_root = base::FilePath(config_root);
830 return 0;
Mike Frysingerb22acdf2017-01-08 02:02:35 -0500831}
832
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700833const char* container_config_get_config_root(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700834 return c->config_root.value().c_str();
Mike Frysingerb22acdf2017-01-08 02:02:35 -0500835}
836
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700837int container_config_rootfs(struct container_config* c, const char* rootfs) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700838 c->rootfs = base::FilePath(rootfs);
839 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800840}
841
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700842const char* container_config_get_rootfs(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700843 return c->rootfs.value().c_str();
Dylan Reid11456722016-05-02 11:24:50 -0700844}
845
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700846void container_config_rootfs_mount_flags(struct container_config* c,
847 unsigned long rootfs_mount_flags) {
848 /* Since we are going to add MS_REMOUNT anyways, add it here so we can
849 * simply check against zero later. MS_BIND is also added to avoid
850 * re-mounting the original filesystem, since the rootfs is always
851 * bind-mounted.
852 */
853 c->rootfs_mount_flags = MS_REMOUNT | MS_BIND | rootfs_mount_flags;
Luis Hector Chavezc240e7e2016-09-22 10:33:03 -0700854}
855
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700856unsigned long container_config_get_rootfs_mount_flags(
857 const struct container_config* c) {
858 return c->rootfs_mount_flags;
Luis Hector Chavezc240e7e2016-09-22 10:33:03 -0700859}
860
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700861int container_config_premounted_runfs(struct container_config* c,
862 const char* runfs) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700863 c->premounted_runfs = base::FilePath(runfs);
864 return 0;
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700865}
866
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700867const char* container_config_get_premounted_runfs(
868 const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700869 return c->premounted_runfs.value().c_str();
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700870}
871
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700872int container_config_pid_file(struct container_config* c, const char* path) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700873 c->pid_file_path = base::FilePath(path);
874 return 0;
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700875}
876
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700877const char* container_config_get_pid_file(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700878 return c->pid_file_path.value().c_str();
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700879}
880
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700881int container_config_program_argv(struct container_config* c,
882 const char** argv,
883 size_t num_args) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700884 if (num_args < 1) {
885 errno = EINVAL;
886 return -1;
887 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700888 c->program_argv.clear();
889 c->program_argv.reserve(num_args);
890 for (size_t i = 0; i < num_args; ++i)
891 c->program_argv.emplace_back(argv[i]);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700892 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800893}
894
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700895size_t container_config_get_num_program_args(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700896 return c->program_argv.size();
Dylan Reid11456722016-05-02 11:24:50 -0700897}
898
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700899const char* container_config_get_program_arg(const struct container_config* c,
900 size_t index) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700901 if (index >= c->program_argv.size())
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700902 return nullptr;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700903 return c->program_argv[index].c_str();
Dylan Reid11456722016-05-02 11:24:50 -0700904}
905
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700906void container_config_uid(struct container_config* c, uid_t uid) {
907 c->uid = uid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700908}
909
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700910uid_t container_config_get_uid(const struct container_config* c) {
911 return c->uid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700912}
913
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700914int container_config_uid_map(struct container_config* c, const char* uid_map) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700915 c->uid_map = uid_map;
916 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800917}
918
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700919void container_config_gid(struct container_config* c, gid_t gid) {
920 c->gid = gid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700921}
922
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700923gid_t container_config_get_gid(const struct container_config* c) {
924 return c->gid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700925}
926
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700927int container_config_gid_map(struct container_config* c, const char* gid_map) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700928 c->gid_map = gid_map;
929 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800930}
931
Risanfd41aee2018-08-15 14:03:38 +0900932void container_config_additional_gids(struct container_config* c,
933 const gid_t* gids,
934 size_t num_gids) {
935 c->additional_gids.assign(gids, gids + num_gids);
936}
937
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700938int container_config_alt_syscall_table(struct container_config* c,
939 const char* alt_syscall_table) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700940 c->alt_syscall_table = alt_syscall_table;
941 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800942}
943
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700944int container_config_add_rlimit(struct container_config* c,
945 int type,
Luis Hector Chavezda352462018-01-30 09:10:00 -0800946 rlim_t cur,
947 rlim_t max) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700948 if (c->num_rlimits >= kMaxRlimits) {
949 errno = ENOMEM;
950 return -1;
951 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700952 c->rlimits[c->num_rlimits].type = type;
953 c->rlimits[c->num_rlimits].cur = cur;
954 c->rlimits[c->num_rlimits].max = max;
955 c->num_rlimits++;
956 return 0;
Dylan Reid93fa4602017-06-06 13:39:31 -0700957}
958
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700959int container_config_add_mount(struct container_config* c,
960 const char* name,
961 const char* source,
962 const char* destination,
963 const char* type,
964 const char* data,
965 const char* verity,
966 int flags,
967 int uid,
968 int gid,
969 int mode,
970 int mount_in_ns,
971 int create,
972 int loopback) {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700973 if (name == nullptr || source == nullptr || destination == nullptr ||
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700974 type == nullptr) {
975 errno = EINVAL;
976 return -1;
977 }
Dylan Reid837c74a2016-01-22 17:25:21 -0800978
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700979 c->mounts.emplace_back(Mount{name,
980 base::FilePath(source),
981 base::FilePath(destination),
982 type,
983 data ? data : "",
984 verity ? verity : "",
985 flags,
986 uid,
987 gid,
988 mode,
989 mount_in_ns != 0,
990 create != 0,
991 loopback != 0});
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700992
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700993 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800994}
995
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700996int container_config_add_cgroup_device(struct container_config* c,
997 int allow,
998 char type,
999 int major,
1000 int minor,
1001 int read,
1002 int write,
1003 int modify) {
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001004 c->cgroup_devices.emplace_back(CgroupDevice{
1005 allow != 0, type, major, minor, read != 0, write != 0, modify != 0});
Dylan Reid4843d6b2017-03-31 18:14:30 -07001006
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001007 return 0;
Dylan Reid4843d6b2017-03-31 18:14:30 -07001008}
1009
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001010int container_config_add_device(struct container_config* c,
1011 char type,
1012 const char* path,
1013 int fs_permissions,
1014 int major,
1015 int minor,
Stephen Barber7bae6642017-11-30 10:47:12 -08001016 int copy_major,
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001017 int copy_minor,
1018 int uid,
1019 int gid,
1020 int read_allowed,
1021 int write_allowed,
1022 int modify_allowed) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001023 if (path == nullptr) {
1024 errno = EINVAL;
1025 return -1;
1026 }
Stephen Barber7bae6642017-11-30 10:47:12 -08001027 /* If using a dynamic major/minor number, ensure that major/minor is -1. */
1028 if ((copy_major && (major != -1)) || (copy_minor && (minor != -1))) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001029 errno = EINVAL;
1030 return -1;
1031 }
Dylan Reid355d5e42016-04-29 16:53:31 -07001032
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001033 if (read_allowed || write_allowed || modify_allowed) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001034 if (container_config_add_cgroup_device(c, 1, type, major, minor,
1035 read_allowed, write_allowed,
1036 modify_allowed) != 0) {
1037 errno = ENOMEM;
1038 return -1;
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001039 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001040 }
Luis Hector Chavez479b95f2016-06-06 08:01:05 -07001041
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001042 c->devices.emplace_back(Device{
Stephen Barber7bae6642017-11-30 10:47:12 -08001043 type, base::FilePath(path), fs_permissions, major, minor, copy_major != 0,
1044 copy_minor != 0, uid, gid,
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001045 });
1046
1047 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -08001048}
1049
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001050int container_config_set_cpu_shares(struct container_config* c, int shares) {
1051 /* CPU shares must be 2 or higher. */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001052 if (shares < 2) {
1053 errno = EINVAL;
1054 return -1;
1055 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001056
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001057 c->cpu_cgparams.shares = shares;
1058 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001059}
1060
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001061int container_config_set_cpu_cfs_params(struct container_config* c,
1062 int quota,
1063 int period) {
1064 /*
1065 * quota could be set higher than period to utilize more than one CPU.
1066 * quota could also be set as -1 to indicate the cgroup does not adhere
1067 * to any CPU time restrictions.
1068 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001069 if (quota <= 0 && quota != -1) {
1070 errno = EINVAL;
1071 return -1;
1072 }
1073 if (period <= 0) {
1074 errno = EINVAL;
1075 return -1;
1076 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001077
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001078 c->cpu_cgparams.quota = quota;
1079 c->cpu_cgparams.period = period;
1080 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001081}
1082
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001083int container_config_set_cpu_rt_params(struct container_config* c,
1084 int rt_runtime,
1085 int rt_period) {
1086 /*
1087 * rt_runtime could be set as 0 to prevent the cgroup from using
1088 * realtime CPU.
1089 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001090 if (rt_runtime < 0 || rt_runtime >= rt_period) {
1091 errno = EINVAL;
1092 return -1;
1093 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001094
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001095 c->cpu_cgparams.rt_runtime = rt_runtime;
1096 c->cpu_cgparams.rt_period = rt_period;
1097 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001098}
1099
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001100int container_config_get_cpu_shares(struct container_config* c) {
1101 return c->cpu_cgparams.shares;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001102}
1103
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001104int container_config_get_cpu_quota(struct container_config* c) {
1105 return c->cpu_cgparams.quota;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001106}
1107
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001108int container_config_get_cpu_period(struct container_config* c) {
1109 return c->cpu_cgparams.period;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001110}
1111
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001112int container_config_get_cpu_rt_runtime(struct container_config* c) {
1113 return c->cpu_cgparams.rt_runtime;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001114}
1115
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001116int container_config_get_cpu_rt_period(struct container_config* c) {
1117 return c->cpu_cgparams.rt_period;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001118}
1119
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001120int container_config_set_cgroup_parent(struct container_config* c,
1121 const char* parent,
1122 uid_t cgroup_owner,
1123 gid_t cgroup_group) {
1124 c->cgroup_owner = cgroup_owner;
1125 c->cgroup_group = cgroup_group;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001126 c->cgroup_parent = base::FilePath(parent);
1127 return 0;
Dylan Reid9e724af2016-07-21 09:58:07 -07001128}
1129
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001130const char* container_config_get_cgroup_parent(struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001131 return c->cgroup_parent.value().c_str();
Dylan Reid9e724af2016-07-21 09:58:07 -07001132}
1133
Stephen Barber771653f2017-10-04 23:48:57 -07001134int container_config_namespaces(struct container_config* c,
1135 const char** namespaces,
1136 size_t num_ns) {
1137 if (num_ns < 1)
1138 return -EINVAL;
1139 c->namespaces.clear();
1140 for (size_t i = 0; i < num_ns; ++i)
1141 c->namespaces.emplace(namespaces[i]);
1142 return 0;
Keshav Santhanam1b6bf672016-08-10 18:35:12 -07001143}
1144
Stephen Barber771653f2017-10-04 23:48:57 -07001145size_t container_config_get_num_namespaces(const struct container_config* c) {
1146 return c->namespaces.size();
1147}
1148
1149bool container_config_has_namespace(const struct container_config* c,
1150 const char* ns) {
1151 return c->namespaces.find(ns) != c->namespaces.end();
Keshav Santhanam1b6bf672016-08-10 18:35:12 -07001152}
1153
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001154void container_config_keep_fds_open(struct container_config* c) {
yusukesf125f332017-12-08 13:45:15 -08001155 c->keep_fds_open = true;
Dylan Reidc4335842016-11-11 10:24:52 -08001156}
1157
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001158void container_config_set_capmask(struct container_config* c,
1159 uint64_t capmask,
1160 int ambient) {
yusukesf125f332017-12-08 13:45:15 -08001161 c->use_capmask = true;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001162 c->capmask = capmask;
1163 c->use_capmask_ambient = ambient;
Luis Hector Chavezff5978f2017-06-27 12:52:58 -07001164}
1165
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001166void container_config_set_securebits_skip_mask(struct container_config* c,
1167 uint64_t securebits_skip_mask) {
1168 c->securebits_skip_mask = securebits_skip_mask;
Luis Hector Chavezcd44ba72017-06-30 13:01:38 -07001169}
1170
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001171void container_config_set_run_as_init(struct container_config* c,
1172 int run_as_init) {
1173 c->do_init = !run_as_init;
Luis Hector Chavezdac65c32017-07-21 10:30:23 -07001174}
1175
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001176int container_config_set_selinux_context(struct container_config* c,
1177 const char* context) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001178 if (!context) {
1179 errno = EINVAL;
1180 return -1;
1181 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001182 c->selinux_context = context;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001183 return 0;
Luis Hector Chavez15e8e672017-07-20 15:13:27 -07001184}
1185
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001186void container_config_set_pre_execve_hook(struct container_config* c,
1187 int (*hook)(void*),
1188 void* payload) {
1189 c->pre_start_hook = hook;
1190 c->pre_start_hook_payload = payload;
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001191}
1192
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001193void container_config_add_hook(struct container_config* c,
1194 minijail_hook_event_t event,
1195 libcontainer::HookCallback callback) {
1196 auto it = c->hooks.insert(
1197 std::make_pair(event, std::vector<libcontainer::HookCallback>()));
1198 it.first->second.emplace_back(std::move(callback));
1199}
1200
Luis Hector Chaveze03926a2017-09-28 17:28:49 -07001201int container_config_add_hook(struct container_config* c,
1202 minijail_hook_event_t event,
1203 const char* filename,
1204 const char** argv,
1205 size_t num_args,
1206 int* pstdin_fd,
1207 int* pstdout_fd,
1208 int* pstderr_fd) {
1209 std::vector<std::string> args;
1210 args.reserve(num_args);
1211 for (size_t i = 0; i < num_args; ++i)
1212 args.emplace_back(argv[i]);
1213
1214 // First element of the array belongs to the parent and the second one belongs
1215 // to the child.
1216 base::ScopedFD stdin_fds[2], stdout_fds[2], stderr_fds[2];
1217 if (pstdin_fd) {
1218 if (!libcontainer::Pipe2(&stdin_fds[1], &stdin_fds[0], 0))
1219 return -1;
1220 }
1221 if (pstdout_fd) {
1222 if (!libcontainer::Pipe2(&stdout_fds[0], &stdout_fds[0], 0))
1223 return -1;
1224 }
1225 if (pstderr_fd) {
1226 if (!libcontainer::Pipe2(&stderr_fds[0], &stderr_fds[0], 0))
1227 return -1;
1228 }
1229
1230 // After this point the call has been successful, so we can now commit to
1231 // whatever pipes we have opened.
1232 if (pstdin_fd) {
1233 *pstdin_fd = stdin_fds[0].release();
1234 c->inherited_fds.emplace_back(stdin_fds[1].get());
1235 }
1236 if (pstdout_fd) {
1237 *pstdout_fd = stdout_fds[0].release();
1238 c->inherited_fds.emplace_back(stdout_fds[1].get());
1239 }
1240 if (pstderr_fd) {
1241 *pstderr_fd = stderr_fds[0].release();
1242 c->inherited_fds.emplace_back(stderr_fds[1].get());
1243 }
1244 container_config_add_hook(
1245 c, event,
1246 libcontainer::CreateExecveCallback(
1247 base::FilePath(filename), args, std::move(stdin_fds[1]),
1248 std::move(stdout_fds[1]), std::move(stderr_fds[1])));
1249 return 0;
1250}
1251
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001252int container_config_inherit_fds(struct container_config* c,
Luis Hector Chaveza5e87cb2018-05-21 07:21:22 -07001253 const int* inherited_fds,
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001254 size_t inherited_fd_count) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001255 if (!c->inherited_fds.empty()) {
1256 errno = EINVAL;
1257 return -1;
1258 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001259 for (size_t i = 0; i < inherited_fd_count; ++i)
1260 c->inherited_fds.emplace_back(inherited_fds[i]);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001261 return 0;
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001262}
1263
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001264struct container* container_new(const char* name, const char* rundir) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001265 struct container* c = new (std::nothrow) container();
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001266 if (!c)
1267 return nullptr;
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001268 c->rundir = base::FilePath(rundir);
1269 c->name = name;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001270 return c;
Dylan Reid837c74a2016-01-22 17:25:21 -08001271}
1272
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001273void container_destroy(struct container* c) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001274 delete c;
Dylan Reid837c74a2016-01-22 17:25:21 -08001275}
1276
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001277int container_start(struct container* c,
1278 const struct container_config* config) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001279 if (!c) {
1280 errno = EINVAL;
1281 return -1;
1282 }
1283 if (!config) {
1284 errno = EINVAL;
1285 return -1;
1286 }
1287 if (config->program_argv.empty()) {
1288 errno = EINVAL;
1289 return -1;
1290 }
Dylan Reide040c6b2016-05-02 18:49:02 -07001291
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001292 // This will run in all the error cases.
1293 base::ScopedClosureRunner teardown(
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -07001294 base::Bind(&CancelContainerStart, base::Unretained(c)));
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001295
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001296 if (!config->config_root.empty())
1297 c->config_root = config->config_root;
1298 if (!config->premounted_runfs.empty()) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001299 c->runfs.clear();
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001300 c->runfsroot = config->premounted_runfs;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001301 } else {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001302 if (!MountRunfs(c, config))
1303 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001304 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001305
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001306 c->jail.reset(minijail_new());
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001307 if (!c->jail) {
1308 errno = ENOMEM;
1309 return -1;
1310 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001311
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001312 if (!DoContainerMounts(c, config))
1313 return -1;
Dylan Reid837c74a2016-01-22 17:25:21 -08001314
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001315 int cgroup_uid;
1316 if (!GetUsernsOutsideId(config->uid_map, config->cgroup_owner, &cgroup_uid))
1317 return -1;
1318 int cgroup_gid;
1319 if (!GetUsernsOutsideId(config->gid_map, config->cgroup_group, &cgroup_gid))
1320 return -1;
Stephen Barber1a398c72017-01-23 12:39:44 -08001321
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -07001322 c->cgroup = libcontainer::Cgroup::Create(c->name,
1323 base::FilePath("/sys/fs/cgroup"),
1324 config->cgroup_parent,
1325 cgroup_uid,
1326 cgroup_gid);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001327 if (!c->cgroup)
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001328 return -1;
Dylan Reida9966422016-07-21 10:11:34 -07001329
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001330 // Must be root to modify device cgroup or mknod.
1331 std::map<minijail_hook_event_t, std::vector<libcontainer::HookCallback>>
1332 hook_callbacks;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001333 if (getuid() == 0) {
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001334 if (!config->devices.empty()) {
1335 // Create the devices in the mount namespace.
1336 auto it = hook_callbacks.insert(
1337 std::make_pair(MINIJAIL_HOOK_EVENT_PRE_CHROOT,
1338 std::vector<libcontainer::HookCallback>()));
1339 it.first->second.emplace_back(
1340 libcontainer::AdaptCallbackToRunInNamespaces(
1341 base::Bind(&CreateDeviceNodes, base::Unretained(c),
1342 base::Unretained(config)),
1343 {CLONE_NEWNS}));
1344 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001345 if (!DeviceSetup(c, config))
1346 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001347 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001348
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001349 /* Setup CPU cgroup params. */
1350 if (config->cpu_cgparams.shares) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001351 if (!c->cgroup->SetCpuShares(config->cpu_cgparams.shares))
1352 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001353 }
1354 if (config->cpu_cgparams.period) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001355 if (!c->cgroup->SetCpuQuota(config->cpu_cgparams.quota))
1356 return -1;
1357 if (!c->cgroup->SetCpuPeriod(config->cpu_cgparams.period))
1358 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001359 }
1360 if (config->cpu_cgparams.rt_period) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001361 if (!c->cgroup->SetCpuRtRuntime(config->cpu_cgparams.rt_runtime))
1362 return -1;
1363 if (!c->cgroup->SetCpuRtPeriod(config->cpu_cgparams.rt_period))
1364 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001365 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001366
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001367 /* Setup and start the container with libminijail. */
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001368 if (!config->pid_file_path.empty())
1369 c->pid_file_path = config->pid_file_path;
1370 else if (!c->runfs.empty())
1371 c->pid_file_path = c->runfs.Append("container.pid");
Keshav Santhanam0e4c3282016-07-14 10:25:16 -07001372
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001373 if (!c->pid_file_path.empty())
1374 minijail_write_pid_file(c->jail.get(), c->pid_file_path.value().c_str());
1375 minijail_reset_signal_mask(c->jail.get());
Luis Hector Chavezcd9a6b62018-04-04 08:39:44 -07001376 minijail_reset_signal_handlers(c->jail.get());
Dylan Reid837c74a2016-01-22 17:25:21 -08001377
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001378 /* Setup container namespaces. */
Stephen Barber771653f2017-10-04 23:48:57 -07001379 if (container_config_has_namespace(config, "ipc"))
1380 minijail_namespace_ipc(c->jail.get());
1381 if (container_config_has_namespace(config, "mount"))
1382 minijail_namespace_vfs(c->jail.get());
1383 if (container_config_has_namespace(config, "network"))
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001384 minijail_namespace_net(c->jail.get());
Stephen Barber771653f2017-10-04 23:48:57 -07001385 if (container_config_has_namespace(config, "pid"))
1386 minijail_namespace_pids(c->jail.get());
1387
1388 if (container_config_has_namespace(config, "user")) {
1389 minijail_namespace_user(c->jail.get());
1390 if (minijail_uidmap(c->jail.get(), config->uid_map.c_str()) != 0)
1391 return -1;
1392 if (minijail_gidmap(c->jail.get(), config->gid_map.c_str()) != 0)
1393 return -1;
1394 }
1395
1396 if (container_config_has_namespace(config, "cgroup"))
1397 minijail_namespace_cgroups(c->jail.get());
1398
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001399 if (getuid() != 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001400 minijail_namespace_user_disable_setgroups(c->jail.get());
Dylan Reid837c74a2016-01-22 17:25:21 -08001401
Risanfd41aee2018-08-15 14:03:38 +09001402 // Set the UID/GID inside the container if not 0.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001403 if (!GetUsernsOutsideId(config->uid_map, config->uid, nullptr))
1404 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001405 else if (config->uid > 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001406 minijail_change_uid(c->jail.get(), config->uid);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001407 if (!GetUsernsOutsideId(config->gid_map, config->gid, nullptr))
1408 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001409 else if (config->gid > 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001410 minijail_change_gid(c->jail.get(), config->gid);
Keshav Santhanam36485ff2016-08-02 16:21:02 -07001411
Risanfd41aee2018-08-15 14:03:38 +09001412 // Set the supplementary GIDs inside the container, if specified.
1413 if (!config->additional_gids.empty()) {
1414 for (const gid_t additional_gid : config->additional_gids) {
1415 if (!GetUsernsOutsideId(config->gid_map, additional_gid, nullptr))
1416 return -1;
1417 }
1418 minijail_set_supplementary_gids(c->jail.get(),
1419 config->additional_gids.size(),
1420 config->additional_gids.data());
1421 }
1422
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001423 if (minijail_enter_pivot_root(c->jail.get(), c->runfsroot.value().c_str()) !=
1424 0) {
1425 return -1;
1426 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001427
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -07001428 // Add the cgroups configured above.
1429 for (int32_t i = 0; i < libcontainer::Cgroup::Type::NUM_TYPES; i++) {
1430 if (c->cgroup->has_tasks_path(i)) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001431 if (minijail_add_to_cgroup(
1432 c->jail.get(), c->cgroup->tasks_path(i).value().c_str()) != 0) {
1433 return -1;
1434 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001435 }
1436 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001437
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001438 if (!config->alt_syscall_table.empty())
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001439 minijail_use_alt_syscall(c->jail.get(), config->alt_syscall_table.c_str());
Dylan Reid837c74a2016-01-22 17:25:21 -08001440
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001441 for (int i = 0; i < config->num_rlimits; i++) {
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001442 const Rlimit& lim = config->rlimits[i];
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001443 if (minijail_rlimit(c->jail.get(), lim.type, lim.cur, lim.max) != 0)
1444 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001445 }
Dylan Reid93fa4602017-06-06 13:39:31 -07001446
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001447 if (!config->selinux_context.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001448 if (minijail_add_hook(c->jail.get(), &Setexeccon,
1449 const_cast<char*>(config->selinux_context.c_str()),
1450 MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) {
1451 return -1;
1452 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001453 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001454
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001455 if (config->pre_start_hook) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001456 if (minijail_add_hook(c->jail.get(), config->pre_start_hook,
1457 config->pre_start_hook_payload,
1458 MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) {
1459 return -1;
1460 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001461 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001462
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001463 // Now that all pre-requisite hooks are installed, copy the ones in the
1464 // container_config object in the correct order.
1465 for (const auto& config_hook : config->hooks) {
1466 auto it = hook_callbacks.insert(std::make_pair(
1467 config_hook.first, std::vector<libcontainer::HookCallback>()));
1468 it.first->second.insert(it.first->second.end(), config_hook.second.begin(),
1469 config_hook.second.end());
1470 }
1471
1472 c->hook_states.clear();
1473 // Reserve enough memory to hold all the hooks, so that their addresses do not
1474 // get invalidated by reallocation.
1475 c->hook_states.reserve(MINIJAIL_HOOK_EVENT_MAX);
1476 for (minijail_hook_event_t event : {MINIJAIL_HOOK_EVENT_PRE_CHROOT,
1477 MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS,
1478 MINIJAIL_HOOK_EVENT_PRE_EXECVE}) {
1479 const auto& it = hook_callbacks.find(event);
1480 if (it == hook_callbacks.end())
1481 continue;
1482 c->hook_states.emplace_back(
1483 std::make_pair(libcontainer::HookState(), it->second));
1484 if (!c->hook_states.back().first.InstallHook(c->jail.get(), event))
1485 return -1;
1486 }
1487
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001488 for (int fd : config->inherited_fds) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001489 if (minijail_preserve_fd(c->jail.get(), fd, fd) != 0)
1490 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001491 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001492
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001493 /* TODO(dgreid) - remove this once shared mounts are cleaned up. */
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001494 minijail_skip_remount_private(c->jail.get());
Dylan Reid3da683b2016-04-05 03:35:35 -07001495
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001496 if (!config->keep_fds_open)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001497 minijail_close_open_fds(c->jail.get());
Luis Hector Chaveze18e7d42016-10-12 07:35:32 -07001498
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001499 if (config->use_capmask) {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001500 minijail_use_caps(c->jail.get(), config->capmask);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001501 if (config->use_capmask_ambient)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001502 minijail_set_ambient_caps(c->jail.get());
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001503 if (config->securebits_skip_mask) {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001504 minijail_skip_setting_securebits(c->jail.get(),
1505 config->securebits_skip_mask);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001506 }
1507 }
Luis Hector Chavezff5978f2017-06-27 12:52:58 -07001508
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001509 if (!config->do_init)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001510 minijail_run_as_init(c->jail.get());
Luis Hector Chavezdac65c32017-07-21 10:30:23 -07001511
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001512 std::vector<char*> argv_cstr;
1513 argv_cstr.reserve(config->program_argv.size() + 1);
1514 for (const auto& arg : config->program_argv)
1515 argv_cstr.emplace_back(const_cast<char*>(arg.c_str()));
1516 argv_cstr.emplace_back(nullptr);
1517
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001518 if (minijail_run_pid_pipes_no_preload(c->jail.get(), argv_cstr[0],
1519 argv_cstr.data(), &c->init_pid, nullptr,
1520 nullptr, nullptr) != 0) {
1521 return -1;
1522 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001523
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001524 // |hook_states| is already sorted in the correct order.
1525 for (auto& hook_state : c->hook_states) {
1526 if (!hook_state.first.WaitForHookAndRun(hook_state.second, c->init_pid))
1527 return -1;
1528 }
1529
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001530 // The container has started successfully, no need to tear it down anymore.
1531 ignore_result(teardown.Release());
1532 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -08001533}
1534
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001535const char* container_root(struct container* c) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001536 return c->runfs.value().c_str();
Dylan Reid837c74a2016-01-22 17:25:21 -08001537}
1538
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001539int container_pid(struct container* c) {
1540 return c->init_pid;
Dylan Reid837c74a2016-01-22 17:25:21 -08001541}
1542
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001543int container_wait(struct container* c) {
1544 int rc;
Dylan Reidcf745c52016-04-22 10:18:03 -07001545
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001546 do {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001547 rc = minijail_wait(c->jail.get());
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001548 } while (rc == -EINTR);
Dylan Reidcf745c52016-04-22 10:18:03 -07001549
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001550 // If the process had already been reaped, still perform teardown.
1551 if (rc == -ECHILD || rc >= 0) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001552 if (!ContainerTeardown(c))
1553 rc = -errno;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001554 }
1555 return rc;
Dylan Reid837c74a2016-01-22 17:25:21 -08001556}
1557
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001558int container_kill(struct container* c) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001559 if (kill(c->init_pid, SIGKILL) != 0 && errno != ESRCH) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -07001560 PLOG(ERROR) << "Failed to kill " << c->init_pid;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001561 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -07001562 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001563 return container_wait(c);
Dylan Reid837c74a2016-01-22 17:25:21 -08001564}
yusukesbbc37a72017-11-21 09:51:54 -08001565
yusukes32622542018-01-05 18:59:52 -08001566char* container_config_dump(struct container_config* c, int sort_vectors) {
yusukesbbc37a72017-11-21 09:51:54 -08001567 std::stringstream out;
yusukes32622542018-01-05 18:59:52 -08001568 DumpConfig(&out, c, sort_vectors);
yusukesbbc37a72017-11-21 09:51:54 -08001569 return strdup(out.str().c_str());
1570}