blob: 8490b845e94c7e391d296c1e384a223817f57380 [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
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
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700177 // CPU cgroup params.
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700178 CpuCgroup cpu_cgparams;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700179
180 // Parent dir for cgroup creation
181 base::FilePath cgroup_parent;
182
183 // uid to own the created cgroups
yusukesb7b9a042017-12-08 13:14:25 -0800184 uid_t cgroup_owner = 0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700185
186 // gid to own the created cgroups
yusukesb7b9a042017-12-08 13:14:25 -0800187 gid_t cgroup_group = 0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700188
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700189 // Allow the child process to keep open FDs (for stdin/out/err).
yusukesf125f332017-12-08 13:45:15 -0800190 bool keep_fds_open = false;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700191
192 // Array of rlimits for the contained process.
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700193 Rlimit rlimits[kMaxRlimits];
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700194
195 // The number of elements in `rlimits`.
yusukesb7b9a042017-12-08 13:14:25 -0800196 int num_rlimits = 0;
yusukesf125f332017-12-08 13:45:15 -0800197 bool use_capmask = false;
198 bool use_capmask_ambient = false;
yusukesb7b9a042017-12-08 13:14:25 -0800199 uint64_t capmask = 0x0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700200
201 // The mask of securebits to skip when restricting caps.
yusukesb7b9a042017-12-08 13:14:25 -0800202 uint64_t securebits_skip_mask = 0x0;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700203
204 // Whether the container needs an extra process to be run as init.
yusukesf125f332017-12-08 13:45:15 -0800205 bool do_init = false;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700206
207 // The SELinux context name the container will run under.
208 std::string selinux_context;
209
210 // A function pointer to be called prior to calling execve(2).
yusukesb7b9a042017-12-08 13:14:25 -0800211 minijail_hook_t pre_start_hook = nullptr;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700212
213 // Parameter that will be passed to pre_start_hook().
yusukesb7b9a042017-12-08 13:14:25 -0800214 void* pre_start_hook_payload = nullptr;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700215
Luis Hector Chaveze03926a2017-09-28 17:28:49 -0700216 // A list of file descriptors to inherit.
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700217 std::vector<int> inherited_fds;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700218
219 // A list of hooks that will be called upon minijail reaching various states
220 // of execution.
221 std::map<minijail_hook_event_t, std::vector<libcontainer::HookCallback>>
222 hooks;
Dylan Reid837c74a2016-01-22 17:25:21 -0800223};
224
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700225// Container manipulation
226struct container {
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -0700227 std::unique_ptr<libcontainer::Cgroup> cgroup;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700228 ScopedMinijail jail;
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700229 pid_t init_pid = -1;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700230 base::FilePath config_root;
231 base::FilePath runfs;
232 base::FilePath rundir;
233 base::FilePath runfsroot;
234 base::FilePath pid_file_path;
235
236 // Mounts made outside of the minijail.
237 std::vector<base::FilePath> ext_mounts;
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700238 std::vector<Loopdev> loopdevs;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700239 std::vector<std::string> device_mappers;
240 std::string name;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700241
242 std::vector<std::pair<libcontainer::HookState,
243 std::vector<libcontainer::HookCallback>>>
244 hook_states;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700245};
246
247namespace {
248
yusukes4d955472018-01-17 16:41:32 -0800249std::string GetMountFlagsAsString(int flags) {
250#define CHECK_MOUNT_FLAG(flag) \
251 do { \
252 if (flags & flag) \
253 result.push_back(#flag); \
254 } while (false)
255
256 std::vector<std::string> result;
257 CHECK_MOUNT_FLAG(MS_RDONLY);
258 CHECK_MOUNT_FLAG(MS_NOSUID);
259 CHECK_MOUNT_FLAG(MS_NODEV);
260 CHECK_MOUNT_FLAG(MS_NOEXEC);
261 CHECK_MOUNT_FLAG(MS_SYNCHRONOUS);
262 CHECK_MOUNT_FLAG(MS_REMOUNT);
263 CHECK_MOUNT_FLAG(MS_MANDLOCK);
264 CHECK_MOUNT_FLAG(MS_DIRSYNC);
265 CHECK_MOUNT_FLAG(MS_NOATIME);
266 CHECK_MOUNT_FLAG(MS_NODIRATIME);
267 CHECK_MOUNT_FLAG(MS_BIND);
268 CHECK_MOUNT_FLAG(MS_MOVE);
269 CHECK_MOUNT_FLAG(MS_REC);
270 CHECK_MOUNT_FLAG(MS_SILENT);
271 CHECK_MOUNT_FLAG(MS_POSIXACL);
272 CHECK_MOUNT_FLAG(MS_UNBINDABLE);
273 CHECK_MOUNT_FLAG(MS_PRIVATE);
274 CHECK_MOUNT_FLAG(MS_SLAVE);
275 CHECK_MOUNT_FLAG(MS_SHARED);
276 return result.empty() ? "no flags" : base::JoinString(result, " | ");
277
278#undef CHECK_MOUNT_FLAG
279}
280
yusukesbbc37a72017-11-21 09:51:54 -0800281std::ostream& operator<<(std::ostream& stream, const Mount& mount) {
282 stream << "mount:" << std::endl
283 << " name: " << QUOTE(mount.name) << std::endl
284 << " source: " << QUOTE(mount.source.value()) << std::endl
285 << " destination: " << QUOTE(mount.destination.value()) << std::endl
286 << " type: " << QUOTE(mount.type) << std::endl
287 << " data: " << QUOTE(mount.data) << std::endl
288 << " verity: " << QUOTE(mount.verity) << std::endl
yusukes4d955472018-01-17 16:41:32 -0800289 << " flags: 0x" << std::hex << mount.flags << std::dec << " ("
290 << GetMountFlagsAsString(mount.flags) << ")" << std::endl
yusukesbbc37a72017-11-21 09:51:54 -0800291 << " uid: " << mount.uid << std::endl
292 << " gid: " << mount.gid << std::endl
293 << " mode: 0" << std::oct << mount.mode << std::dec << std::endl
294 << " mount_in_ns: " << mount.mount_in_ns << std::endl
295 << " create: " << mount.create << std::endl
296 << " loopback: " << mount.loopback << std::endl;
297
298 return stream;
299}
300
301std::ostream& operator<<(std::ostream& stream, const Device& device) {
302 stream << "device:" << std::endl
303 << " type: " << device.type << std::endl
304 << " path: " << QUOTE(device.path.value()) << std::endl
305 << " fs_permissions: 0" << std::oct << device.fs_permissions
306 << std::dec << std::endl
307 << " major: " << device.major << std::endl
308 << " minor: " << device.minor << std::endl
309 << " copy_minor: " << device.copy_minor << std::endl
310 << " uid: " << device.uid << std::endl
311 << " gid: " << device.gid << std::endl;
312
313 return stream;
314}
315
316std::ostream& operator<<(std::ostream& stream,
317 const CgroupDevice& cgroup_device) {
318 stream << "cgroup_device:" << std::endl
319 << " allow: " << cgroup_device.allow << std::endl
320 << " type: " << cgroup_device.type << std::endl
321 << " major: " << cgroup_device.major << std::endl
322 << " minor: " << cgroup_device.minor << std::endl
323 << " read: " << cgroup_device.read << std::endl
324 << " write: " << cgroup_device.write << std::endl
325 << " modify: " << cgroup_device.modify << std::endl;
326
327 return stream;
328}
329
330std::ostream& operator<<(std::ostream& stream, const CpuCgroup& cpu_cgroup) {
331 stream << "cpu_cgroup:" << std::endl
332 << " shares: " << cpu_cgroup.shares << std::endl
333 << " quota: " << cpu_cgroup.quota << std::endl
334 << " period: " << cpu_cgroup.period << std::endl
335 << " rt_runtime: " << cpu_cgroup.rt_runtime << std::endl
336 << " rt_period: " << cpu_cgroup.rt_period << std::endl;
337
338 return stream;
339}
340
341std::ostream& operator<<(std::ostream& stream, const Rlimit& rlimit) {
342 stream << "rlimit:" << std::endl
343 << " type: " << rlimit.type << std::endl
344 << " cur: " << rlimit.cur << std::endl
345 << " max: " << rlimit.max << std::endl;
346
347 return stream;
348}
349
yusukes32622542018-01-05 18:59:52 -0800350void DumpConfig(std::ostream* stream,
351 const container_config* c,
352 bool sort_vectors) {
353 *stream << "config_root: " << QUOTE(c->config_root.value()) << std::endl
354 << "rootfs: " << QUOTE(c->rootfs.value()) << std::endl
355 << "rootfs_mount_flags: 0x" << std::hex << c->rootfs_mount_flags
yusukes4d955472018-01-17 16:41:32 -0800356 << std::dec << " (" << GetMountFlagsAsString(c->rootfs_mount_flags)
357 << ")" << std::endl
yusukes32622542018-01-05 18:59:52 -0800358 << "premounted_runfs: " << QUOTE(c->premounted_runfs.value())
359 << std::endl
360 << "pid_file_path: " << QUOTE(c->pid_file_path.value()) << std::endl
361 << "program_argv: size=" << c->program_argv.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800362
363 for (const std::string& argv : c->program_argv)
yusukes32622542018-01-05 18:59:52 -0800364 *stream << " " << QUOTE(argv) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800365
yusukes32622542018-01-05 18:59:52 -0800366 *stream << "uid: " << c->uid << std::endl
367 << "uid_map: " << QUOTE(c->uid_map) << std::endl
368 << "gid: " << c->gid << std::endl
369 << "gid_map: " << QUOTE(c->gid_map) << std::endl
370 << "alt_syscall_table: " << QUOTE(c->alt_syscall_table) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800371
yusukes32622542018-01-05 18:59:52 -0800372 auto mount_sorted = c->mounts;
373 if (sort_vectors) {
374 std::stable_sort(mount_sorted.begin(), mount_sorted.end(),
375 [](const Mount& lhs, const Mount& rhs) {
376 return std::make_tuple(lhs.destination.value(),
377 lhs.source.value(), lhs.flags) <
378 std::make_tuple(rhs.destination.value(),
379 rhs.source.value(), rhs.flags);
380 });
381 }
382 for (const auto& mount : mount_sorted)
383 *stream << mount;
yusukesbbc37a72017-11-21 09:51:54 -0800384
yusukes32622542018-01-05 18:59:52 -0800385 *stream << "namespaces: size=" << c->namespaces.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800386 for (const std::string& ns : c->namespaces)
yusukes32622542018-01-05 18:59:52 -0800387 *stream << " " << QUOTE(ns) << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800388
yusukes32622542018-01-05 18:59:52 -0800389 auto devices_sorted = c->devices;
390 if (sort_vectors) {
391 std::stable_sort(devices_sorted.begin(), devices_sorted.end(),
392 [](const Device& lhs, const Device& rhs) {
393 return lhs.path.value() < rhs.path.value();
394 });
395 }
396 for (const auto& device : devices_sorted)
397 *stream << device;
yusukesbbc37a72017-11-21 09:51:54 -0800398
yusukes32622542018-01-05 18:59:52 -0800399 auto cgroup_devices_sorted = c->cgroup_devices;
400 if (sort_vectors) {
401 std::stable_sort(cgroup_devices_sorted.begin(), cgroup_devices_sorted.end(),
402 [](const CgroupDevice& lhs, const CgroupDevice& rhs) {
403 return std::make_tuple(lhs.type, lhs.major, lhs.minor) <
404 std::make_tuple(rhs.type, rhs.major, rhs.minor);
405 });
406 }
407 for (const auto& cgroup_device : cgroup_devices_sorted)
408 *stream << cgroup_device;
yusukesbbc37a72017-11-21 09:51:54 -0800409
yusukese67af442017-12-23 00:52:15 -0800410 *stream << c->cpu_cgparams
yusukes32622542018-01-05 18:59:52 -0800411 << "cgroup_parent: " << QUOTE(c->cgroup_parent.value()) << std::endl
412 << "cgroup_owner: " << c->cgroup_owner << std::endl
413 << "cgroup_group: " << c->cgroup_group << std::endl
414 << "keep_fds_open: " << c->keep_fds_open << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800415
yusukes32622542018-01-05 18:59:52 -0800416 *stream << "num_rlimits: " << c->num_rlimits << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800417 for (size_t i = 0; i < c->num_rlimits; ++i)
yusukes32622542018-01-05 18:59:52 -0800418 *stream << c->rlimits[i];
yusukesbbc37a72017-11-21 09:51:54 -0800419
yusukes32622542018-01-05 18:59:52 -0800420 *stream << "use_capmask: " << c->use_capmask << std::endl
421 << "use_capmask_ambient: " << c->use_capmask_ambient << std::endl
422 << "capmask: 0x" << std::hex << c->capmask << std::dec << std::endl
423 << "securebits_skip_mask: 0x" << std::hex << c->securebits_skip_mask
424 << std::dec << std::endl
425 << "do_init: " << c->do_init << std::endl
426 << "selinux_context: " << QUOTE(c->selinux_context) << std::endl
427 << "pre_start_hook: " << reinterpret_cast<void*>(c->pre_start_hook)
428 << std::endl
429 << "pre_start_hook_payload: " << c->pre_start_hook_payload
430 << std::endl
431 << "inherited_fds: size=" << c->inherited_fds.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800432
433 for (int fd : c->inherited_fds)
yusukes32622542018-01-05 18:59:52 -0800434 *stream << " " << fd << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800435
yusukes32622542018-01-05 18:59:52 -0800436 *stream << "hooks: size=" << c->hooks.size() << std::endl;
yusukesbbc37a72017-11-21 09:51:54 -0800437}
438
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700439// Returns the path for |path_in_container| in the outer namespace.
440base::FilePath GetPathInOuterNamespace(
441 const base::FilePath& root, const base::FilePath& path_in_container) {
442 if (path_in_container.IsAbsolute())
443 return base::FilePath(root.value() + path_in_container.value());
444 return root.Append(path_in_container);
445}
446
447// Make sure the mount target exists in the new rootfs. Create if needed and
448// possible.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700449bool SetupMountDestination(const struct container_config* config,
450 const Mount& mount,
451 const base::FilePath& source,
452 const base::FilePath& dest) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700453 struct stat st_buf;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700454 if (stat(dest.value().c_str(), &st_buf) == 0) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700455 // destination exists.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700456 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700457 }
458
459 // Try to create the destination. Either make directory or touch a file
460 // depending on the source type.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700461 int uid_userns;
462 if (!GetUsernsOutsideId(config->uid_map, mount.uid, &uid_userns))
463 return false;
464 int gid_userns;
465 if (!GetUsernsOutsideId(config->gid_map, mount.gid, &gid_userns))
466 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700467
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700468 if (stat(source.value().c_str(), &st_buf) != 0 || S_ISDIR(st_buf.st_mode) ||
469 S_ISBLK(st_buf.st_mode)) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700470 return MakeDir(dest, uid_userns, gid_userns, mount.mode);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700471 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700472
473 return TouchFile(dest, uid_userns, gid_userns, mount.mode);
474}
475
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700476// Unmounts anything we mounted in this mount namespace in the opposite order
477// that they were mounted.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700478bool UnmountExternalMounts(struct container* c) {
479 bool ret = true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700480
481 for (auto it = c->ext_mounts.rbegin(); it != c->ext_mounts.rend(); ++it) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700482 if (umount(it->value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700483 PLOG(ERROR) << "Failed to unmount " << it->value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700484 ret = false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700485 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700486 }
487 c->ext_mounts.clear();
488
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700489 for (auto it = c->loopdevs.rbegin(); it != c->loopdevs.rend(); ++it) {
490 if (!LoopdevDetach(&(*it)))
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700491 ret = false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700492 }
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700493 c->loopdevs.clear();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700494
495 for (auto it = c->device_mappers.rbegin(); it != c->device_mappers.rend();
496 ++it) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700497 if (!DeviceMapperDetach(*it))
498 ret = false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700499 }
500 c->device_mappers.clear();
501
502 return ret;
503}
504
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700505bool DoContainerMount(struct container* c,
506 const struct container_config* config,
507 const Mount& mount) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700508 base::FilePath dest =
509 GetPathInOuterNamespace(c->runfsroot, mount.destination);
510
511 // If it's a bind mount relative to rootfs, append source to
512 // rootfs path, otherwise source path is absolute.
513 base::FilePath source;
514 if ((mount.flags & MS_BIND) && !mount.source.IsAbsolute()) {
515 source = GetPathInOuterNamespace(c->runfsroot, mount.source);
516 } else if (mount.loopback && !mount.source.IsAbsolute() &&
517 !c->config_root.empty()) {
518 source = GetPathInOuterNamespace(c->config_root, mount.source);
519 } else {
520 source = mount.source;
521 }
522
523 // Only create the destinations for external mounts, minijail will take
524 // care of those mounted in the new namespace.
525 if (mount.create && !mount.mount_in_ns) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700526 if (!SetupMountDestination(config, mount, source, dest))
527 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700528 }
529 if (mount.loopback) {
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700530 Loopdev loopdev;
531 if (!LoopdevSetup(source, &loopdev))
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700532 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700533
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700534 // Replace the mount source with the loopback device path.
535 source = loopdev.path;
536
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700537 // Save this to cleanup when shutting down.
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700538 c->loopdevs.emplace_back(std::move(loopdev));
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700539 }
540 if (!mount.verity.empty()) {
541 // Set this device up via dm-verity.
542 std::string dm_name;
543 base::FilePath dm_source = source;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700544 if (!DeviceMapperSetup(dm_source, mount.verity, &source, &dm_name))
545 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700546
547 // Save this to cleanup when shutting down.
548 c->device_mappers.push_back(dm_name);
549 }
550 if (mount.mount_in_ns) {
551 // We can mount this with minijail.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700552 if (minijail_mount_with_data(
553 c->jail.get(), source.value().c_str(),
554 mount.destination.value().c_str(), mount.type.c_str(), mount.flags,
555 mount.data.empty() ? nullptr : mount.data.c_str()) != 0) {
556 return false;
557 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700558 } else {
559 // Mount this externally and unmount it on exit.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700560 if (!MountExternal(source.value(), dest.value(), mount.type, mount.flags,
561 mount.data)) {
562 return false;
563 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700564 // Save this to unmount when shutting down.
565 c->ext_mounts.push_back(dest);
566 }
567
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700568 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700569}
570
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700571bool DoContainerMounts(struct container* c,
572 const struct container_config* config) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700573 UnmountExternalMounts(c);
574
575 // This will run in all the error cases.
576 base::ScopedClosureRunner teardown(base::Bind(
577 base::IgnoreResult(&UnmountExternalMounts), base::Unretained(c)));
578
579 for (const auto& mount : config->mounts) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700580 if (!DoContainerMount(c, config, mount))
581 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700582 }
583
584 // The mounts have been done successfully, no need to tear them down anymore.
585 ignore_result(teardown.Release());
586
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700587 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700588}
589
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700590bool ContainerCreateDevice(const struct container* c,
591 const struct container_config* config,
592 const Device& dev,
Stephen Barber7bae6642017-11-30 10:47:12 -0800593 int major,
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700594 int minor) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700595 mode_t mode = dev.fs_permissions;
596 switch (dev.type) {
597 case 'b':
598 mode |= S_IFBLK;
599 break;
600 case 'c':
601 mode |= S_IFCHR;
602 break;
603 default:
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700604 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700605 }
606
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700607 int uid_userns;
608 if (!GetUsernsOutsideId(config->uid_map, dev.uid, &uid_userns))
609 return false;
610 int gid_userns;
611 if (!GetUsernsOutsideId(config->gid_map, dev.gid, &gid_userns))
612 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700613
614 base::FilePath path = GetPathInOuterNamespace(c->runfsroot, dev.path);
Luis Hector Chavez92278e82017-10-16 11:30:27 -0700615 if (!libcontainer::CreateDirectoryOwnedBy(path.DirName(), 0755, uid_userns,
616 gid_userns)) {
Luis Hector Chavez5d51abb2017-10-11 17:05:57 -0700617 PLOG(ERROR) << "Failed to create parent directory for " << path.value();
618 return false;
619 }
Stephen Barber7bae6642017-11-30 10:47:12 -0800620 if (mknod(path.value().c_str(), mode, makedev(major, minor)) != 0 &&
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700621 errno != EEXIST) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700622 PLOG(ERROR) << "Failed to mknod " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700623 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700624 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700625 if (chown(path.value().c_str(), uid_userns, gid_userns) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700626 PLOG(ERROR) << "Failed to chown " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700627 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700628 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700629 if (chmod(path.value().c_str(), dev.fs_permissions) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700630 PLOG(ERROR) << "Failed to chmod " << path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700631 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700632 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700633
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700634 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700635}
636
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700637bool MountRunfs(struct container* c, const struct container_config* config) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700638 {
639 std::string runfs_template = base::StringPrintf(
640 "%s/%s_XXXXXX", c->rundir.value().c_str(), c->name.c_str());
641 // TODO(lhchavez): Replace this with base::CreateTemporaryDirInDir().
642 char* runfs_path = mkdtemp(const_cast<char*>(runfs_template.c_str()));
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700643 if (!runfs_path) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700644 PLOG(ERROR) << "Failed to mkdtemp in " << c->rundir.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700645 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700646 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700647 c->runfs = base::FilePath(runfs_path);
648 }
649
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700650 int uid_userns;
651 if (!GetUsernsOutsideId(config->uid_map, config->uid, &uid_userns))
652 return false;
653 int gid_userns;
654 if (!GetUsernsOutsideId(config->gid_map, config->gid, &gid_userns))
655 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700656
657 // Make sure the container uid can access the rootfs.
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700658 if (chmod(c->runfs.value().c_str(), 0700) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700659 PLOG(ERROR) << "Failed to chmod " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700660 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700661 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700662 if (chown(c->runfs.value().c_str(), uid_userns, gid_userns) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700663 PLOG(ERROR) << "Failed to chown " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700664 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700665 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700666
667 c->runfsroot = c->runfs.Append("root");
668
669 constexpr mode_t kRootDirMode = 0660;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700670 if (mkdir(c->runfsroot.value().c_str(), kRootDirMode) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700671 PLOG(ERROR) << "Failed to mkdir " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700672 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700673 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700674 if (chmod(c->runfsroot.value().c_str(), kRootDirMode) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700675 PLOG(ERROR) << "Failed to chmod " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700676 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700677 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700678
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700679 if (mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "",
680 MS_BIND | (config->rootfs_mount_flags & MS_REC), nullptr) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700681 PLOG(ERROR) << "Failed to bind-mount " << config->rootfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700682 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700683 }
684
685 // MS_BIND ignores any flags passed to it (except MS_REC). We need a
686 // second call to mount() to actually set them.
687 if (config->rootfs_mount_flags &&
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700688 mount(config->rootfs.value().c_str(), c->runfsroot.value().c_str(), "",
689 (config->rootfs_mount_flags & ~MS_REC), nullptr) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700690 PLOG(ERROR) << "Failed to remount " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700691 return false;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700692 }
693
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700694 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700695}
696
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700697bool CreateDeviceNodes(struct container* c,
698 const struct container_config* config,
699 pid_t container_pid) {
700 for (const auto& dev : config->devices) {
Stephen Barber7bae6642017-11-30 10:47:12 -0800701 int major = dev.major;
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700702 int minor = dev.minor;
703
Stephen Barber7bae6642017-11-30 10:47:12 -0800704 if (dev.copy_major || dev.copy_minor) {
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700705 struct stat st_buff;
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700706 if (stat(dev.path.value().c_str(), &st_buff) != 0)
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700707 continue;
Stephen Barber7bae6642017-11-30 10:47:12 -0800708
709 if (dev.copy_major)
710 major = major(st_buff.st_rdev);
711 if (dev.copy_minor)
712 minor = minor(st_buff.st_rdev);
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700713 }
Stephen Barber7bae6642017-11-30 10:47:12 -0800714 if (major < 0 || minor < 0)
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700715 continue;
Stephen Barber7bae6642017-11-30 10:47:12 -0800716 if (!ContainerCreateDevice(c, config, dev, major, minor))
Luis Hector Chavez644d2042017-09-19 18:56:44 -0700717 return false;
718 }
719
720 return true;
721}
722
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700723bool DeviceSetup(struct container* c, const struct container_config* config) {
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -0700724 c->cgroup->DenyAllDevices();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700725
726 for (const auto& dev : config->cgroup_devices) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700727 if (!c->cgroup->AddDevice(dev.allow, dev.major, dev.minor, dev.read,
728 dev.write, dev.modify, dev.type)) {
729 return false;
730 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700731 }
732
Luis Hector Chavez5cf71ed2018-05-07 14:45:43 -0700733 for (const auto& loopdev : c->loopdevs) {
734 if (!c->cgroup->AddDevice(1, major(loopdev.info.lo_rdevice),
735 minor(loopdev.info.lo_rdevice), 1, 0, 0, 'b')) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700736 return false;
737 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700738 }
739
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700740 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700741}
742
743int Setexeccon(void* payload) {
744 char* init_domain = reinterpret_cast<char*>(payload);
745 pid_t tid = syscall(SYS_gettid);
746
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700747 if (tid < 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700748 PLOG(ERROR) << "Failed to gettid";
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700749 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700750 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700751
752 std::string exec_path =
753 base::StringPrintf("/proc/self/task/%d/attr/exec", tid);
754
755 base::ScopedFD fd(open(exec_path.c_str(), O_WRONLY | O_CLOEXEC));
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700756 if (!fd.is_valid()) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700757 PLOG(ERROR) << "Failed to open " << exec_path;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700758 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700759 }
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700760
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700761 if (!base::WriteFileDescriptor(fd.get(), init_domain, strlen(init_domain))) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700762 PLOG(ERROR) << "Failed to write the SELinux label to " << exec_path;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700763 return -errno;
764 }
765
766 return 0;
767}
768
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700769bool ContainerTeardown(struct container* c) {
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700770 UnmountExternalMounts(c);
771 if (!c->runfsroot.empty() && !c->runfs.empty()) {
772 /* |c->runfsroot| may have been mounted recursively. Thus use
773 * MNT_DETACH to "immediately disconnect the filesystem and all
774 * filesystems mounted below it from each other and from the
775 * mount table". Otherwise one would need to unmount every
776 * single dependent mount before unmounting |c->runfsroot|
777 * itself.
778 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700779 if (umount2(c->runfsroot.value().c_str(), MNT_DETACH) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700780 PLOG(ERROR) << "Failed to detach " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700781 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700782 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700783 if (rmdir(c->runfsroot.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700784 PLOG(ERROR) << "Failed to rmdir " << c->runfsroot.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700785 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700786 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700787 c->runfsroot = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700788 }
789 if (!c->pid_file_path.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700790 if (unlink(c->pid_file_path.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700791 PLOG(ERROR) << "Failed to unlink " << c->pid_file_path.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700792 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700793 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700794 c->pid_file_path = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700795 }
796 if (!c->runfs.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700797 if (rmdir(c->runfs.value().c_str()) != 0) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -0700798 PLOG(ERROR) << "Failed to rmdir " << c->runfs.value();
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700799 return false;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -0700800 }
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700801 c->runfs = base::FilePath();
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700802 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700803 return true;
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700804}
805
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -0700806void CancelContainerStart(struct container* c) {
807 if (c->init_pid != -1)
808 container_kill(c);
809 ContainerTeardown(c);
810}
Luis Hector Chavez81efb332017-09-18 14:01:29 -0700811
812} // namespace
813
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700814struct container_config* container_config_create() {
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700815 return new (std::nothrow) struct container_config();
Dylan Reid837c74a2016-01-22 17:25:21 -0800816}
817
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700818void container_config_destroy(struct container_config* c) {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700819 if (c == nullptr)
820 return;
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700821 delete c;
Dylan Reid837c74a2016-01-22 17:25:21 -0800822}
823
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700824int container_config_config_root(struct container_config* c,
825 const char* config_root) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700826 c->config_root = base::FilePath(config_root);
827 return 0;
Mike Frysingerb22acdf2017-01-08 02:02:35 -0500828}
829
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700830const char* container_config_get_config_root(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700831 return c->config_root.value().c_str();
Mike Frysingerb22acdf2017-01-08 02:02:35 -0500832}
833
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700834int container_config_rootfs(struct container_config* c, const char* rootfs) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700835 c->rootfs = base::FilePath(rootfs);
836 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800837}
838
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700839const char* container_config_get_rootfs(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700840 return c->rootfs.value().c_str();
Dylan Reid11456722016-05-02 11:24:50 -0700841}
842
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700843void container_config_rootfs_mount_flags(struct container_config* c,
844 unsigned long rootfs_mount_flags) {
845 /* Since we are going to add MS_REMOUNT anyways, add it here so we can
846 * simply check against zero later. MS_BIND is also added to avoid
847 * re-mounting the original filesystem, since the rootfs is always
848 * bind-mounted.
849 */
850 c->rootfs_mount_flags = MS_REMOUNT | MS_BIND | rootfs_mount_flags;
Luis Hector Chavezc240e7e2016-09-22 10:33:03 -0700851}
852
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700853unsigned long container_config_get_rootfs_mount_flags(
854 const struct container_config* c) {
855 return c->rootfs_mount_flags;
Luis Hector Chavezc240e7e2016-09-22 10:33:03 -0700856}
857
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700858int container_config_premounted_runfs(struct container_config* c,
859 const char* runfs) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700860 c->premounted_runfs = base::FilePath(runfs);
861 return 0;
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700862}
863
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700864const char* container_config_get_premounted_runfs(
865 const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700866 return c->premounted_runfs.value().c_str();
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700867}
868
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700869int container_config_pid_file(struct container_config* c, const char* path) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700870 c->pid_file_path = base::FilePath(path);
871 return 0;
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700872}
873
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700874const char* container_config_get_pid_file(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700875 return c->pid_file_path.value().c_str();
Keshav Santhanam0e4c3282016-07-14 10:25:16 -0700876}
877
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700878int container_config_program_argv(struct container_config* c,
879 const char** argv,
880 size_t num_args) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700881 if (num_args < 1) {
882 errno = EINVAL;
883 return -1;
884 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700885 c->program_argv.clear();
886 c->program_argv.reserve(num_args);
887 for (size_t i = 0; i < num_args; ++i)
888 c->program_argv.emplace_back(argv[i]);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700889 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800890}
891
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700892size_t container_config_get_num_program_args(const struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700893 return c->program_argv.size();
Dylan Reid11456722016-05-02 11:24:50 -0700894}
895
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700896const char* container_config_get_program_arg(const struct container_config* c,
897 size_t index) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700898 if (index >= c->program_argv.size())
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700899 return nullptr;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700900 return c->program_argv[index].c_str();
Dylan Reid11456722016-05-02 11:24:50 -0700901}
902
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700903void container_config_uid(struct container_config* c, uid_t uid) {
904 c->uid = uid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700905}
906
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700907uid_t container_config_get_uid(const struct container_config* c) {
908 return c->uid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700909}
910
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700911int container_config_uid_map(struct container_config* c, const char* uid_map) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700912 c->uid_map = uid_map;
913 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800914}
915
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700916void container_config_gid(struct container_config* c, gid_t gid) {
917 c->gid = gid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700918}
919
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700920gid_t container_config_get_gid(const struct container_config* c) {
921 return c->gid;
Dylan Reid1874feb2016-06-22 17:53:50 -0700922}
923
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700924int container_config_gid_map(struct container_config* c, const char* gid_map) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700925 c->gid_map = gid_map;
926 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800927}
928
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700929int container_config_alt_syscall_table(struct container_config* c,
930 const char* alt_syscall_table) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -0700931 c->alt_syscall_table = alt_syscall_table;
932 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800933}
934
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700935int container_config_add_rlimit(struct container_config* c,
936 int type,
Luis Hector Chavezda352462018-01-30 09:10:00 -0800937 rlim_t cur,
938 rlim_t max) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700939 if (c->num_rlimits >= kMaxRlimits) {
940 errno = ENOMEM;
941 return -1;
942 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700943 c->rlimits[c->num_rlimits].type = type;
944 c->rlimits[c->num_rlimits].cur = cur;
945 c->rlimits[c->num_rlimits].max = max;
946 c->num_rlimits++;
947 return 0;
Dylan Reid93fa4602017-06-06 13:39:31 -0700948}
949
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700950int container_config_add_mount(struct container_config* c,
951 const char* name,
952 const char* source,
953 const char* destination,
954 const char* type,
955 const char* data,
956 const char* verity,
957 int flags,
958 int uid,
959 int gid,
960 int mode,
961 int mount_in_ns,
962 int create,
963 int loopback) {
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700964 if (name == nullptr || source == nullptr || destination == nullptr ||
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -0700965 type == nullptr) {
966 errno = EINVAL;
967 return -1;
968 }
Dylan Reid837c74a2016-01-22 17:25:21 -0800969
Luis Hector Chavez5381d002017-09-16 12:54:24 -0700970 c->mounts.emplace_back(Mount{name,
971 base::FilePath(source),
972 base::FilePath(destination),
973 type,
974 data ? data : "",
975 verity ? verity : "",
976 flags,
977 uid,
978 gid,
979 mode,
980 mount_in_ns != 0,
981 create != 0,
982 loopback != 0});
Luis Hector Chavez479b95f2016-06-06 08:01:05 -0700983
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700984 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -0800985}
986
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700987int container_config_add_cgroup_device(struct container_config* c,
988 int allow,
989 char type,
990 int major,
991 int minor,
992 int read,
993 int write,
994 int modify) {
Luis Hector Chaveze1062e82017-09-18 09:57:37 -0700995 c->cgroup_devices.emplace_back(CgroupDevice{
996 allow != 0, type, major, minor, read != 0, write != 0, modify != 0});
Dylan Reid4843d6b2017-03-31 18:14:30 -0700997
Luis Hector Chavez31735bc2017-09-15 08:17:10 -0700998 return 0;
Dylan Reid4843d6b2017-03-31 18:14:30 -0700999}
1000
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001001int container_config_add_device(struct container_config* c,
1002 char type,
1003 const char* path,
1004 int fs_permissions,
1005 int major,
1006 int minor,
Stephen Barber7bae6642017-11-30 10:47:12 -08001007 int copy_major,
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001008 int copy_minor,
1009 int uid,
1010 int gid,
1011 int read_allowed,
1012 int write_allowed,
1013 int modify_allowed) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001014 if (path == nullptr) {
1015 errno = EINVAL;
1016 return -1;
1017 }
Stephen Barber7bae6642017-11-30 10:47:12 -08001018 /* If using a dynamic major/minor number, ensure that major/minor is -1. */
1019 if ((copy_major && (major != -1)) || (copy_minor && (minor != -1))) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001020 errno = EINVAL;
1021 return -1;
1022 }
Dylan Reid355d5e42016-04-29 16:53:31 -07001023
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001024 if (read_allowed || write_allowed || modify_allowed) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001025 if (container_config_add_cgroup_device(c, 1, type, major, minor,
1026 read_allowed, write_allowed,
1027 modify_allowed) != 0) {
1028 errno = ENOMEM;
1029 return -1;
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001030 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001031 }
Luis Hector Chavez479b95f2016-06-06 08:01:05 -07001032
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001033 c->devices.emplace_back(Device{
Stephen Barber7bae6642017-11-30 10:47:12 -08001034 type, base::FilePath(path), fs_permissions, major, minor, copy_major != 0,
1035 copy_minor != 0, uid, gid,
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001036 });
1037
1038 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -08001039}
1040
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001041int container_config_set_cpu_shares(struct container_config* c, int shares) {
1042 /* CPU shares must be 2 or higher. */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001043 if (shares < 2) {
1044 errno = EINVAL;
1045 return -1;
1046 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001047
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001048 c->cpu_cgparams.shares = shares;
1049 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001050}
1051
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001052int container_config_set_cpu_cfs_params(struct container_config* c,
1053 int quota,
1054 int period) {
1055 /*
1056 * quota could be set higher than period to utilize more than one CPU.
1057 * quota could also be set as -1 to indicate the cgroup does not adhere
1058 * to any CPU time restrictions.
1059 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001060 if (quota <= 0 && quota != -1) {
1061 errno = EINVAL;
1062 return -1;
1063 }
1064 if (period <= 0) {
1065 errno = EINVAL;
1066 return -1;
1067 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001068
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001069 c->cpu_cgparams.quota = quota;
1070 c->cpu_cgparams.period = period;
1071 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001072}
1073
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001074int container_config_set_cpu_rt_params(struct container_config* c,
1075 int rt_runtime,
1076 int rt_period) {
1077 /*
1078 * rt_runtime could be set as 0 to prevent the cgroup from using
1079 * realtime CPU.
1080 */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001081 if (rt_runtime < 0 || rt_runtime >= rt_period) {
1082 errno = EINVAL;
1083 return -1;
1084 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001085
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001086 c->cpu_cgparams.rt_runtime = rt_runtime;
1087 c->cpu_cgparams.rt_period = rt_period;
1088 return 0;
Chinyue Chenfac909e2016-06-24 14:17:42 +08001089}
1090
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001091int container_config_get_cpu_shares(struct container_config* c) {
1092 return c->cpu_cgparams.shares;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001093}
1094
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001095int container_config_get_cpu_quota(struct container_config* c) {
1096 return c->cpu_cgparams.quota;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001097}
1098
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001099int container_config_get_cpu_period(struct container_config* c) {
1100 return c->cpu_cgparams.period;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001101}
1102
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001103int container_config_get_cpu_rt_runtime(struct container_config* c) {
1104 return c->cpu_cgparams.rt_runtime;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001105}
1106
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001107int container_config_get_cpu_rt_period(struct container_config* c) {
1108 return c->cpu_cgparams.rt_period;
Chinyue Chen4f3fd682016-07-01 14:11:42 +08001109}
1110
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001111int container_config_set_cgroup_parent(struct container_config* c,
1112 const char* parent,
1113 uid_t cgroup_owner,
1114 gid_t cgroup_group) {
1115 c->cgroup_owner = cgroup_owner;
1116 c->cgroup_group = cgroup_group;
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001117 c->cgroup_parent = base::FilePath(parent);
1118 return 0;
Dylan Reid9e724af2016-07-21 09:58:07 -07001119}
1120
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001121const char* container_config_get_cgroup_parent(struct container_config* c) {
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001122 return c->cgroup_parent.value().c_str();
Dylan Reid9e724af2016-07-21 09:58:07 -07001123}
1124
Stephen Barber771653f2017-10-04 23:48:57 -07001125int container_config_namespaces(struct container_config* c,
1126 const char** namespaces,
1127 size_t num_ns) {
1128 if (num_ns < 1)
1129 return -EINVAL;
1130 c->namespaces.clear();
1131 for (size_t i = 0; i < num_ns; ++i)
1132 c->namespaces.emplace(namespaces[i]);
1133 return 0;
Keshav Santhanam1b6bf672016-08-10 18:35:12 -07001134}
1135
Stephen Barber771653f2017-10-04 23:48:57 -07001136size_t container_config_get_num_namespaces(const struct container_config* c) {
1137 return c->namespaces.size();
1138}
1139
1140bool container_config_has_namespace(const struct container_config* c,
1141 const char* ns) {
1142 return c->namespaces.find(ns) != c->namespaces.end();
Keshav Santhanam1b6bf672016-08-10 18:35:12 -07001143}
1144
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001145void container_config_keep_fds_open(struct container_config* c) {
yusukesf125f332017-12-08 13:45:15 -08001146 c->keep_fds_open = true;
Dylan Reidc4335842016-11-11 10:24:52 -08001147}
1148
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001149void container_config_set_capmask(struct container_config* c,
1150 uint64_t capmask,
1151 int ambient) {
yusukesf125f332017-12-08 13:45:15 -08001152 c->use_capmask = true;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001153 c->capmask = capmask;
1154 c->use_capmask_ambient = ambient;
Luis Hector Chavezff5978f2017-06-27 12:52:58 -07001155}
1156
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001157void container_config_set_securebits_skip_mask(struct container_config* c,
1158 uint64_t securebits_skip_mask) {
1159 c->securebits_skip_mask = securebits_skip_mask;
Luis Hector Chavezcd44ba72017-06-30 13:01:38 -07001160}
1161
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001162void container_config_set_run_as_init(struct container_config* c,
1163 int run_as_init) {
1164 c->do_init = !run_as_init;
Luis Hector Chavezdac65c32017-07-21 10:30:23 -07001165}
1166
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001167int container_config_set_selinux_context(struct container_config* c,
1168 const char* context) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001169 if (!context) {
1170 errno = EINVAL;
1171 return -1;
1172 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001173 c->selinux_context = context;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001174 return 0;
Luis Hector Chavez15e8e672017-07-20 15:13:27 -07001175}
1176
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001177void container_config_set_pre_execve_hook(struct container_config* c,
1178 int (*hook)(void*),
1179 void* payload) {
1180 c->pre_start_hook = hook;
1181 c->pre_start_hook_payload = payload;
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001182}
1183
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001184void container_config_add_hook(struct container_config* c,
1185 minijail_hook_event_t event,
1186 libcontainer::HookCallback callback) {
1187 auto it = c->hooks.insert(
1188 std::make_pair(event, std::vector<libcontainer::HookCallback>()));
1189 it.first->second.emplace_back(std::move(callback));
1190}
1191
Luis Hector Chaveze03926a2017-09-28 17:28:49 -07001192int container_config_add_hook(struct container_config* c,
1193 minijail_hook_event_t event,
1194 const char* filename,
1195 const char** argv,
1196 size_t num_args,
1197 int* pstdin_fd,
1198 int* pstdout_fd,
1199 int* pstderr_fd) {
1200 std::vector<std::string> args;
1201 args.reserve(num_args);
1202 for (size_t i = 0; i < num_args; ++i)
1203 args.emplace_back(argv[i]);
1204
1205 // First element of the array belongs to the parent and the second one belongs
1206 // to the child.
1207 base::ScopedFD stdin_fds[2], stdout_fds[2], stderr_fds[2];
1208 if (pstdin_fd) {
1209 if (!libcontainer::Pipe2(&stdin_fds[1], &stdin_fds[0], 0))
1210 return -1;
1211 }
1212 if (pstdout_fd) {
1213 if (!libcontainer::Pipe2(&stdout_fds[0], &stdout_fds[0], 0))
1214 return -1;
1215 }
1216 if (pstderr_fd) {
1217 if (!libcontainer::Pipe2(&stderr_fds[0], &stderr_fds[0], 0))
1218 return -1;
1219 }
1220
1221 // After this point the call has been successful, so we can now commit to
1222 // whatever pipes we have opened.
1223 if (pstdin_fd) {
1224 *pstdin_fd = stdin_fds[0].release();
1225 c->inherited_fds.emplace_back(stdin_fds[1].get());
1226 }
1227 if (pstdout_fd) {
1228 *pstdout_fd = stdout_fds[0].release();
1229 c->inherited_fds.emplace_back(stdout_fds[1].get());
1230 }
1231 if (pstderr_fd) {
1232 *pstderr_fd = stderr_fds[0].release();
1233 c->inherited_fds.emplace_back(stderr_fds[1].get());
1234 }
1235 container_config_add_hook(
1236 c, event,
1237 libcontainer::CreateExecveCallback(
1238 base::FilePath(filename), args, std::move(stdin_fds[1]),
1239 std::move(stdout_fds[1]), std::move(stderr_fds[1])));
1240 return 0;
1241}
1242
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001243int container_config_inherit_fds(struct container_config* c,
1244 int* inherited_fds,
1245 size_t inherited_fd_count) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001246 if (!c->inherited_fds.empty()) {
1247 errno = EINVAL;
1248 return -1;
1249 }
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001250 for (size_t i = 0; i < inherited_fd_count; ++i)
1251 c->inherited_fds.emplace_back(inherited_fds[i]);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001252 return 0;
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001253}
1254
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001255struct container* container_new(const char* name, const char* rundir) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001256 struct container* c = new (std::nothrow) container();
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001257 if (!c)
1258 return nullptr;
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001259 c->rundir = base::FilePath(rundir);
1260 c->name = name;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001261 return c;
Dylan Reid837c74a2016-01-22 17:25:21 -08001262}
1263
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001264void container_destroy(struct container* c) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001265 delete c;
Dylan Reid837c74a2016-01-22 17:25:21 -08001266}
1267
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001268int container_start(struct container* c,
1269 const struct container_config* config) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001270 if (!c) {
1271 errno = EINVAL;
1272 return -1;
1273 }
1274 if (!config) {
1275 errno = EINVAL;
1276 return -1;
1277 }
1278 if (config->program_argv.empty()) {
1279 errno = EINVAL;
1280 return -1;
1281 }
Dylan Reide040c6b2016-05-02 18:49:02 -07001282
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001283 // This will run in all the error cases.
1284 base::ScopedClosureRunner teardown(
Luis Hector Chavez15d0d1a2017-10-12 09:30:19 -07001285 base::Bind(&CancelContainerStart, base::Unretained(c)));
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001286
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001287 if (!config->config_root.empty())
1288 c->config_root = config->config_root;
1289 if (!config->premounted_runfs.empty()) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001290 c->runfs.clear();
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001291 c->runfsroot = config->premounted_runfs;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001292 } else {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001293 if (!MountRunfs(c, config))
1294 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001295 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001296
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001297 c->jail.reset(minijail_new());
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001298 if (!c->jail) {
1299 errno = ENOMEM;
1300 return -1;
1301 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001302
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001303 if (!DoContainerMounts(c, config))
1304 return -1;
Dylan Reid837c74a2016-01-22 17:25:21 -08001305
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001306 int cgroup_uid;
1307 if (!GetUsernsOutsideId(config->uid_map, config->cgroup_owner, &cgroup_uid))
1308 return -1;
1309 int cgroup_gid;
1310 if (!GetUsernsOutsideId(config->gid_map, config->cgroup_group, &cgroup_gid))
1311 return -1;
Stephen Barber1a398c72017-01-23 12:39:44 -08001312
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -07001313 c->cgroup = libcontainer::Cgroup::Create(c->name,
1314 base::FilePath("/sys/fs/cgroup"),
1315 config->cgroup_parent,
1316 cgroup_uid,
1317 cgroup_gid);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001318 if (!c->cgroup)
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001319 return -1;
Dylan Reida9966422016-07-21 10:11:34 -07001320
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001321 // Must be root to modify device cgroup or mknod.
1322 std::map<minijail_hook_event_t, std::vector<libcontainer::HookCallback>>
1323 hook_callbacks;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001324 if (getuid() == 0) {
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001325 if (!config->devices.empty()) {
1326 // Create the devices in the mount namespace.
1327 auto it = hook_callbacks.insert(
1328 std::make_pair(MINIJAIL_HOOK_EVENT_PRE_CHROOT,
1329 std::vector<libcontainer::HookCallback>()));
1330 it.first->second.emplace_back(
1331 libcontainer::AdaptCallbackToRunInNamespaces(
1332 base::Bind(&CreateDeviceNodes, base::Unretained(c),
1333 base::Unretained(config)),
1334 {CLONE_NEWNS}));
1335 }
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001336 if (!DeviceSetup(c, config))
1337 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001338 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001339
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001340 /* Setup CPU cgroup params. */
1341 if (config->cpu_cgparams.shares) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001342 if (!c->cgroup->SetCpuShares(config->cpu_cgparams.shares))
1343 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001344 }
1345 if (config->cpu_cgparams.period) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001346 if (!c->cgroup->SetCpuQuota(config->cpu_cgparams.quota))
1347 return -1;
1348 if (!c->cgroup->SetCpuPeriod(config->cpu_cgparams.period))
1349 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001350 }
1351 if (config->cpu_cgparams.rt_period) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001352 if (!c->cgroup->SetCpuRtRuntime(config->cpu_cgparams.rt_runtime))
1353 return -1;
1354 if (!c->cgroup->SetCpuRtPeriod(config->cpu_cgparams.rt_period))
1355 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001356 }
Chinyue Chenfac909e2016-06-24 14:17:42 +08001357
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001358 /* Setup and start the container with libminijail. */
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001359 if (!config->pid_file_path.empty())
1360 c->pid_file_path = config->pid_file_path;
1361 else if (!c->runfs.empty())
1362 c->pid_file_path = c->runfs.Append("container.pid");
Keshav Santhanam0e4c3282016-07-14 10:25:16 -07001363
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001364 if (!c->pid_file_path.empty())
1365 minijail_write_pid_file(c->jail.get(), c->pid_file_path.value().c_str());
1366 minijail_reset_signal_mask(c->jail.get());
Luis Hector Chavezcd9a6b62018-04-04 08:39:44 -07001367 minijail_reset_signal_handlers(c->jail.get());
Dylan Reid837c74a2016-01-22 17:25:21 -08001368
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001369 /* Setup container namespaces. */
Stephen Barber771653f2017-10-04 23:48:57 -07001370 if (container_config_has_namespace(config, "ipc"))
1371 minijail_namespace_ipc(c->jail.get());
1372 if (container_config_has_namespace(config, "mount"))
1373 minijail_namespace_vfs(c->jail.get());
1374 if (container_config_has_namespace(config, "network"))
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001375 minijail_namespace_net(c->jail.get());
Stephen Barber771653f2017-10-04 23:48:57 -07001376 if (container_config_has_namespace(config, "pid"))
1377 minijail_namespace_pids(c->jail.get());
1378
1379 if (container_config_has_namespace(config, "user")) {
1380 minijail_namespace_user(c->jail.get());
1381 if (minijail_uidmap(c->jail.get(), config->uid_map.c_str()) != 0)
1382 return -1;
1383 if (minijail_gidmap(c->jail.get(), config->gid_map.c_str()) != 0)
1384 return -1;
1385 }
1386
1387 if (container_config_has_namespace(config, "cgroup"))
1388 minijail_namespace_cgroups(c->jail.get());
1389
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001390 if (getuid() != 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001391 minijail_namespace_user_disable_setgroups(c->jail.get());
Dylan Reid837c74a2016-01-22 17:25:21 -08001392
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001393 /* Set the UID/GID inside the container if not 0. */
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001394 if (!GetUsernsOutsideId(config->uid_map, config->uid, nullptr))
1395 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001396 else if (config->uid > 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001397 minijail_change_uid(c->jail.get(), config->uid);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001398 if (!GetUsernsOutsideId(config->gid_map, config->gid, nullptr))
1399 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001400 else if (config->gid > 0)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001401 minijail_change_gid(c->jail.get(), config->gid);
Keshav Santhanam36485ff2016-08-02 16:21:02 -07001402
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001403 if (minijail_enter_pivot_root(c->jail.get(), c->runfsroot.value().c_str()) !=
1404 0) {
1405 return -1;
1406 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001407
Luis Hector Chavez76ae9ac2017-09-20 21:13:08 -07001408 // Add the cgroups configured above.
1409 for (int32_t i = 0; i < libcontainer::Cgroup::Type::NUM_TYPES; i++) {
1410 if (c->cgroup->has_tasks_path(i)) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001411 if (minijail_add_to_cgroup(
1412 c->jail.get(), c->cgroup->tasks_path(i).value().c_str()) != 0) {
1413 return -1;
1414 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001415 }
1416 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001417
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001418 if (!config->alt_syscall_table.empty())
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001419 minijail_use_alt_syscall(c->jail.get(), config->alt_syscall_table.c_str());
Dylan Reid837c74a2016-01-22 17:25:21 -08001420
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001421 for (int i = 0; i < config->num_rlimits; i++) {
Luis Hector Chaveze1062e82017-09-18 09:57:37 -07001422 const Rlimit& lim = config->rlimits[i];
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001423 if (minijail_rlimit(c->jail.get(), lim.type, lim.cur, lim.max) != 0)
1424 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001425 }
Dylan Reid93fa4602017-06-06 13:39:31 -07001426
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001427 if (!config->selinux_context.empty()) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001428 if (minijail_add_hook(c->jail.get(), &Setexeccon,
1429 const_cast<char*>(config->selinux_context.c_str()),
1430 MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) {
1431 return -1;
1432 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001433 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001434
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001435 if (config->pre_start_hook) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001436 if (minijail_add_hook(c->jail.get(), config->pre_start_hook,
1437 config->pre_start_hook_payload,
1438 MINIJAIL_HOOK_EVENT_PRE_EXECVE) != 0) {
1439 return -1;
1440 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001441 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001442
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001443 // Now that all pre-requisite hooks are installed, copy the ones in the
1444 // container_config object in the correct order.
1445 for (const auto& config_hook : config->hooks) {
1446 auto it = hook_callbacks.insert(std::make_pair(
1447 config_hook.first, std::vector<libcontainer::HookCallback>()));
1448 it.first->second.insert(it.first->second.end(), config_hook.second.begin(),
1449 config_hook.second.end());
1450 }
1451
1452 c->hook_states.clear();
1453 // Reserve enough memory to hold all the hooks, so that their addresses do not
1454 // get invalidated by reallocation.
1455 c->hook_states.reserve(MINIJAIL_HOOK_EVENT_MAX);
1456 for (minijail_hook_event_t event : {MINIJAIL_HOOK_EVENT_PRE_CHROOT,
1457 MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS,
1458 MINIJAIL_HOOK_EVENT_PRE_EXECVE}) {
1459 const auto& it = hook_callbacks.find(event);
1460 if (it == hook_callbacks.end())
1461 continue;
1462 c->hook_states.emplace_back(
1463 std::make_pair(libcontainer::HookState(), it->second));
1464 if (!c->hook_states.back().first.InstallHook(c->jail.get(), event))
1465 return -1;
1466 }
1467
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001468 for (int fd : config->inherited_fds) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001469 if (minijail_preserve_fd(c->jail.get(), fd, fd) != 0)
1470 return -1;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001471 }
Luis Hector Chavezf8e8f4c2017-08-01 01:09:39 -07001472
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001473 /* TODO(dgreid) - remove this once shared mounts are cleaned up. */
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001474 minijail_skip_remount_private(c->jail.get());
Dylan Reid3da683b2016-04-05 03:35:35 -07001475
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001476 if (!config->keep_fds_open)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001477 minijail_close_open_fds(c->jail.get());
Luis Hector Chaveze18e7d42016-10-12 07:35:32 -07001478
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001479 if (config->use_capmask) {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001480 minijail_use_caps(c->jail.get(), config->capmask);
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001481 if (config->use_capmask_ambient)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001482 minijail_set_ambient_caps(c->jail.get());
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001483 if (config->securebits_skip_mask) {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001484 minijail_skip_setting_securebits(c->jail.get(),
1485 config->securebits_skip_mask);
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001486 }
1487 }
Luis Hector Chavezff5978f2017-06-27 12:52:58 -07001488
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001489 if (!config->do_init)
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001490 minijail_run_as_init(c->jail.get());
Luis Hector Chavezdac65c32017-07-21 10:30:23 -07001491
Luis Hector Chavez9cde12a2017-09-18 10:53:38 -07001492 std::vector<char*> argv_cstr;
1493 argv_cstr.reserve(config->program_argv.size() + 1);
1494 for (const auto& arg : config->program_argv)
1495 argv_cstr.emplace_back(const_cast<char*>(arg.c_str()));
1496 argv_cstr.emplace_back(nullptr);
1497
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001498 if (minijail_run_pid_pipes_no_preload(c->jail.get(), argv_cstr[0],
1499 argv_cstr.data(), &c->init_pid, nullptr,
1500 nullptr, nullptr) != 0) {
1501 return -1;
1502 }
Dylan Reid837c74a2016-01-22 17:25:21 -08001503
Luis Hector Chavez644d2042017-09-19 18:56:44 -07001504 // |hook_states| is already sorted in the correct order.
1505 for (auto& hook_state : c->hook_states) {
1506 if (!hook_state.first.WaitForHookAndRun(hook_state.second, c->init_pid))
1507 return -1;
1508 }
1509
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001510 // The container has started successfully, no need to tear it down anymore.
1511 ignore_result(teardown.Release());
1512 return 0;
Dylan Reid837c74a2016-01-22 17:25:21 -08001513}
1514
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001515const char* container_root(struct container* c) {
Luis Hector Chavez5381d002017-09-16 12:54:24 -07001516 return c->runfs.value().c_str();
Dylan Reid837c74a2016-01-22 17:25:21 -08001517}
1518
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001519int container_pid(struct container* c) {
1520 return c->init_pid;
Dylan Reid837c74a2016-01-22 17:25:21 -08001521}
1522
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001523int container_wait(struct container* c) {
1524 int rc;
Dylan Reidcf745c52016-04-22 10:18:03 -07001525
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001526 do {
Luis Hector Chavez626f5c82017-09-18 11:19:32 -07001527 rc = minijail_wait(c->jail.get());
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001528 } while (rc == -EINTR);
Dylan Reidcf745c52016-04-22 10:18:03 -07001529
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001530 // If the process had already been reaped, still perform teardown.
1531 if (rc == -ECHILD || rc >= 0) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001532 if (!ContainerTeardown(c))
1533 rc = -errno;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001534 }
1535 return rc;
Dylan Reid837c74a2016-01-22 17:25:21 -08001536}
1537
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001538int container_kill(struct container* c) {
Luis Hector Chavez1f7e60c2017-09-27 22:03:48 -07001539 if (kill(c->init_pid, SIGKILL) != 0 && errno != ESRCH) {
Luis Hector Chavezdc61f8d2017-10-02 11:12:46 -07001540 PLOG(ERROR) << "Failed to kill " << c->init_pid;
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001541 return -errno;
Luis Hector Chavez835d39e2017-09-19 15:16:31 -07001542 }
Luis Hector Chavez31735bc2017-09-15 08:17:10 -07001543 return container_wait(c);
Dylan Reid837c74a2016-01-22 17:25:21 -08001544}
yusukesbbc37a72017-11-21 09:51:54 -08001545
yusukes32622542018-01-05 18:59:52 -08001546char* container_config_dump(struct container_config* c, int sort_vectors) {
yusukesbbc37a72017-11-21 09:51:54 -08001547 std::stringstream out;
yusukes32622542018-01-05 18:59:52 -08001548 DumpConfig(&out, c, sort_vectors);
yusukesbbc37a72017-11-21 09:51:54 -08001549 return strdup(out.str().c_str());
1550}