blob: 4ae9073aa63eae553fbcf2536ae56cacd1d4fefd [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;
54using libcontainer::LoopdevDetach;
55using libcontainer::LoopdevSetup;
56using libcontainer::MakeDir;
57using libcontainer::MountExternal;
58using libcontainer::TouchFile;
Mike Frysinger412dbd22017-01-06 01:50:34 -050059
Luis Hector Chavez81efb332017-09-18 14:01:29 -070060constexpr size_t kMaxNumSetfilesArgs = 128;
61constexpr 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;
124 uint32_t cur;
125 uint32_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
162 // Syscall table to use or nullptr if none.
163 std::string alt_syscall_table;
164
165 // Filesystems to mount in the new namespace.
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700166 std::vector<Mount> mounts;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700167
Stephen Barber771653f2017-10-04 23:48:57 -0700168 // Namespaces that should be used for the container.
169 std::set<std::string> namespaces;
170
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700171 // Device nodes to create.
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700172 std::vector<Device> devices;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700173
174 // Device node cgroup permissions.
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700175 std::vector<CgroupDevice> cgroup_devices;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700176
177 // Should run setfiles on mounts to enable selinux.
178 std::string run_setfiles;
179
180 // 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;
241 std::vector<base::FilePath> loopdev_paths;
242 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
yusukesbbc37a72017-11-21 09:51:54 -0800252std::ostream& operator<<(std::ostream& stream, const Mount& mount) {
253 stream << "mount:" << std::endl
254 << " name: " << QUOTE(mount.name) << std::endl
255 << " source: " << QUOTE(mount.source.value()) << std::endl
256 << " destination: " << QUOTE(mount.destination.value()) << std::endl
257 << " type: " << QUOTE(mount.type) << std::endl
258 << " data: " << QUOTE(mount.data) << std::endl
259 << " verity: " << QUOTE(mount.verity) << std::endl
260 << " flags: 0x" << std::hex << mount.flags << std::dec << std::endl
261 << " uid: " << mount.uid << std::endl
262 << " gid: " << mount.gid << std::endl
263 << " mode: 0" << std::oct << mount.mode << std::dec << std::endl
264 << " mount_in_ns: " << mount.mount_in_ns << std::endl
265 << " create: " << mount.create << std::endl
266 << " loopback: " << mount.loopback << std::endl;
267
268 return stream;
269}
270
271std::ostream& operator<<(std::ostream& stream, const Device& device) {
272 stream << "device:" << std::endl
273 << " type: " << device.type << std::endl
274 << " path: " << QUOTE(device.path.value()) << std::endl
275 << " fs_permissions: 0" << std::oct << device.fs_permissions
276 << std::dec << std::endl
277 << " major: " << device.major << std::endl
278 << " minor: " << device.minor << std::endl
279 << " copy_minor: " << device.copy_minor << std::endl
280 << " uid: " << device.uid << std::endl
281 << " gid: " << device.gid << std::endl;
282
283 return stream;
284}
285
286std::ostream& operator<<(std::ostream& stream,
287 const CgroupDevice& cgroup_device) {
288 stream << "cgroup_device:" << std::endl
289 << " allow: " << cgroup_device.allow << std::endl
290 << " type: " << cgroup_device.type << std::endl
291 << " major: " << cgroup_device.major << std::endl
292 << " minor: " << cgroup_device.minor << std::endl
293 << " read: " << cgroup_device.read << std::endl
294 << " write: " << cgroup_device.write << std::endl
295 << " modify: " << cgroup_device.modify << std::endl;
296
297 return stream;
298}
299
300std::ostream& operator<<(std::ostream& stream, const CpuCgroup& cpu_cgroup) {
301 stream << "cpu_cgroup:" << std::endl
302 << " shares: " << cpu_cgroup.shares << std::endl
303 << " quota: " << cpu_cgroup.quota << std::endl
304 << " period: " << cpu_cgroup.period << std::endl
305 << " rt_runtime: " << cpu_cgroup.rt_runtime << std::endl
306 << " rt_period: " << cpu_cgroup.rt_period << std::endl;
307
308 return stream;
309}
310
311std::ostream& operator<<(std::ostream& stream, const Rlimit& rlimit) {
312 stream << "rlimit:" << std::endl
313 << " type: " << rlimit.type << std::endl
314 << " cur: " << rlimit.cur << std::endl
315 << " max: " << rlimit.max << std::endl;
316
317 return stream;
318}
319
yusukes32622542018-01-05 18:59:52 -0800320void DumpConfig(std::ostream* stream,
321 const container_config* c,
322 bool sort_vectors) {
323 *stream << "config_root: " << QUOTE(c->config_root.value()) << std::endl
324 << "rootfs: " << QUOTE(c->rootfs.value()) << std::endl
325 << "rootfs_mount_flags: 0x" << std::hex << c->rootfs_mount_flags
326 << std::dec << std::endl
327 << "premounted_runfs: " << QUOTE(c->premounted_runfs.value())
328 << std::endl
329 << "pid_file_path: " << QUOTE(c->pid_file_path.value()) << std::endl
330 << "program_argv: size=" << c->program_argv.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800331
332 for (const std::string& argv : c->program_argv)
yusukes32622542018-01-05 18:59:52 -0800333 *stream << " " << QUOTE(argv) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800334
yusukes32622542018-01-05 18:59:52 -0800335 *stream << "uid: " << c->uid << std::endl
336 << "uid_map: " << QUOTE(c->uid_map) << std::endl
337 << "gid: " << c->gid << std::endl
338 << "gid_map: " << QUOTE(c->gid_map) << std::endl
339 << "alt_syscall_table: " << QUOTE(c->alt_syscall_table) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800340
yusukes32622542018-01-05 18:59:52 -0800341 auto mount_sorted = c->mounts;
342 if (sort_vectors) {
343 std::stable_sort(mount_sorted.begin(), mount_sorted.end(),
344 [](const Mount& lhs, const Mount& rhs) {
345 return std::make_tuple(lhs.destination.value(),
346 lhs.source.value(), lhs.flags) <
347 std::make_tuple(rhs.destination.value(),
348 rhs.source.value(), rhs.flags);
349 });
350 }
351 for (const auto& mount : mount_sorted)
352 *stream << mount;
yusukesbbc37a72017-11-21 09:51:54 -0800353
yusukes32622542018-01-05 18:59:52 -0800354 *stream << "namespaces: size=" << c->namespaces.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800355 for (const std::string& ns : c->namespaces)
yusukes32622542018-01-05 18:59:52 -0800356 *stream << " " << QUOTE(ns) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800357
yusukes32622542018-01-05 18:59:52 -0800358 auto devices_sorted = c->devices;
359 if (sort_vectors) {
360 std::stable_sort(devices_sorted.begin(), devices_sorted.end(),
361 [](const Device& lhs, const Device& rhs) {
362 return lhs.path.value() < rhs.path.value();
363 });
364 }
365 for (const auto& device : devices_sorted)
366 *stream << device;
yusukesbbc37a72017-11-21 09:51:54 -0800367
yusukes32622542018-01-05 18:59:52 -0800368 auto cgroup_devices_sorted = c->cgroup_devices;
369 if (sort_vectors) {
370 std::stable_sort(cgroup_devices_sorted.begin(), cgroup_devices_sorted.end(),
371 [](const CgroupDevice& lhs, const CgroupDevice& rhs) {
372 return std::make_tuple(lhs.type, lhs.major, lhs.minor) <
373 std::make_tuple(rhs.type, rhs.major, rhs.minor);
374 });
375 }
376 for (const auto& cgroup_device : cgroup_devices_sorted)
377 *stream << cgroup_device;
yusukesbbc37a72017-11-21 09:51:54 -0800378
yusukes32622542018-01-05 18:59:52 -0800379 *stream << "run_setfiles: " << QUOTE(c->run_setfiles) << std::endl
380 << c->cpu_cgparams
381 << "cgroup_parent: " << QUOTE(c->cgroup_parent.value()) << std::endl
382 << "cgroup_owner: " << c->cgroup_owner << std::endl
383 << "cgroup_group: " << c->cgroup_group << std::endl
384 << "keep_fds_open: " << c->keep_fds_open << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800385
yusukes32622542018-01-05 18:59:52 -0800386 *stream << "num_rlimits: " << c->num_rlimits << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800387 for (size_t i = 0; i < c->num_rlimits; ++i)
yusukes32622542018-01-05 18:59:52 -0800388 *stream << c->rlimits[i];
yusukesbbc37a72017-11-21 09:51:54 -0800389
yusukes32622542018-01-05 18:59:52 -0800390 *stream << "use_capmask: " << c->use_capmask << std::endl
391 << "use_capmask_ambient: " << c->use_capmask_ambient << std::endl
392 << "capmask: 0x" << std::hex << c->capmask << std::dec << std::endl
393 << "securebits_skip_mask: 0x" << std::hex << c->securebits_skip_mask
394 << std::dec << std::endl
395 << "do_init: " << c->do_init << std::endl
396 << "selinux_context: " << QUOTE(c->selinux_context) << std::endl
397 << "pre_start_hook: " << reinterpret_cast<void*>(c->pre_start_hook)
398 << std::endl
399 << "pre_start_hook_payload: " << c->pre_start_hook_payload
400 << std::endl
401 << "inherited_fds: size=" << c->inherited_fds.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800402
403 for (int fd : c->inherited_fds)
yusukes32622542018-01-05 18:59:52 -0800404 *stream << " " << fd << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800405
yusukes32622542018-01-05 18:59:52 -0800406 *stream << "hooks: size=" << c->hooks.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800407}
408
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700409// Returns the path for |path_in_container| in the outer namespace.
410base::FilePath GetPathInOuterNamespace(
411 const base::FilePath& root, const base::FilePath& path_in_container) {
412 if (path_in_container.IsAbsolute())
413 return base::FilePath(root.value() + path_in_container.value());
414 return root.Append(path_in_container);
415}
416
417// Make sure the mount target exists in the new rootfs. Create if needed and
418// possible.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700419bool SetupMountDestination(const struct container_config* config,
420 const Mount& mount,
421 const base::FilePath& source,
422 const base::FilePath& dest) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700423 struct stat st_buf;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700424 if (stat(dest.value().c_str(), &st_buf) == 0) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700425 // destination exists.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700426 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700427 }
428
429 // Try to create the destination. Either make directory or touch a file
430 // depending on the source type.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700431 int uid_userns;
432 if (!GetUsernsOutsideId(config->uid_map, mount.uid, &uid_userns))
433 return false;
434 int gid_userns;
435 if (!GetUsernsOutsideId(config->gid_map, mount.gid, &gid_userns))
436 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700437
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700438 if (stat(source.value().c_str(), &st_buf) != 0 || S_ISDIR(st_buf.st_mode) ||
439 S_ISBLK(st_buf.st_mode)) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700440 return MakeDir(dest, uid_userns, gid_userns, mount.mode);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700441 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700442
443 return TouchFile(dest, uid_userns, gid_userns, mount.mode);
444}
445
446// Fork and exec the setfiles command to configure the selinux policy.
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700447bool RunSetfilesCommand(const struct container* c,
448 const struct container_config* config,
449 const std::vector<base::FilePath>& destinations,
450 pid_t container_pid) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700451 int pid = fork();
452 if (pid == 0) {
453 size_t arg_index = 0;
454 const char* argv[kMaxNumSetfilesArgs];
455 const char* env[] = {
456 nullptr,
457 };
458
459 base::FilePath context_path = c->runfsroot.Append("file_contexts");
460
461 argv[arg_index++] = config->run_setfiles.c_str();
462 argv[arg_index++] = "-r";
463 argv[arg_index++] = c->runfsroot.value().c_str();
464 argv[arg_index++] = context_path.value().c_str();
465 if (arg_index + destinations.size() >= kMaxNumSetfilesArgs)
466 _exit(-E2BIG);
467 for (const auto& destination : destinations)
468 argv[arg_index++] = destination.value().c_str();
469 argv[arg_index] = nullptr;
470
471 execve(
472 argv[0], const_cast<char* const*>(argv), const_cast<char* const*>(env));
473
474 /* Command failed to exec if execve returns. */
475 _exit(-errno);
476 }
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700477 if (pid < 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700478 PLOG(ERROR) << "Failed to fork to run setfiles";
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700479 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700480 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700481
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700482 int status;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700483 if (HANDLE_EINTR(waitpid(pid, &status, 0)) < 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700484 PLOG(ERROR) << "Failed to wait for setfiles";
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700485 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700486 }
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700487 if (!WIFEXITED(status)) {
488 LOG(ERROR) << "setfiles did not terminate cleanly";
489 return false;
490 }
491 if (WEXITSTATUS(status) != 0) {
492 LOG(ERROR) << "setfiles exited with non-zero status: "
493 << WEXITSTATUS(status);
494 return false;
495 }
496 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700497}
498
499// Unmounts anything we mounted in this mount namespace in the opposite order
500// that they were mounted.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700501bool UnmountExternalMounts(struct container* c) {
502 bool ret = true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700503
504 for (auto it = c->ext_mounts.rbegin(); it != c->ext_mounts.rend(); ++it) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700505 if (umount(it->value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700506 PLOG(ERROR) << "Failed to unmount " << it->value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700507 ret = false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700508 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700509 }
510 c->ext_mounts.clear();
511
512 for (auto it = c->loopdev_paths.rbegin(); it != c->loopdev_paths.rend();
513 ++it) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700514 if (!LoopdevDetach(*it))
515 ret = false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700516 }
517 c->loopdev_paths.clear();
518
519 for (auto it = c->device_mappers.rbegin(); it != c->device_mappers.rend();
520 ++it) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700521 if (!DeviceMapperDetach(*it))
522 ret = false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700523 }
524 c->device_mappers.clear();
525
526 return ret;
527}
528
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700529bool DoContainerMount(struct container* c,
530 const struct container_config* config,
531 const Mount& mount) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700532 base::FilePath dest =
533 GetPathInOuterNamespace(c->runfsroot, mount.destination);
534
535 // If it's a bind mount relative to rootfs, append source to
536 // rootfs path, otherwise source path is absolute.
537 base::FilePath source;
538 if ((mount.flags & MS_BIND) && !mount.source.IsAbsolute()) {
539 source = GetPathInOuterNamespace(c->runfsroot, mount.source);
540 } else if (mount.loopback && !mount.source.IsAbsolute() &&
541 !c->config_root.empty()) {
542 source = GetPathInOuterNamespace(c->config_root, mount.source);
543 } else {
544 source = mount.source;
545 }
546
547 // Only create the destinations for external mounts, minijail will take
548 // care of those mounted in the new namespace.
549 if (mount.create && !mount.mount_in_ns) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700550 if (!SetupMountDestination(config, mount, source, dest))
551 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700552 }
553 if (mount.loopback) {
554 // Record this loopback file for cleanup later.
555 base::FilePath loop_source = source;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700556 if (!LoopdevSetup(loop_source, &source))
557 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700558
559 // Save this to cleanup when shutting down.
560 c->loopdev_paths.push_back(source);
561 }
562 if (!mount.verity.empty()) {
563 // Set this device up via dm-verity.
564 std::string dm_name;
565 base::FilePath dm_source = source;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700566 if (!DeviceMapperSetup(dm_source, mount.verity, &source, &dm_name))
567 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700568
569 // Save this to cleanup when shutting down.
570 c->device_mappers.push_back(dm_name);
571 }
572 if (mount.mount_in_ns) {
573 // We can mount this with minijail.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700574 if (minijail_mount_with_data(
575 c->jail.get(), source.value().c_str(),
576 mount.destination.value().c_str(), mount.type.c_str(), mount.flags,
577 mount.data.empty() ? nullptr : mount.data.c_str()) != 0) {
578 return false;
579 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700580 } else {
581 // Mount this externally and unmount it on exit.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700582 if (!MountExternal(source.value(), dest.value(), mount.type, mount.flags,
583 mount.data)) {
584 return false;
585 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700586 // Save this to unmount when shutting down.
587 c->ext_mounts.push_back(dest);
588 }
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 DoContainerMounts(struct container* c,
594 const struct container_config* config) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700595 UnmountExternalMounts(c);
596
597 // This will run in all the error cases.
598 base::ScopedClosureRunner teardown(base::Bind(
599 base::IgnoreResult(&UnmountExternalMounts), base::Unretained(c)));
600
601 for (const auto& mount : config->mounts) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700602 if (!DoContainerMount(c, config, mount))
603 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700604 }
605
606 // The mounts have been done successfully, no need to tear them down anymore.
607 ignore_result(teardown.Release());
608
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700609 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700610}
611
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700612bool ContainerCreateDevice(const struct container* c,
613 const struct container_config* config,
614 const Device& dev,
Stephen Barber7bae6642017-11-30 10:47:12 -0800615 int major,
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700616 int minor) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700617 mode_t mode = dev.fs_permissions;
618 switch (dev.type) {
619 case 'b':
620 mode |= S_IFBLK;
621 break;
622 case 'c':
623 mode |= S_IFCHR;
624 break;
625 default:
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700626 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700627 }
628
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700629 int uid_userns;
630 if (!GetUsernsOutsideId(config->uid_map, dev.uid, &uid_userns))
631 return false;
632 int gid_userns;
633 if (!GetUsernsOutsideId(config->gid_map, dev.gid, &gid_userns))
634 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700635
636 base::FilePath path = GetPathInOuterNamespace(c->runfsroot, dev.path);
Luis Hector Chavez92278e82017-10-16 11:30:27 -0700637 if (!libcontainer::CreateDirectoryOwnedBy(path.DirName(), 0755, uid_userns,
638 gid_userns)) {
Luis Hector Chavez5d51abb2017-10-11 17:05:57 -0700639 PLOG(ERROR) << "Failed to create parent directory for " << path.value();
640 return false;
641 }
Stephen Barber7bae6642017-11-30 10:47:12 -0800642 if (mknod(path.value().c_str(), mode, makedev(major, minor)) != 0 &&
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700643 errno != EEXIST) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700644 PLOG(ERROR) << "Failed to mknod " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700645 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700646 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700647 if (chown(path.value().c_str(), uid_userns, gid_userns) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700648 PLOG(ERROR) << "Failed to chown " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700649 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700650 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700651 if (chmod(path.value().c_str(), dev.fs_permissions) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700652 PLOG(ERROR) << "Failed to chmod " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700653 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700654 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700655
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700656 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700657}
658
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700659bool MountRunfs(struct container* c, const struct container_config* config) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700660 {
661 std::string runfs_template = base::StringPrintf(
662 "%s/%s_XXXXXX", c->rundir.value().c_str(), c->name.c_str());
663 // TODO(lhchavez): Replace this with base::CreateTemporaryDirInDir().
664 char* runfs_path = mkdtemp(const_cast<char*>(runfs_template.c_str()));
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700665 if (!runfs_path) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700666 PLOG(ERROR) << "Failed to mkdtemp in " << c->rundir.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 c->runfs = base::FilePath(runfs_path);
670 }
671
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700672 int uid_userns;
673 if (!GetUsernsOutsideId(config->uid_map, config->uid, &uid_userns))
674 return false;
675 int gid_userns;
676 if (!GetUsernsOutsideId(config->gid_map, config->gid, &gid_userns))
677 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700678
679 // Make sure the container uid can access the rootfs.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700680 if (chmod(c->runfs.value().c_str(), 0700) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700681 PLOG(ERROR) << "Failed to chmod " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700682 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700683 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700684 if (chown(c->runfs.value().c_str(), uid_userns, gid_userns) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700685 PLOG(ERROR) << "Failed to chown " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700686 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700687 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700688
689 c->runfsroot = c->runfs.Append("root");
690
691 constexpr mode_t kRootDirMode = 0660;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700692 if (mkdir(c->runfsroot.value().c_str(), kRootDirMode) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700693 PLOG(ERROR) << "Failed to mkdir " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700694 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700695 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700696 if (chmod(c->runfsroot.value().c_str(), kRootDirMode) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700697 PLOG(ERROR) << "Failed to chmod " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700698 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700699 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700700
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700701 if (mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "",
702 MS_BIND | (config->rootfs_mount_flags & MS_REC), nullptr) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700703 PLOG(ERROR) << "Failed to bind-mount " << config->rootfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700704 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700705 }
706
707 // MS_BIND ignores any flags passed to it (except MS_REC). We need a
708 // second call to mount() to actually set them.
709 if (config->rootfs_mount_flags &&
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700710 mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "",
711 (config->rootfs_mount_flags & ~MS_REC), nullptr) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700712 PLOG(ERROR) << "Failed to remount " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700713 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700714 }
715
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700716 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700717}
718
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700719bool CreateDeviceNodes(struct container* c,
720 const struct container_config* config,
721 pid_t container_pid) {
722 for (const auto& dev : config->devices) {
Stephen Barber7bae6642017-11-30 10:47:12 -0800723 int major = dev.major;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700724 int minor = dev.minor;
725
Stephen Barber7bae6642017-11-30 10:47:12 -0800726 if (dev.copy_major || dev.copy_minor) {
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700727 struct stat st_buff;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700728 if (stat(dev.path.value().c_str(), &st_buff) != 0)
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700729 continue;
Stephen Barber7bae6642017-11-30 10:47:12 -0800730
731 if (dev.copy_major)
732 major = major(st_buff.st_rdev);
733 if (dev.copy_minor)
734 minor = minor(st_buff.st_rdev);
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700735 }
Stephen Barber7bae6642017-11-30 10:47:12 -0800736 if (major < 0 || minor < 0)
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700737 continue;
Stephen Barber7bae6642017-11-30 10:47:12 -0800738 if (!ContainerCreateDevice(c, config, dev, major, minor))
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700739 return false;
740 }
741
742 return true;
743}
744
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700745bool DeviceSetup(struct container* c, const struct container_config* config) {
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -0700746 c->cgroup->DenyAllDevices();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700747
748 for (const auto& dev : config->cgroup_devices) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700749 if (!c->cgroup->AddDevice(dev.allow, dev.major, dev.minor, dev.read,
750 dev.write, dev.modify, dev.type)) {
751 return false;
752 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700753 }
754
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700755 for (const auto& loopdev_path : c->loopdev_paths) {
756 struct stat st;
757
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700758 if (stat(loopdev_path.value().c_str(), &st) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700759 PLOG(ERROR) << "Failed to stat " << loopdev_path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700760 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700761 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700762 if (!c->cgroup->AddDevice(1, major(st.st_rdev), minor(st.st_rdev), 1, 0, 0,
763 'b')) {
764 return false;
765 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700766 }
767
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700768 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700769}
770
771int Setexeccon(void* payload) {
772 char* init_domain = reinterpret_cast<char*>(payload);
773 pid_t tid = syscall(SYS_gettid);
774
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700775 if (tid < 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700776 PLOG(ERROR) << "Failed to gettid";
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700777 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700778 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700779
780 std::string exec_path =
781 base::StringPrintf("/proc/self/task/%d/attr/exec", tid);
782
783 base::ScopedFD fd(open(exec_path.c_str(), O_WRONLY | O_CLOEXEC));
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700784 if (!fd.is_valid()) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700785 PLOG(ERROR) << "Failed to open " << exec_path;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700786 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700787 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700788
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700789 if (!base::WriteFileDescriptor(fd.get(), init_domain, strlen(init_domain))) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700790 PLOG(ERROR) << "Failed to write the SELinux label to " << exec_path;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700791 return -errno;
792 }
793
794 return 0;
795}
796
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700797bool ContainerTeardown(struct container* c) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700798 UnmountExternalMounts(c);
799 if (!c->runfsroot.empty() && !c->runfs.empty()) {
800 /* |c->runfsroot| may have been mounted recursively. Thus use
801 * MNT_DETACH to "immediately disconnect the filesystem and all
802 * filesystems mounted below it from each other and from the
803 * mount table". Otherwise one would need to unmount every
804 * single dependent mount before unmounting |c->runfsroot|
805 * itself.
806 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700807 if (umount2(c->runfsroot.value().c_str(), MNT_DETACH) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700808 PLOG(ERROR) << "Failed to detach " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700809 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700810 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700811 if (rmdir(c->runfsroot.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700812 PLOG(ERROR) << "Failed to rmdir " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700813 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700814 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700815 c->runfsroot = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700816 }
817 if (!c->pid_file_path.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700818 if (unlink(c->pid_file_path.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700819 PLOG(ERROR) << "Failed to unlink " << c->pid_file_path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700820 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700821 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700822 c->pid_file_path = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700823 }
824 if (!c->runfs.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700825 if (rmdir(c->runfs.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700826 PLOG(ERROR) << "Failed to rmdir " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700827 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700828 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700829 c->runfs = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700830 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700831 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700832}
833
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700834void CancelContainerStart(struct container* c) {
835 if (c->init_pid != -1)
836 container_kill(c);
837 ContainerTeardown(c);
838}
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700839
840} // namespace
841
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700842struct container_config* container_config_create() {
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700843 return new (std::nothrow) struct container_config();
Dylan Reid837c74a2016-01-22 17:25:21 -0800844}
845
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700846void container_config_destroy(struct container_config* c) {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700847 if (c == nullptr)
848 return;
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700849 delete c;
Dylan Reid837c74a2016-01-22 17:25:21 -0800850}
851
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700852int container_config_config_root(struct container_config* c,
853 const char* config_root) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700854 c->config_root = base::FilePath(config_root);
855 return 0;
Mike Frysingerb22acdf2017-01-08 02:02:35 -0500856}
857
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700858const char* container_config_get_config_root(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700859 return c->config_root.value().c_str();
Mike Frysingerb22acdf2017-01-08 02:02:35 -0500860}
861
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700862int container_config_rootfs(struct container_config* c, const char* rootfs) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700863 c->rootfs = base::FilePath(rootfs);
864 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800865}
866
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700867const char* container_config_get_rootfs(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700868 return c->rootfs.value().c_str();
Dylan Reid11456722016-05-02 11:24:50 -0700869}
870
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700871void container_config_rootfs_mount_flags(struct container_config* c,
872 unsigned long rootfs_mount_flags) {
873 /* Since we are going to add MS_REMOUNT anyways, add it here so we can
874 * simply check against zero later. MS_BIND is also added to avoid
875 * re-mounting the original filesystem, since the rootfs is always
876 * bind-mounted.
877 */
878 c->rootfs_mount_flags = MS_REMOUNT | MS_BIND | rootfs_mount_flags;
Luis Hector Chavezc240e7e2016-09-22 10:33:03 -0700879}
880
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700881unsigned long container_config_get_rootfs_mount_flags(
882 const struct container_config* c) {
883 return c->rootfs_mount_flags;
Luis Hector Chavezc240e7e2016-09-22 10:33:03 -0700884}
885
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700886int container_config_premounted_runfs(struct container_config* c,
887 const char* runfs) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700888 c->premounted_runfs = base::FilePath(runfs);
889 return 0;
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700890}
891
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700892const char* container_config_get_premounted_runfs(
893 const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700894 return c->premounted_runfs.value().c_str();
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700895}
896
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700897int container_config_pid_file(struct container_config* c, const char* path) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700898 c->pid_file_path = base::FilePath(path);
899 return 0;
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700900}
901
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700902const char* container_config_get_pid_file(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700903 return c->pid_file_path.value().c_str();
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700904}
905
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700906int container_config_program_argv(struct container_config* c,
907 const char** argv,
908 size_t num_args) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700909 if (num_args < 1) {
910 errno = EINVAL;
911 return -1;
912 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700913 c->program_argv.clear();
914 c->program_argv.reserve(num_args);
915 for (size_t i = 0; i < num_args; ++i)
916 c->program_argv.emplace_back(argv[i]);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700917 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800918}
919
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700920size_t container_config_get_num_program_args(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700921 return c->program_argv.size();
Dylan Reid11456722016-05-02 11:24:50 -0700922}
923
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700924const char* container_config_get_program_arg(const struct container_config* c,
925 size_t index) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700926 if (index >= c->program_argv.size())
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700927 return nullptr;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700928 return c->program_argv[index].c_str();
Dylan Reid11456722016-05-02 11:24:50 -0700929}
930
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700931void container_config_uid(struct container_config* c, uid_t uid) {
932 c->uid = uid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700933}
934
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700935uid_t container_config_get_uid(const struct container_config* c) {
936 return c->uid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700937}
938
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700939int container_config_uid_map(struct container_config* c, const char* uid_map) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700940 c->uid_map = uid_map;
941 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800942}
943
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700944void container_config_gid(struct container_config* c, gid_t gid) {
945 c->gid = gid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700946}
947
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700948gid_t container_config_get_gid(const struct container_config* c) {
949 return c->gid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700950}
951
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700952int container_config_gid_map(struct container_config* c, const char* gid_map) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700953 c->gid_map = gid_map;
954 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800955}
956
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700957int container_config_alt_syscall_table(struct container_config* c,
958 const char* alt_syscall_table) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700959 c->alt_syscall_table = alt_syscall_table;
960 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800961}
962
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700963int container_config_add_rlimit(struct container_config* c,
964 int type,
965 uint32_t cur,
966 uint32_t max) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700967 if (c->num_rlimits >= kMaxRlimits) {
968 errno = ENOMEM;
969 return -1;
970 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700971 c->rlimits[c->num_rlimits].type = type;
972 c->rlimits[c->num_rlimits].cur = cur;
973 c->rlimits[c->num_rlimits].max = max;
974 c->num_rlimits++;
975 return 0;
Dylan Reid93fa4602017-06-06 13:39:31 -0700976}
977
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700978int container_config_add_mount(struct container_config* c,
979 const char* name,
980 const char* source,
981 const char* destination,
982 const char* type,
983 const char* data,
984 const char* verity,
985 int flags,
986 int uid,
987 int gid,
988 int mode,
989 int mount_in_ns,
990 int create,
991 int loopback) {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700992 if (name == nullptr || source == nullptr || destination == nullptr ||
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700993 type == nullptr) {
994 errno = EINVAL;
995 return -1;
996 }
Dylan Reid837c74a2016-01-22 17:25:21 -0800997
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700998 c->mounts.emplace_back(Mount{name,
999 base::FilePath(source),
1000 base::FilePath(destination),
1001 type,
1002 data ? data : "",
1003 verity ? verity : "",
1004 flags,
1005 uid,
1006 gid,
1007 mode,
1008 mount_in_ns != 0,
1009 create != 0,
1010 loopback != 0});
Luis Hector Chavez479b95f2016-06-06 08:01:05 -07001011
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001012 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -08001013}
1014
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001015int container_config_add_cgroup_device(struct container_config* c,
1016 int allow,
1017 char type,
1018 int major,
1019 int minor,
1020 int read,
1021 int write,
1022 int modify) {
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001023 c->cgroup_devices.emplace_back(CgroupDevice{
1024 allow != 0, type, major, minor, read != 0, write != 0, modify != 0});
Dylan Reid4843d6b2017-03-31 18:14:30 -07001025
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001026 return 0;
Dylan Reid4843d6b2017-03-31 18:14:30 -07001027}
1028
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001029int container_config_add_device(struct container_config* c,
1030 char type,
1031 const char* path,
1032 int fs_permissions,
1033 int major,
1034 int minor,
Stephen Barber7bae6642017-11-30 10:47:12 -08001035 int copy_major,
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001036 int copy_minor,
1037 int uid,
1038 int gid,
1039 int read_allowed,
1040 int write_allowed,
1041 int modify_allowed) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001042 if (path == nullptr) {
1043 errno = EINVAL;
1044 return -1;
1045 }
Stephen Barber7bae6642017-11-30 10:47:12 -08001046 /* If using a dynamic major/minor number, ensure that major/minor is -1. */
1047 if ((copy_major && (major != -1)) || (copy_minor && (minor != -1))) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001048 errno = EINVAL;
1049 return -1;
1050 }
Dylan Reid355d5e42016-04-29 16:53:31 -07001051
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001052 if (read_allowed || write_allowed || modify_allowed) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001053 if (container_config_add_cgroup_device(c, 1, type, major, minor,
1054 read_allowed, write_allowed,
1055 modify_allowed) != 0) {
1056 errno = ENOMEM;
1057 return -1;
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001058 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001059 }
Luis Hector Chavez479b95f2016-06-06 08:01:05 -07001060
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001061 c->devices.emplace_back(Device{
Stephen Barber7bae6642017-11-30 10:47:12 -08001062 type, base::FilePath(path), fs_permissions, major, minor, copy_major != 0,
1063 copy_minor != 0, uid, gid,
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001064 });
1065
1066 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -08001067}
1068
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001069int container_config_run_setfiles(struct container_config* c,
1070 const char* setfiles_cmd) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001071 c->run_setfiles = setfiles_cmd;
1072 return 0;
Dylan Reid2bd9ea92016-04-07 20:57:47 -07001073}
Dylan Reid837c74a2016-01-22 17:25:21 -08001074
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001075const char* container_config_get_run_setfiles(
1076 const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001077 return c->run_setfiles.c_str();
Dylan Reid11456722016-05-02 11:24:50 -07001078}
1079
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001080int container_config_set_cpu_shares(struct container_config* c, int shares) {
1081 /* CPU shares must be 2 or higher. */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001082 if (shares < 2) {
1083 errno = EINVAL;
1084 return -1;
1085 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001086
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001087 c->cpu_cgparams.shares = shares;
1088 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001089}
1090
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001091int container_config_set_cpu_cfs_params(struct container_config* c,
1092 int quota,
1093 int period) {
1094 /*
1095 * quota could be set higher than period to utilize more than one CPU.
1096 * quota could also be set as -1 to indicate the cgroup does not adhere
1097 * to any CPU time restrictions.
1098 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001099 if (quota <= 0 && quota != -1) {
1100 errno = EINVAL;
1101 return -1;
1102 }
1103 if (period <= 0) {
1104 errno = EINVAL;
1105 return -1;
1106 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001107
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001108 c->cpu_cgparams.quota = quota;
1109 c->cpu_cgparams.period = period;
1110 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001111}
1112
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001113int container_config_set_cpu_rt_params(struct container_config* c,
1114 int rt_runtime,
1115 int rt_period) {
1116 /*
1117 * rt_runtime could be set as 0 to prevent the cgroup from using
1118 * realtime CPU.
1119 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001120 if (rt_runtime < 0 || rt_runtime >= rt_period) {
1121 errno = EINVAL;
1122 return -1;
1123 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001124
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001125 c->cpu_cgparams.rt_runtime = rt_runtime;
1126 c->cpu_cgparams.rt_period = rt_period;
1127 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001128}
1129
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001130int container_config_get_cpu_shares(struct container_config* c) {
1131 return c->cpu_cgparams.shares;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001132}
1133
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001134int container_config_get_cpu_quota(struct container_config* c) {
1135 return c->cpu_cgparams.quota;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001136}
1137
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001138int container_config_get_cpu_period(struct container_config* c) {
1139 return c->cpu_cgparams.period;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001140}
1141
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001142int container_config_get_cpu_rt_runtime(struct container_config* c) {
1143 return c->cpu_cgparams.rt_runtime;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001144}
1145
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001146int container_config_get_cpu_rt_period(struct container_config* c) {
1147 return c->cpu_cgparams.rt_period;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001148}
1149
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001150int container_config_set_cgroup_parent(struct container_config* c,
1151 const char* parent,
1152 uid_t cgroup_owner,
1153 gid_t cgroup_group) {
1154 c->cgroup_owner = cgroup_owner;
1155 c->cgroup_group = cgroup_group;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001156 c->cgroup_parent = base::FilePath(parent);
1157 return 0;
Dylan Reid9e724af2016-07-21 09:58:07 -07001158}
1159
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001160const char* container_config_get_cgroup_parent(struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001161 return c->cgroup_parent.value().c_str();
Dylan Reid9e724af2016-07-21 09:58:07 -07001162}
1163
Stephen Barber771653f2017-10-04 23:48:57 -07001164int container_config_namespaces(struct container_config* c,
1165 const char** namespaces,
1166 size_t num_ns) {
1167 if (num_ns < 1)
1168 return -EINVAL;
1169 c->namespaces.clear();
1170 for (size_t i = 0; i < num_ns; ++i)
1171 c->namespaces.emplace(namespaces[i]);
1172 return 0;
Keshav Santhanam1b6bf672016-08-10 18:35:12 -07001173}
1174
Stephen Barber771653f2017-10-04 23:48:57 -07001175size_t container_config_get_num_namespaces(const struct container_config* c) {
1176 return c->namespaces.size();
1177}
1178
1179bool container_config_has_namespace(const struct container_config* c,
1180 const char* ns) {
1181 return c->namespaces.find(ns) != c->namespaces.end();
Keshav Santhanam1b6bf672016-08-10 18:35:12 -07001182}
1183
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001184void container_config_keep_fds_open(struct container_config* c) {
yusukesf125f332017-12-08 13:45:15 -08001185 c->keep_fds_open = true;
Dylan Reidc4335842016-11-11 10:24:52 -08001186}
1187
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001188void container_config_set_capmask(struct container_config* c,
1189 uint64_t capmask,
1190 int ambient) {
yusukesf125f332017-12-08 13:45:15 -08001191 c->use_capmask = true;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001192 c->capmask = capmask;
1193 c->use_capmask_ambient = ambient;
Luis Hector Chavezff5978f2017-06-27 12:52:58 -07001194}
1195
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001196void container_config_set_securebits_skip_mask(struct container_config* c,
1197 uint64_t securebits_skip_mask) {
1198 c->securebits_skip_mask = securebits_skip_mask;
Luis Hector Chavezcd44ba72017-06-30 13:01:38 -07001199}
1200
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001201void container_config_set_run_as_init(struct container_config* c,
1202 int run_as_init) {
1203 c->do_init = !run_as_init;
Luis Hector Chavezdac65c32017-07-21 10:30:23 -07001204}
1205
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001206int container_config_set_selinux_context(struct container_config* c,
1207 const char* context) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001208 if (!context) {
1209 errno = EINVAL;
1210 return -1;
1211 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001212 c->selinux_context = context;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001213 return 0;
Luis Hector Chavez15e8e672017-07-20 15:13:27 -07001214}
1215
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001216void container_config_set_pre_execve_hook(struct container_config* c,
1217 int (*hook)(void*),
1218 void* payload) {
1219 c->pre_start_hook = hook;
1220 c->pre_start_hook_payload = payload;
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001221}
1222
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001223void container_config_add_hook(struct container_config* c,
1224 minijail_hook_event_t event,
1225 libcontainer::HookCallback callback) {
1226 auto it = c->hooks.insert(
1227 std::make_pair(event, std::vector<libcontainer::HookCallback>()));
1228 it.first->second.emplace_back(std::move(callback));
1229}
1230
Luis Hector Chaveze03926a2017-09-28 17:28:49 -07001231int container_config_add_hook(struct container_config* c,
1232 minijail_hook_event_t event,
1233 const char* filename,
1234 const char** argv,
1235 size_t num_args,
1236 int* pstdin_fd,
1237 int* pstdout_fd,
1238 int* pstderr_fd) {
1239 std::vector<std::string> args;
1240 args.reserve(num_args);
1241 for (size_t i = 0; i < num_args; ++i)
1242 args.emplace_back(argv[i]);
1243
1244 // First element of the array belongs to the parent and the second one belongs
1245 // to the child.
1246 base::ScopedFD stdin_fds[2], stdout_fds[2], stderr_fds[2];
1247 if (pstdin_fd) {
1248 if (!libcontainer::Pipe2(&stdin_fds[1], &stdin_fds[0], 0))
1249 return -1;
1250 }
1251 if (pstdout_fd) {
1252 if (!libcontainer::Pipe2(&stdout_fds[0], &stdout_fds[0], 0))
1253 return -1;
1254 }
1255 if (pstderr_fd) {
1256 if (!libcontainer::Pipe2(&stderr_fds[0], &stderr_fds[0], 0))
1257 return -1;
1258 }
1259
1260 // After this point the call has been successful, so we can now commit to
1261 // whatever pipes we have opened.
1262 if (pstdin_fd) {
1263 *pstdin_fd = stdin_fds[0].release();
1264 c->inherited_fds.emplace_back(stdin_fds[1].get());
1265 }
1266 if (pstdout_fd) {
1267 *pstdout_fd = stdout_fds[0].release();
1268 c->inherited_fds.emplace_back(stdout_fds[1].get());
1269 }
1270 if (pstderr_fd) {
1271 *pstderr_fd = stderr_fds[0].release();
1272 c->inherited_fds.emplace_back(stderr_fds[1].get());
1273 }
1274 container_config_add_hook(
1275 c, event,
1276 libcontainer::CreateExecveCallback(
1277 base::FilePath(filename), args, std::move(stdin_fds[1]),
1278 std::move(stdout_fds[1]), std::move(stderr_fds[1])));
1279 return 0;
1280}
1281
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001282int container_config_inherit_fds(struct container_config* c,
1283 int* inherited_fds,
1284 size_t inherited_fd_count) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001285 if (!c->inherited_fds.empty()) {
1286 errno = EINVAL;
1287 return -1;
1288 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001289 for (size_t i = 0; i < inherited_fd_count; ++i)
1290 c->inherited_fds.emplace_back(inherited_fds[i]);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001291 return 0;
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001292}
1293
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001294struct container* container_new(const char* name, const char* rundir) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001295 struct container* c = new (std::nothrow) container();
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001296 if (!c)
1297 return nullptr;
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001298 c->rundir = base::FilePath(rundir);
1299 c->name = name;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001300 return c;
Dylan Reid837c74a2016-01-22 17:25:21 -08001301}
1302
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001303void container_destroy(struct container* c) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001304 delete c;
Dylan Reid837c74a2016-01-22 17:25:21 -08001305}
1306
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001307int container_start(struct container* c,
1308 const struct container_config* config) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001309 if (!c) {
1310 errno = EINVAL;
1311 return -1;
1312 }
1313 if (!config) {
1314 errno = EINVAL;
1315 return -1;
1316 }
1317 if (config->program_argv.empty()) {
1318 errno = EINVAL;
1319 return -1;
1320 }
Dylan Reide040c6b2016-05-02 18:49:02 -07001321
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001322 // This will run in all the error cases.
1323 base::ScopedClosureRunner teardown(
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -07001324 base::Bind(&CancelContainerStart, base::Unretained(c)));
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001325
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001326 if (!config->config_root.empty())
1327 c->config_root = config->config_root;
1328 if (!config->premounted_runfs.empty()) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001329 c->runfs.clear();
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001330 c->runfsroot = config->premounted_runfs;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001331 } else {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001332 if (!MountRunfs(c, config))
1333 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001334 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001335
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001336 c->jail.reset(minijail_new());
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001337 if (!c->jail) {
1338 errno = ENOMEM;
1339 return -1;
1340 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001341
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001342 if (!DoContainerMounts(c, config))
1343 return -1;
Dylan Reid837c74a2016-01-22 17:25:21 -08001344
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001345 int cgroup_uid;
1346 if (!GetUsernsOutsideId(config->uid_map, config->cgroup_owner, &cgroup_uid))
1347 return -1;
1348 int cgroup_gid;
1349 if (!GetUsernsOutsideId(config->gid_map, config->cgroup_group, &cgroup_gid))
1350 return -1;
Stephen Barber1a398c72017-01-23 12:39:44 -08001351
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -07001352 c->cgroup = libcontainer::Cgroup::Create(c->name,
1353 base::FilePath("/sys/fs/cgroup"),
1354 config->cgroup_parent,
1355 cgroup_uid,
1356 cgroup_gid);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001357 if (!c->cgroup)
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001358 return -1;
Dylan Reida9966422016-07-21 10:11:34 -07001359
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001360 // Must be root to modify device cgroup or mknod.
1361 std::map<minijail_hook_event_t, std::vector<libcontainer::HookCallback>>
1362 hook_callbacks;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001363 if (getuid() == 0) {
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001364 if (!config->devices.empty()) {
1365 // Create the devices in the mount namespace.
1366 auto it = hook_callbacks.insert(
1367 std::make_pair(MINIJAIL_HOOK_EVENT_PRE_CHROOT,
1368 std::vector<libcontainer::HookCallback>()));
1369 it.first->second.emplace_back(
1370 libcontainer::AdaptCallbackToRunInNamespaces(
1371 base::Bind(&CreateDeviceNodes, base::Unretained(c),
1372 base::Unretained(config)),
1373 {CLONE_NEWNS}));
1374 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001375 if (!DeviceSetup(c, config))
1376 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001377 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001378
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001379 // Potentially run setfiles on mounts configured outside of the jail.
1380 if (!config->run_setfiles.empty()) {
1381 const base::FilePath kDataPath("/data");
1382 const base::FilePath kCachePath("/cache");
1383 std::vector<base::FilePath> destinations;
1384 for (const auto& mnt : config->mounts) {
1385 if (mnt.mount_in_ns)
1386 continue;
1387 if (mnt.flags & MS_RDONLY)
1388 continue;
Yusuke Sato91f11f02016-12-02 16:15:13 -08001389
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001390 // A hack to avoid setfiles on /data and /cache.
1391 if (mnt.destination == kDataPath || mnt.destination == kCachePath)
1392 continue;
Yusuke Sato91f11f02016-12-02 16:15:13 -08001393
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001394 destinations.emplace_back(
1395 GetPathInOuterNamespace(c->runfsroot, mnt.destination));
1396 }
1397
1398 if (!destinations.empty()) {
1399 auto it = hook_callbacks.insert(
1400 std::make_pair(MINIJAIL_HOOK_EVENT_PRE_CHROOT,
1401 std::vector<libcontainer::HookCallback>()));
1402 it.first->second.emplace_back(
1403 libcontainer::AdaptCallbackToRunInNamespaces(
1404 base::Bind(&RunSetfilesCommand, base::Unretained(c),
1405 base::Unretained(config), destinations),
1406 {CLONE_NEWNS}));
1407 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001408 }
Dylan Reidd7229582016-04-27 17:08:40 -07001409
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001410 /* Setup CPU cgroup params. */
1411 if (config->cpu_cgparams.shares) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001412 if (!c->cgroup->SetCpuShares(config->cpu_cgparams.shares))
1413 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001414 }
1415 if (config->cpu_cgparams.period) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001416 if (!c->cgroup->SetCpuQuota(config->cpu_cgparams.quota))
1417 return -1;
1418 if (!c->cgroup->SetCpuPeriod(config->cpu_cgparams.period))
1419 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001420 }
1421 if (config->cpu_cgparams.rt_period) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001422 if (!c->cgroup->SetCpuRtRuntime(config->cpu_cgparams.rt_runtime))
1423 return -1;
1424 if (!c->cgroup->SetCpuRtPeriod(config->cpu_cgparams.rt_period))
1425 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001426 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001427
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001428 /* Setup and start the container with libminijail. */
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001429 if (!config->pid_file_path.empty())
1430 c->pid_file_path = config->pid_file_path;
1431 else if (!c->runfs.empty())
1432 c->pid_file_path = c->runfs.Append("container.pid");
Keshav Santhanam0e4c3282016-07-14 10:25:16 -07001433
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001434 if (!c->pid_file_path.empty())
1435 minijail_write_pid_file(c->jail.get(), c->pid_file_path.value().c_str());
1436 minijail_reset_signal_mask(c->jail.get());
Dylan Reid837c74a2016-01-22 17:25:21 -08001437
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001438 /* Setup container namespaces. */
Stephen Barber771653f2017-10-04 23:48:57 -07001439 if (container_config_has_namespace(config, "ipc"))
1440 minijail_namespace_ipc(c->jail.get());
1441 if (container_config_has_namespace(config, "mount"))
1442 minijail_namespace_vfs(c->jail.get());
1443 if (container_config_has_namespace(config, "network"))
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001444 minijail_namespace_net(c->jail.get());
Stephen Barber771653f2017-10-04 23:48:57 -07001445 if (container_config_has_namespace(config, "pid"))
1446 minijail_namespace_pids(c->jail.get());
1447
1448 if (container_config_has_namespace(config, "user")) {
1449 minijail_namespace_user(c->jail.get());
1450 if (minijail_uidmap(c->jail.get(), config->uid_map.c_str()) != 0)
1451 return -1;
1452 if (minijail_gidmap(c->jail.get(), config->gid_map.c_str()) != 0)
1453 return -1;
1454 }
1455
1456 if (container_config_has_namespace(config, "cgroup"))
1457 minijail_namespace_cgroups(c->jail.get());
1458
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001459 if (getuid() != 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001460 minijail_namespace_user_disable_setgroups(c->jail.get());
Dylan Reid837c74a2016-01-22 17:25:21 -08001461
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001462 /* Set the UID/GID inside the container if not 0. */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001463 if (!GetUsernsOutsideId(config->uid_map, config->uid, nullptr))
1464 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001465 else if (config->uid > 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001466 minijail_change_uid(c->jail.get(), config->uid);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001467 if (!GetUsernsOutsideId(config->gid_map, config->gid, nullptr))
1468 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001469 else if (config->gid > 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001470 minijail_change_gid(c->jail.get(), config->gid);
Keshav Santhanam36485ff2016-08-02 16:21:02 -07001471
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001472 if (minijail_enter_pivot_root(c->jail.get(), c->runfsroot.value().c_str()) !=
1473 0) {
1474 return -1;
1475 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001476
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -07001477 // Add the cgroups configured above.
1478 for (int32_t i = 0; i < libcontainer::Cgroup::Type::NUM_TYPES; i++) {
1479 if (c->cgroup->has_tasks_path(i)) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001480 if (minijail_add_to_cgroup(
1481 c->jail.get(), c->cgroup->tasks_path(i).value().c_str()) != 0) {
1482 return -1;
1483 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001484 }
1485 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001486
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001487 if (!config->alt_syscall_table.empty())
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001488 minijail_use_alt_syscall(c->jail.get(), config->alt_syscall_table.c_str());
Dylan Reid837c74a2016-01-22 17:25:21 -08001489
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001490 for (int i = 0; i < config->num_rlimits; i++) {
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001491 const Rlimit& lim = config->rlimits[i];
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001492 if (minijail_rlimit(c->jail.get(), lim.type, lim.cur, lim.max) != 0)
1493 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001494 }
Dylan Reid93fa4602017-06-06 13:39:31 -07001495
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001496 if (!config->selinux_context.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001497 if (minijail_add_hook(c->jail.get(), &Setexeccon,
1498 const_cast<char*>(config->selinux_context.c_str()),
1499 MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) {
1500 return -1;
1501 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001502 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001503
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001504 if (config->pre_start_hook) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001505 if (minijail_add_hook(c->jail.get(), config->pre_start_hook,
1506 config->pre_start_hook_payload,
1507 MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) {
1508 return -1;
1509 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001510 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001511
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001512 // Now that all pre-requisite hooks are installed, copy the ones in the
1513 // container_config object in the correct order.
1514 for (const auto& config_hook : config->hooks) {
1515 auto it = hook_callbacks.insert(std::make_pair(
1516 config_hook.first, std::vector<libcontainer::HookCallback>()));
1517 it.first->second.insert(it.first->second.end(), config_hook.second.begin(),
1518 config_hook.second.end());
1519 }
1520
1521 c->hook_states.clear();
1522 // Reserve enough memory to hold all the hooks, so that their addresses do not
1523 // get invalidated by reallocation.
1524 c->hook_states.reserve(MINIJAIL_HOOK_EVENT_MAX);
1525 for (minijail_hook_event_t event : {MINIJAIL_HOOK_EVENT_PRE_CHROOT,
1526 MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS,
1527 MINIJAIL_HOOK_EVENT_PRE_EXECVE}) {
1528 const auto& it = hook_callbacks.find(event);
1529 if (it == hook_callbacks.end())
1530 continue;
1531 c->hook_states.emplace_back(
1532 std::make_pair(libcontainer::HookState(), it->second));
1533 if (!c->hook_states.back().first.InstallHook(c->jail.get(), event))
1534 return -1;
1535 }
1536
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001537 for (int fd : config->inherited_fds) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001538 if (minijail_preserve_fd(c->jail.get(), fd, fd) != 0)
1539 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001540 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001541
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001542 /* TODO(dgreid) - remove this once shared mounts are cleaned up. */
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001543 minijail_skip_remount_private(c->jail.get());
Dylan Reid3da683b2016-04-05 03:35:35 -07001544
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001545 if (!config->keep_fds_open)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001546 minijail_close_open_fds(c->jail.get());
Luis Hector Chaveze18e7d42016-10-12 07:35:32 -07001547
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001548 if (config->use_capmask) {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001549 minijail_use_caps(c->jail.get(), config->capmask);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001550 if (config->use_capmask_ambient)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001551 minijail_set_ambient_caps(c->jail.get());
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001552 if (config->securebits_skip_mask) {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001553 minijail_skip_setting_securebits(c->jail.get(),
1554 config->securebits_skip_mask);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001555 }
1556 }
Luis Hector Chavezff5978f2017-06-27 12:52:58 -07001557
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001558 if (!config->do_init)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001559 minijail_run_as_init(c->jail.get());
Luis Hector Chavezdac65c32017-07-21 10:30:23 -07001560
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001561 std::vector<char*> argv_cstr;
1562 argv_cstr.reserve(config->program_argv.size() + 1);
1563 for (const auto& arg : config->program_argv)
1564 argv_cstr.emplace_back(const_cast<char*>(arg.c_str()));
1565 argv_cstr.emplace_back(nullptr);
1566
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001567 if (minijail_run_pid_pipes_no_preload(c->jail.get(), argv_cstr[0],
1568 argv_cstr.data(), &c->init_pid, nullptr,
1569 nullptr, nullptr) != 0) {
1570 return -1;
1571 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001572
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001573 // |hook_states| is already sorted in the correct order.
1574 for (auto& hook_state : c->hook_states) {
1575 if (!hook_state.first.WaitForHookAndRun(hook_state.second, c->init_pid))
1576 return -1;
1577 }
1578
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001579 // The container has started successfully, no need to tear it down anymore.
1580 ignore_result(teardown.Release());
1581 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -08001582}
1583
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001584const char* container_root(struct container* c) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001585 return c->runfs.value().c_str();
Dylan Reid837c74a2016-01-22 17:25:21 -08001586}
1587
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001588int container_pid(struct container* c) {
1589 return c->init_pid;
Dylan Reid837c74a2016-01-22 17:25:21 -08001590}
1591
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001592int container_wait(struct container* c) {
1593 int rc;
Dylan Reidcf745c52016-04-22 10:18:03 -07001594
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001595 do {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001596 rc = minijail_wait(c->jail.get());
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001597 } while (rc == -EINTR);
Dylan Reidcf745c52016-04-22 10:18:03 -07001598
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001599 // If the process had already been reaped, still perform teardown.
1600 if (rc == -ECHILD || rc >= 0) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001601 if (!ContainerTeardown(c))
1602 rc = -errno;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001603 }
1604 return rc;
Dylan Reid837c74a2016-01-22 17:25:21 -08001605}
1606
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001607int container_kill(struct container* c) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001608 if (kill(c->init_pid, SIGKILL) != 0 && errno != ESRCH) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -07001609 PLOG(ERROR) << "Failed to kill " << c->init_pid;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001610 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -07001611 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001612 return container_wait(c);
Dylan Reid837c74a2016-01-22 17:25:21 -08001613}
yusukesbbc37a72017-11-21 09:51:54 -08001614
yusukes32622542018-01-05 18:59:52 -08001615char* container_config_dump(struct container_config* c, int sort_vectors) {
yusukesbbc37a72017-11-21 09:51:54 -08001616 std::stringstream out;
yusukes32622542018-01-05 18:59:52 -08001617 DumpConfig(&out, c, sort_vectors);
yusukesbbc37a72017-11-21 09:51:54 -08001618 return strdup(out.str().c_str());
1619}