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 | #ifndef LIBCONTAINER_LIBCONTAINER_UTIL_H_ |
| 6 | #define LIBCONTAINER_LIBCONTAINER_UTIL_H_ |
| 7 | |
Luis Hector Chavez | 5cf71ed | 2018-05-07 14:45:43 -0700 | [diff] [blame] | 8 | #include <linux/loop.h> |
| 9 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 10 | #include <string> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 11 | #include <vector> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 12 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 13 | #include <base/callback_forward.h> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 14 | #include <base/files/file_path.h> |
Luis Hector Chavez | e03926a | 2017-09-28 17:28:49 -0700 | [diff] [blame] | 15 | #include <base/files/scoped_file.h> |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 16 | #include <base/logging.h> |
| 17 | #include <base/macros.h> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 18 | #include <libminijail.h> |
| 19 | |
| 20 | #include "libcontainer/config.h" |
| 21 | #include "libcontainer/libcontainer.h" |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 22 | |
| 23 | namespace libcontainer { |
| 24 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 25 | // WaitablePipe provides a way for one process to wait on another. This only |
| 26 | // uses the read(2) and close(2) syscalls, so it can work even in a restrictive |
| 27 | // environment. Each process must call only one of Wait() and Signal() exactly |
| 28 | // once. |
| 29 | struct WaitablePipe { |
| 30 | WaitablePipe(); |
| 31 | ~WaitablePipe(); |
| 32 | |
| 33 | WaitablePipe(WaitablePipe&&); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 34 | WaitablePipe(const WaitablePipe&) = delete; |
| 35 | WaitablePipe& operator=(const WaitablePipe&) = delete; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 36 | |
| 37 | // Waits for Signal() to be called. |
| 38 | void Wait(); |
| 39 | |
| 40 | // Notifies the process that called Wait() to continue running. |
| 41 | void Signal(); |
| 42 | |
| 43 | int pipe_fds[2]; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | // HookState holds two WaitablePipes so that the container can wait for its |
| 47 | // parent to run prestart hooks just prior to calling execve(2). |
| 48 | class HookState { |
| 49 | public: |
| 50 | HookState(); |
| 51 | ~HookState(); |
| 52 | |
| 53 | HookState(HookState&& state); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 54 | HookState(const HookState&) = delete; |
| 55 | HookState& operator=(const HookState&) = delete; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 56 | |
| 57 | // Initializes this HookState so that WaitForHookAndRun() can be invoked and |
| 58 | // waited upon when |j| reaches |event|. Returns true on success. |
| 59 | bool InstallHook(minijail* j, minijail_hook_event_t event); |
| 60 | |
| 61 | // Waits for the event specified in InstallHook() and invokes |callbacks| in |
| 62 | // the caller process. Returns true if all callbacks succeeded. |
| 63 | bool WaitForHookAndRun(const std::vector<HookCallback>& callbacks, |
| 64 | pid_t container_pid); |
| 65 | |
| 66 | private: |
| 67 | // A function that can be passed to minijail_add_hook() that blocks the |
| 68 | // process in the container until the parent has finished running whatever |
| 69 | // operations are needed outside the container. This is not expected to be |
| 70 | // called directly. |
| 71 | static int WaitHook(void* payload); |
| 72 | |
| 73 | bool installed_ = false; |
| 74 | WaitablePipe reached_pipe_; |
| 75 | WaitablePipe ready_pipe_; |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Luis Hector Chavez | 5cf71ed | 2018-05-07 14:45:43 -0700 | [diff] [blame] | 78 | // Loopdev represents an active loopback device. |
| 79 | struct Loopdev { |
| 80 | // The path of the loopback device. e.g. /dev/loop1 |
| 81 | base::FilePath path; |
| 82 | |
| 83 | // An open file descriptor for the loopback device. Has the autoclear flag, |
| 84 | // such that the kernel will automatically remove it once all references to it |
| 85 | // are closed. |
| 86 | base::ScopedFD fd; |
| 87 | |
| 88 | // Information about the loop device. |
| 89 | struct loop_info64 info; |
| 90 | }; |
| 91 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 92 | // Given a uid/gid map of "inside1 outside1 length1, ...", and an id inside of |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 93 | // the user namespace, populate |id_out|. Returns true on success. |
| 94 | bool GetUsernsOutsideId(const std::string& map, int id, int* id_out); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 95 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 96 | bool MakeDir(const base::FilePath& path, int uid, int gid, int mode); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 97 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 98 | bool TouchFile(const base::FilePath& path, int uid, int gid, int mode); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 99 | |
| 100 | // Find a free loop device and attach it. |
Luis Hector Chavez | 5cf71ed | 2018-05-07 14:45:43 -0700 | [diff] [blame] | 101 | bool LoopdevSetup(const base::FilePath& source, Loopdev* loopdev_out); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 102 | |
| 103 | // Detach the specified loop device. |
Luis Hector Chavez | 5cf71ed | 2018-05-07 14:45:43 -0700 | [diff] [blame] | 104 | bool LoopdevDetach(Loopdev* loopdev); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 105 | |
| 106 | // Create a new device mapper target for the source. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 107 | bool DeviceMapperSetup(const base::FilePath& source, |
| 108 | const std::string& verity_cmdline, |
| 109 | base::FilePath* dm_path_out, |
| 110 | std::string* dm_name_out); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 111 | |
| 112 | // Tear down the device mapper target. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 113 | bool DeviceMapperDetach(const std::string& dm_name); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 114 | |
| 115 | // Match mount_one in minijail, mount one mountpoint with |
| 116 | // consideration for combination of MS_BIND/MS_RDONLY flag. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 117 | bool MountExternal(const std::string& src, |
| 118 | const std::string& dest, |
| 119 | const std::string& type, |
| 120 | unsigned long flags, |
| 121 | const std::string& data); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 122 | |
Luis Hector Chavez | e03926a | 2017-09-28 17:28:49 -0700 | [diff] [blame] | 123 | // Creates a pipe using pipe2(2) and returns both ends as base::ScopedFDs. |
| 124 | bool Pipe2(base::ScopedFD* read_pipe, base::ScopedFD* write_pipe, int flags); |
| 125 | |
| 126 | // Creates a callback that will fork(2)+execve(2) the program specified by args. |
| 127 | HookCallback CreateExecveCallback(base::FilePath filename, |
| 128 | std::vector<std::string> args, |
| 129 | base::ScopedFD stdin_fd, |
| 130 | base::ScopedFD stdout_fd, |
| 131 | base::ScopedFD stderr_fd); |
| 132 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 133 | // Wraps a callback to be run in a subset of the container's namespaces. |
| 134 | HookCallback AdaptCallbackToRunInNamespaces(HookCallback callback, |
| 135 | std::vector<int> nstypes); |
| 136 | |
Luis Hector Chavez | 92278e8 | 2017-10-16 11:30:27 -0700 | [diff] [blame] | 137 | // Similar to base::CreateDirectory, but allows specifying the created |
| 138 | // directories' mode and owner. |
| 139 | bool CreateDirectoryOwnedBy(const base::FilePath& full_path, |
| 140 | mode_t mode, |
| 141 | uid_t uid, |
| 142 | gid_t gid); |
| 143 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 144 | } // namespace libcontainer |
| 145 | |
| 146 | #endif // LIBCONTAINER_LIBCONTAINER_UTIL_H_ |