Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 1 | // Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "libcontainer/libcontainer_util.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #if USE_device_mapper |
| 10 | #include <libdevmapper.h> |
| 11 | #endif |
| 12 | #include <linux/loop.h> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 13 | #include <sched.h> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 14 | #include <sys/mount.h> |
| 15 | #include <sys/stat.h> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 16 | #include <sys/wait.h> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 17 | |
| 18 | #include <memory> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 19 | #include <utility> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | #include <base/bind.h> |
| 23 | #include <base/bind_helpers.h> |
| 24 | #include <base/callback_helpers.h> |
| 25 | #include <base/files/scoped_file.h> |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 26 | #include <base/logging.h> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 27 | #include <base/macros.h> |
| 28 | #include <base/posix/eintr_wrapper.h> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 29 | #include <base/strings/string_number_conversions.h> |
| 30 | #include <base/strings/string_split.h> |
| 31 | #include <base/strings/string_util.h> |
| 32 | #include <base/strings/stringprintf.h> |
| 33 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 34 | // New cgroup namespace might not be in linux-headers yet. |
| 35 | #ifndef CLONE_NEWCGROUP |
| 36 | #define CLONE_NEWCGROUP 0x02000000 |
| 37 | #endif |
| 38 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 39 | namespace libcontainer { |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | constexpr base::FilePath::CharType kLoopdevCtlPath[] = |
| 44 | FILE_PATH_LITERAL("/dev/loop-control"); |
| 45 | #if USE_device_mapper |
| 46 | constexpr base::FilePath::CharType kDevMapperPath[] = |
| 47 | FILE_PATH_LITERAL("/dev/mapper/"); |
| 48 | #endif |
| 49 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 50 | // Gets the namespace name for |nstype|. |
| 51 | std::string GetNamespaceNameForType(int nstype) { |
| 52 | switch (nstype) { |
| 53 | case CLONE_NEWCGROUP: |
| 54 | return "cgroup"; |
| 55 | case CLONE_NEWIPC: |
| 56 | return "ipc"; |
| 57 | case CLONE_NEWNET: |
| 58 | return "net"; |
| 59 | case CLONE_NEWNS: |
| 60 | return "mnt"; |
| 61 | case CLONE_NEWPID: |
| 62 | return "pid"; |
| 63 | case CLONE_NEWUSER: |
| 64 | return "user"; |
| 65 | case CLONE_NEWUTS: |
| 66 | return "uts"; |
| 67 | } |
| 68 | return std::string(); |
| 69 | } |
| 70 | |
| 71 | // Helper function that runs |callback| in all the namespaces identified by |
| 72 | // |nstypes|. |
| 73 | bool RunInNamespacesHelper(HookCallback callback, |
| 74 | std::vector<int> nstypes, |
| 75 | pid_t container_pid) { |
| 76 | pid_t child = fork(); |
| 77 | if (child < 0) { |
| 78 | PLOG_PRESERVE(ERROR) << "Failed to fork()"; |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (child == 0) { |
| 83 | for (const int nstype : nstypes) { |
| 84 | std::string nstype_name = GetNamespaceNameForType(nstype); |
| 85 | if (nstype_name.empty()) { |
| 86 | LOG(ERROR) << "Invalid namespace type " << nstype; |
| 87 | _exit(-1); |
| 88 | } |
| 89 | base::FilePath ns_path = base::FilePath(base::StringPrintf( |
| 90 | "/proc/%d/ns/%s", container_pid, nstype_name.c_str())); |
| 91 | base::ScopedFD ns_fd(open(ns_path.value().c_str(), O_RDONLY)); |
| 92 | if (!ns_fd.is_valid()) { |
| 93 | PLOG_PRESERVE(ERROR) << "Failed to open " << ns_path.value(); |
| 94 | _exit(-1); |
| 95 | } |
| 96 | if (setns(ns_fd.get(), nstype)) { |
| 97 | PLOG_PRESERVE(ERROR) << "Failed to enter PID " << container_pid << "'s " |
| 98 | << nstype_name << " namespace"; |
| 99 | _exit(-1); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Preserve normal POSIX semantics of calling exit(2) with 0 for success and |
| 104 | // non-zero for failure. |
| 105 | _exit(callback.Run(container_pid) ? 0 : 1); |
| 106 | } |
| 107 | |
| 108 | int status; |
| 109 | if (HANDLE_EINTR(waitpid(child, &status, 0)) < 0) { |
| 110 | PLOG_PRESERVE(ERROR) << "Failed to wait for callback"; |
| 111 | return false; |
| 112 | } |
| 113 | if (!WIFEXITED(status)) { |
| 114 | LOG(ERROR) << "Callback terminated abnormally: " << std::hex << status; |
| 115 | return false; |
| 116 | } |
| 117 | return static_cast<int8_t>(WEXITSTATUS(status)) == 0; |
| 118 | } |
| 119 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 120 | } // namespace |
| 121 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 122 | SaveErrno::SaveErrno() : saved_errno_(errno) {} |
| 123 | |
| 124 | SaveErrno::~SaveErrno() { |
| 125 | errno = saved_errno_; |
| 126 | } |
| 127 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 128 | WaitablePipe::WaitablePipe() { |
| 129 | if (pipe2(pipe_fds, O_CLOEXEC) == -1) |
| 130 | PLOG(FATAL) << "Failed to create pipe"; |
| 131 | } |
| 132 | |
| 133 | WaitablePipe::~WaitablePipe() { |
| 134 | if (pipe_fds[0] != -1) |
| 135 | close(pipe_fds[0]); |
| 136 | if (pipe_fds[1] != -1) |
| 137 | close(pipe_fds[1]); |
| 138 | } |
| 139 | |
| 140 | WaitablePipe::WaitablePipe(WaitablePipe&& other) { |
| 141 | pipe_fds[0] = pipe_fds[1] = -1; |
| 142 | std::swap(pipe_fds, other.pipe_fds); |
| 143 | } |
| 144 | |
| 145 | void WaitablePipe::Wait() { |
| 146 | char buf; |
| 147 | |
| 148 | close(pipe_fds[1]); |
| 149 | HANDLE_EINTR(read(pipe_fds[0], &buf, sizeof(buf))); |
| 150 | close(pipe_fds[0]); |
| 151 | |
| 152 | pipe_fds[0] = pipe_fds[1] = -1; |
| 153 | } |
| 154 | |
| 155 | void WaitablePipe::Signal() { |
| 156 | close(pipe_fds[0]); |
| 157 | close(pipe_fds[1]); |
| 158 | |
| 159 | pipe_fds[0] = pipe_fds[1] = -1; |
| 160 | } |
| 161 | |
| 162 | HookState::HookState() = default; |
| 163 | HookState::~HookState() = default; |
| 164 | |
| 165 | HookState::HookState(HookState&& state) = default; |
| 166 | |
| 167 | bool HookState::InstallHook(struct minijail* j, minijail_hook_event_t event) { |
| 168 | if (installed_) { |
| 169 | LOG(ERROR) << "Failed to install hook: already installed"; |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // All these fds will be closed in WaitHook in the child process. |
| 174 | for (size_t i = 0; i < 2; ++i) { |
| 175 | if (minijail_preserve_fd( |
| 176 | j, reached_pipe_.pipe_fds[i], reached_pipe_.pipe_fds[i]) < 0) { |
| 177 | LOG(ERROR) << "Failed to preserve reached pipe FDs to install hook"; |
| 178 | return false; |
| 179 | } |
| 180 | if (minijail_preserve_fd( |
| 181 | j, ready_pipe_.pipe_fds[i], ready_pipe_.pipe_fds[i]) < 0) { |
| 182 | LOG(ERROR) << "Failed to preserve ready pipe FDs to install hook"; |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (minijail_add_hook(j, &HookState::WaitHook, this, event) < 0) { |
| 188 | LOG(ERROR) << "Failed to add hook"; |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | installed_ = true; |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | bool HookState::WaitForHookAndRun(const std::vector<HookCallback>& callbacks, |
| 197 | pid_t container_pid) { |
| 198 | if (!installed_) { |
| 199 | LOG(ERROR) << "Failed to wait for hook: not installed"; |
| 200 | return false; |
| 201 | } |
| 202 | reached_pipe_.Wait(); |
| 203 | base::ScopedClosureRunner teardown( |
| 204 | base::Bind(&WaitablePipe::Signal, base::Unretained(&ready_pipe_))); |
| 205 | |
| 206 | for (auto& callback : callbacks) { |
| 207 | bool success = callback.Run(container_pid); |
| 208 | if (!success) |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | // static |
| 215 | int HookState::WaitHook(void* payload) { |
| 216 | HookState* self = reinterpret_cast<HookState*>(payload); |
| 217 | |
| 218 | self->reached_pipe_.Signal(); |
| 219 | self->ready_pipe_.Wait(); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 224 | int GetUsernsOutsideId(const std::string& map, int id) { |
| 225 | if (map.empty()) |
| 226 | return id; |
| 227 | |
| 228 | std::string map_copy = map; |
| 229 | base::StringPiece map_piece(map_copy); |
| 230 | |
| 231 | for (const auto& mapping : base::SplitStringPiece( |
| 232 | map_piece, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL)) { |
| 233 | std::vector<base::StringPiece> tokens = base::SplitStringPiece( |
| 234 | mapping, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 235 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 236 | if (tokens.size() != 3) { |
| 237 | LOG(ERROR) << "Malformed ugid mapping: '" << mapping << "'"; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 238 | return -EINVAL; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 239 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 240 | |
| 241 | uint32_t inside, outside, length; |
| 242 | if (!base::StringToUint(tokens[0], &inside) || |
| 243 | !base::StringToUint(tokens[1], &outside) || |
| 244 | !base::StringToUint(tokens[2], &length)) { |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 245 | LOG(ERROR) << "Malformed ugid mapping: '" << mapping << "'"; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | if (id >= inside && id <= (inside + length)) { |
| 250 | return (id - inside) + outside; |
| 251 | } |
| 252 | } |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 253 | VLOG(1) << "ugid " << id << " not found in mapping"; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 254 | |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | int MakeDir(const base::FilePath& path, int uid, int gid, int mode) { |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 259 | if (mkdir(path.value().c_str(), mode)) { |
| 260 | PLOG_PRESERVE(ERROR) << "Failed to mkdir " << path.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 261 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 262 | } |
| 263 | if (chmod(path.value().c_str(), mode)) { |
| 264 | PLOG_PRESERVE(ERROR) << "Failed to chmod " << path.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 265 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 266 | } |
| 267 | if (chown(path.value().c_str(), uid, gid)) { |
| 268 | PLOG_PRESERVE(ERROR) << "Failed to chown " << path.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 269 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 270 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int TouchFile(const base::FilePath& path, int uid, int gid, int mode) { |
| 275 | base::ScopedFD fd(open(path.value().c_str(), O_RDWR | O_CREAT, mode)); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 276 | if (!fd.is_valid()) { |
| 277 | PLOG_PRESERVE(ERROR) << "Failed to create " << path.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 278 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 279 | } |
| 280 | if (fchown(fd.get(), uid, gid)) { |
| 281 | PLOG_PRESERVE(ERROR) << "Failed to chown " << path.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 282 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 283 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | int LoopdevSetup(const base::FilePath& source, |
| 288 | base::FilePath* loopdev_path_out) { |
| 289 | base::ScopedFD source_fd(open(source.value().c_str(), O_RDONLY | O_CLOEXEC)); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 290 | if (!source_fd.is_valid()) { |
| 291 | PLOG_PRESERVE(ERROR) << "Failed to open " << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 292 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 293 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 294 | |
| 295 | base::ScopedFD control_fd( |
| 296 | open(kLoopdevCtlPath, O_RDWR | O_NOFOLLOW | O_CLOEXEC)); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 297 | if (!control_fd.is_valid()) { |
| 298 | PLOG_PRESERVE(ERROR) << "Failed to open " << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 299 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 300 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 301 | |
| 302 | while (true) { |
| 303 | int num = ioctl(control_fd.get(), LOOP_CTL_GET_FREE); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 304 | if (num < 0) { |
| 305 | PLOG_PRESERVE(ERROR) << "Failed to open " << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 306 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 307 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 308 | |
| 309 | base::FilePath loopdev_path(base::StringPrintf("/dev/loop%i", num)); |
| 310 | base::ScopedFD loop_fd( |
| 311 | open(loopdev_path.value().c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 312 | if (!loop_fd.is_valid()) { |
| 313 | PLOG_PRESERVE(ERROR) << "Failed to open " << loopdev_path.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 314 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 315 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 316 | |
| 317 | if (ioctl(loop_fd.get(), LOOP_SET_FD, source_fd.get()) == 0) { |
| 318 | *loopdev_path_out = loopdev_path; |
| 319 | break; |
| 320 | } |
| 321 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 322 | if (errno != EBUSY) { |
| 323 | PLOG_PRESERVE(ERROR) << "Failed to ioctl(LOOP_SET_FD) " |
| 324 | << loopdev_path.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 325 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 326 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int LoopdevDetach(const base::FilePath& loopdev) { |
| 333 | base::ScopedFD fd( |
| 334 | open(loopdev.value().c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 335 | if (!fd.is_valid()) { |
| 336 | PLOG_PRESERVE(ERROR) << "Failed to open " << loopdev.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 337 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 338 | } |
| 339 | if (ioctl(fd.get(), LOOP_CLR_FD) < 0) { |
| 340 | PLOG_PRESERVE(ERROR) << "Failed to ioctl(LOOP_CLR_FD) for " |
| 341 | << loopdev.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 342 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 343 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int DeviceMapperSetup(const base::FilePath& source, |
| 349 | const std::string& verity_cmdline, |
| 350 | base::FilePath* dm_path_out, |
| 351 | std::string* dm_name_out) { |
| 352 | #if USE_device_mapper |
| 353 | // Normalize the name into something unique-esque. |
| 354 | std::string dm_name = |
| 355 | base::StringPrintf("cros-containers-%s", source.value().c_str()); |
| 356 | base::ReplaceChars(dm_name, "/", "_", &dm_name); |
| 357 | |
| 358 | // Get the /dev path for the higher levels to mount. |
| 359 | base::FilePath dm_path = base::FilePath(kDevMapperPath).Append(dm_name); |
| 360 | |
| 361 | // Insert the source path in the verity command line. |
| 362 | std::string verity = verity_cmdline; |
| 363 | base::ReplaceSubstringsAfterOffset(&verity, 0, "@DEV@", source.value()); |
| 364 | |
| 365 | // Extract the first three parameters for dm-verity settings. |
| 366 | char ttype[20]; |
| 367 | unsigned long long start, size; |
| 368 | int n; |
| 369 | if (sscanf(verity.c_str(), "%llu %llu %10s %n", &start, &size, ttype, &n) != |
| 370 | 3) { |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 371 | PLOG_PRESERVE(ERROR) << "Malformed verity string " << verity; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 372 | return -errno; |
| 373 | } |
| 374 | |
| 375 | /* Finally create the device mapper. */ |
| 376 | std::unique_ptr<struct dm_task, decltype(&dm_task_destroy)> dmt( |
| 377 | dm_task_create(DM_DEVICE_CREATE), &dm_task_destroy); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 378 | if (dmt == nullptr) { |
| 379 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_create() for " << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 380 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 381 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 382 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 383 | if (!dm_task_set_name(dmt.get(), dm_name.c_str())) { |
| 384 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_set_name() for " |
| 385 | << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 386 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 387 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 388 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 389 | if (!dm_task_set_ro(dmt.get())) { |
| 390 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_set_ro() for " << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 391 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 392 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 393 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 394 | if (!dm_task_add_target(dmt.get(), start, size, ttype, verity.c_str() + n)) { |
| 395 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_add_target() for " |
| 396 | << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 397 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 398 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 399 | |
| 400 | uint32_t cookie = 0; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 401 | if (!dm_task_set_cookie(dmt.get(), &cookie, 0)) { |
| 402 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_set_cookie() for " |
| 403 | << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 404 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 405 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 406 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 407 | if (!dm_task_run(dmt.get())) { |
| 408 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_run() for " << source.value(); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 409 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 410 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 411 | |
| 412 | /* Make sure the node exists before we continue. */ |
| 413 | dm_udev_wait(cookie); |
| 414 | |
| 415 | *dm_path_out = dm_path; |
| 416 | *dm_name_out = dm_name; |
| 417 | #endif |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | // Tear down the device mapper target. |
| 422 | int DeviceMapperDetach(const std::string& dm_name) { |
| 423 | #if USE_device_mapper |
| 424 | struct dm_task* dmt = dm_task_create(DM_DEVICE_REMOVE); |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 425 | if (dmt == nullptr) { |
| 426 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_run() for " << dm_name; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 427 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 428 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 429 | |
| 430 | base::ScopedClosureRunner teardown( |
| 431 | base::Bind(base::IgnoreResult(&dm_task_destroy), base::Unretained(dmt))); |
| 432 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 433 | if (!dm_task_set_name(dmt, dm_name.c_str())) { |
| 434 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_set_name() for " << dm_name; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 435 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 436 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 437 | |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 438 | if (!dm_task_run(dmt)) { |
| 439 | PLOG_PRESERVE(ERROR) << "Failed to dm_task_run() for " << dm_name; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 440 | return -errno; |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 441 | } |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 442 | #endif |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | int MountExternal(const std::string& src, |
| 447 | const std::string& dest, |
| 448 | const std::string& type, |
| 449 | unsigned long flags, |
| 450 | const std::string& data) { |
| 451 | bool remount_ro = false; |
| 452 | |
| 453 | // R/O bind mounts have to be remounted since 'bind' and 'ro' can't both be |
| 454 | // specified in the original bind mount. Remount R/O after the initial mount. |
| 455 | if ((flags & MS_BIND) && (flags & MS_RDONLY)) { |
| 456 | remount_ro = true; |
| 457 | flags &= ~MS_RDONLY; |
| 458 | } |
| 459 | |
| 460 | if (mount(src.c_str(), |
| 461 | dest.c_str(), |
| 462 | type.c_str(), |
| 463 | flags, |
| 464 | data.empty() ? nullptr : data.c_str()) == -1) { |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 465 | PLOG_PRESERVE(ERROR) << "Failed to mount " << src << " to " << dest; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 466 | return -errno; |
| 467 | } |
| 468 | |
| 469 | if (remount_ro) { |
| 470 | flags |= MS_RDONLY; |
| 471 | if (mount(src.c_str(), |
| 472 | dest.c_str(), |
| 473 | nullptr, |
| 474 | flags | MS_REMOUNT, |
| 475 | data.empty() ? nullptr : data.c_str()) == -1) { |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 476 | PLOG_PRESERVE(ERROR) << "Failed to remount " << src << " to " << dest; |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 477 | return -errno; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame^] | 484 | HookCallback AdaptCallbackToRunInNamespaces(HookCallback callback, |
| 485 | std::vector<int> nstypes) { |
| 486 | return base::Bind(&RunInNamespacesHelper, |
| 487 | base::Passed(std::move(callback)), |
| 488 | base::Passed(std::move(nstypes))); |
| 489 | } |
| 490 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 491 | } // namespace libcontainer |