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 | |
| 8 | #include <string> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 9 | #include <vector> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 10 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 11 | #include <base/callback_forward.h> |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 12 | #include <base/files/file_path.h> |
Luis Hector Chavez | 835d39e | 2017-09-19 15:16:31 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
| 14 | #include <base/macros.h> |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 15 | #include <libminijail.h> |
| 16 | |
| 17 | #include "libcontainer/config.h" |
| 18 | #include "libcontainer/libcontainer.h" |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 19 | |
| 20 | namespace libcontainer { |
| 21 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 22 | // WaitablePipe provides a way for one process to wait on another. This only |
| 23 | // uses the read(2) and close(2) syscalls, so it can work even in a restrictive |
| 24 | // environment. Each process must call only one of Wait() and Signal() exactly |
| 25 | // once. |
| 26 | struct WaitablePipe { |
| 27 | WaitablePipe(); |
| 28 | ~WaitablePipe(); |
| 29 | |
| 30 | WaitablePipe(WaitablePipe&&); |
| 31 | |
| 32 | // Waits for Signal() to be called. |
| 33 | void Wait(); |
| 34 | |
| 35 | // Notifies the process that called Wait() to continue running. |
| 36 | void Signal(); |
| 37 | |
| 38 | int pipe_fds[2]; |
| 39 | |
| 40 | private: |
| 41 | DISALLOW_COPY_AND_ASSIGN(WaitablePipe); |
| 42 | }; |
| 43 | |
| 44 | // HookState holds two WaitablePipes so that the container can wait for its |
| 45 | // parent to run prestart hooks just prior to calling execve(2). |
| 46 | class HookState { |
| 47 | public: |
| 48 | HookState(); |
| 49 | ~HookState(); |
| 50 | |
| 51 | HookState(HookState&& state); |
| 52 | |
| 53 | // Initializes this HookState so that WaitForHookAndRun() can be invoked and |
| 54 | // waited upon when |j| reaches |event|. Returns true on success. |
| 55 | bool InstallHook(minijail* j, minijail_hook_event_t event); |
| 56 | |
| 57 | // Waits for the event specified in InstallHook() and invokes |callbacks| in |
| 58 | // the caller process. Returns true if all callbacks succeeded. |
| 59 | bool WaitForHookAndRun(const std::vector<HookCallback>& callbacks, |
| 60 | pid_t container_pid); |
| 61 | |
| 62 | private: |
| 63 | // A function that can be passed to minijail_add_hook() that blocks the |
| 64 | // process in the container until the parent has finished running whatever |
| 65 | // operations are needed outside the container. This is not expected to be |
| 66 | // called directly. |
| 67 | static int WaitHook(void* payload); |
| 68 | |
| 69 | bool installed_ = false; |
| 70 | WaitablePipe reached_pipe_; |
| 71 | WaitablePipe ready_pipe_; |
| 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(HookState); |
| 74 | }; |
| 75 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 76 | // 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] | 77 | // the user namespace, populate |id_out|. Returns true on success. |
| 78 | bool GetUsernsOutsideId(const std::string& map, int id, int* id_out); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 79 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 80 | 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] | 81 | |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 82 | 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] | 83 | |
| 84 | // Find a free loop device and attach it. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 85 | bool LoopdevSetup(const base::FilePath& source, |
| 86 | base::FilePath* loopdev_path_out); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 87 | |
| 88 | // Detach the specified loop device. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 89 | bool LoopdevDetach(const base::FilePath& loopdev); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 90 | |
| 91 | // Create a new device mapper target for the source. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 92 | bool DeviceMapperSetup(const base::FilePath& source, |
| 93 | const std::string& verity_cmdline, |
| 94 | base::FilePath* dm_path_out, |
| 95 | std::string* dm_name_out); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 96 | |
| 97 | // Tear down the device mapper target. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 98 | bool DeviceMapperDetach(const std::string& dm_name); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 99 | |
| 100 | // Match mount_one in minijail, mount one mountpoint with |
| 101 | // consideration for combination of MS_BIND/MS_RDONLY flag. |
Luis Hector Chavez | 1f7e60c | 2017-09-27 22:03:48 -0700 | [diff] [blame] | 102 | bool MountExternal(const std::string& src, |
| 103 | const std::string& dest, |
| 104 | const std::string& type, |
| 105 | unsigned long flags, |
| 106 | const std::string& data); |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 107 | |
Luis Hector Chavez | 644d204 | 2017-09-19 18:56:44 -0700 | [diff] [blame] | 108 | // Wraps a callback to be run in a subset of the container's namespaces. |
| 109 | HookCallback AdaptCallbackToRunInNamespaces(HookCallback callback, |
| 110 | std::vector<int> nstypes); |
| 111 | |
Luis Hector Chavez | 81efb33 | 2017-09-18 14:01:29 -0700 | [diff] [blame] | 112 | } // namespace libcontainer |
| 113 | |
| 114 | #endif // LIBCONTAINER_LIBCONTAINER_UTIL_H_ |