Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "mount_namespace.h" |
| 18 | |
| 19 | #include <sys/mount.h> |
| 20 | |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Jiyong Park | 8502ed3 | 2019-02-25 22:18:37 +0900 | [diff] [blame] | 24 | #include <ApexProperties.sysprop.h> |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/properties.h> |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 28 | #include <android-base/result.h> |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
| 30 | |
| 31 | #include "util.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace init { |
| 35 | namespace { |
| 36 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 37 | static bool MakeShared(const std::string& mount_point, bool recursive = false) { |
| 38 | unsigned long mountflags = MS_SHARED; |
| 39 | if (recursive) { |
| 40 | mountflags |= MS_REC; |
| 41 | } |
| 42 | if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
| 43 | PLOG(ERROR) << "Failed to change propagation type to shared"; |
| 44 | return false; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | static bool MakePrivate(const std::string& mount_point, bool recursive = false) { |
| 50 | unsigned long mountflags = MS_PRIVATE; |
| 51 | if (recursive) { |
| 52 | mountflags |= MS_REC; |
| 53 | } |
| 54 | if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
| 55 | PLOG(ERROR) << "Failed to change propagation type to private"; |
| 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | static int OpenMountNamespace() { |
| 62 | int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC); |
| 63 | if (fd < 0) { |
| 64 | PLOG(ERROR) << "Cannot open fd for current mount namespace"; |
| 65 | } |
| 66 | return fd; |
| 67 | } |
| 68 | |
| 69 | static std::string GetMountNamespaceId() { |
| 70 | std::string ret; |
| 71 | if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) { |
| 72 | PLOG(ERROR) << "Failed to read namespace ID"; |
| 73 | return ""; |
| 74 | } |
| 75 | return ret; |
| 76 | } |
| 77 | |
Jiyong Park | 8502ed3 | 2019-02-25 22:18:37 +0900 | [diff] [blame] | 78 | static bool IsApexUpdatable() { |
| 79 | static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 80 | return updatable; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 83 | static Result<void> MountDir(const std::string& path, const std::string& mount_path) { |
| 84 | if (int ret = mkdir(mount_path.c_str(), 0755); ret != 0 && ret != EEXIST) { |
| 85 | return ErrnoError() << "Could not create mount point " << mount_path; |
| 86 | } |
| 87 | if (mount(path.c_str(), mount_path.c_str(), nullptr, MS_BIND, nullptr) != 0) { |
| 88 | return ErrnoError() << "Could not bind mount " << path << " to " << mount_path; |
| 89 | } |
| 90 | return {}; |
| 91 | } |
| 92 | |
| 93 | static Result<void> ActivateFlattenedApexesFrom(const std::string& from_dir, |
| 94 | const std::string& to_dir) { |
| 95 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(from_dir.c_str()), closedir); |
| 96 | if (!dir) { |
| 97 | return {}; |
| 98 | } |
| 99 | dirent* entry; |
| 100 | while ((entry = readdir(dir.get())) != nullptr) { |
| 101 | if (entry->d_name[0] == '.') continue; |
| 102 | if (entry->d_type == DT_DIR) { |
| 103 | const std::string apex_path = from_dir + "/" + entry->d_name; |
| 104 | const std::string mount_path = to_dir + "/" + entry->d_name; |
| 105 | if (auto result = MountDir(apex_path, mount_path); !result) { |
| 106 | return result; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return {}; |
| 111 | } |
| 112 | |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 113 | static bool ActivateFlattenedApexesIfPossible() { |
| 114 | if (IsRecoveryMode() || IsApexUpdatable()) { |
| 115 | return true; |
| 116 | } |
| 117 | |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 118 | const std::string kApexTop = "/apex"; |
| 119 | const std::vector<std::string> kBuiltinDirsForApexes = { |
| 120 | "/system/apex", |
| 121 | "/system_ext/apex", |
| 122 | "/product/apex", |
| 123 | "/vendor/apex", |
| 124 | }; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 125 | |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 126 | for (const auto& dir : kBuiltinDirsForApexes) { |
| 127 | if (auto result = ActivateFlattenedApexesFrom(dir, kApexTop); !result) { |
| 128 | LOG(ERROR) << result.error(); |
| 129 | return false; |
| 130 | } |
| 131 | } |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 132 | // Special casing for the ART APEX |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 133 | constexpr const char kArtApexMountPath[] = "/apex/com.android.art"; |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 134 | static const std::vector<std::string> kArtApexDirNames = {"com.android.art.release", |
| 135 | "com.android.art.debug"}; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 136 | bool success = false; |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 137 | for (const auto& name : kArtApexDirNames) { |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 138 | std::string path = kApexTop + "/" + name; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 139 | if (access(path.c_str(), F_OK) == 0) { |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 140 | if (auto result = MountDir(path, kArtApexMountPath); !result) { |
| 141 | LOG(ERROR) << result.error(); |
| 142 | return false; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 143 | } |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame^] | 144 | success = true; |
| 145 | break; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | if (!success) { |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 149 | PLOG(ERROR) << "Failed to bind mount the ART APEX to " << kArtApexMountPath; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 150 | } |
| 151 | return success; |
| 152 | } |
| 153 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 154 | static android::base::unique_fd bootstrap_ns_fd; |
| 155 | static android::base::unique_fd default_ns_fd; |
| 156 | |
| 157 | static std::string bootstrap_ns_id; |
| 158 | static std::string default_ns_id; |
| 159 | |
| 160 | } // namespace |
| 161 | |
| 162 | bool SetupMountNamespaces() { |
| 163 | // Set the propagation type of / as shared so that any mounting event (e.g. |
| 164 | // /data) is by default visible to all processes. When private mounting is |
| 165 | // needed for /foo/bar, then we will make /foo/bar as a mount point (by |
| 166 | // bind-mounting by to itself) and set the propagation type of the mount |
| 167 | // point to private. |
| 168 | if (!MakeShared("/", true /*recursive*/)) return false; |
| 169 | |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 170 | // /apex is a private mountpoint to give different sets of APEXes for |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 171 | // the bootstrap and default mount namespaces. The processes running with |
| 172 | // the bootstrap namespace get APEXes from the read-only partition. |
| 173 | if (!(MakePrivate("/apex"))) return false; |
| 174 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 175 | bootstrap_ns_fd.reset(OpenMountNamespace()); |
| 176 | bootstrap_ns_id = GetMountNamespaceId(); |
| 177 | |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 178 | // When APEXes are updatable (e.g. not-flattened), we create separate mount |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 179 | // namespaces for processes that are started before and after the APEX is |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 180 | // activated by apexd. In the namespace for pre-apexd processes, small |
| 181 | // number of essential APEXes (e.g. com.android.runtime) are activated. |
| 182 | // In the namespace for post-apexd processes, all APEXes are activated. |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 183 | bool success = true; |
Jiyong Park | 8502ed3 | 2019-02-25 22:18:37 +0900 | [diff] [blame] | 184 | if (IsApexUpdatable() && !IsRecoveryMode()) { |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 185 | // Creating a new namespace by cloning, saving, and switching back to |
| 186 | // the original namespace. |
| 187 | if (unshare(CLONE_NEWNS) == -1) { |
| 188 | PLOG(ERROR) << "Cannot create mount namespace"; |
| 189 | return false; |
| 190 | } |
| 191 | default_ns_fd.reset(OpenMountNamespace()); |
| 192 | default_ns_id = GetMountNamespaceId(); |
| 193 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 194 | if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 195 | PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace"; |
| 196 | return false; |
| 197 | } |
| 198 | } else { |
| 199 | // Otherwise, default == bootstrap |
| 200 | default_ns_fd.reset(OpenMountNamespace()); |
| 201 | default_ns_id = GetMountNamespaceId(); |
| 202 | } |
| 203 | |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 204 | success &= ActivateFlattenedApexesIfPossible(); |
| 205 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 206 | LOG(INFO) << "SetupMountNamespaces done"; |
| 207 | return success; |
| 208 | } |
| 209 | |
| 210 | bool SwitchToDefaultMountNamespace() { |
| 211 | if (IsRecoveryMode()) { |
| 212 | // we don't have multiple namespaces in recovery mode |
| 213 | return true; |
| 214 | } |
| 215 | if (default_ns_id != GetMountNamespaceId()) { |
| 216 | if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 217 | PLOG(ERROR) << "Failed to switch back to the default mount namespace."; |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 222 | LOG(INFO) << "Switched to default mount namespace"; |
| 223 | return true; |
| 224 | } |
| 225 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 226 | bool SwitchToBootstrapMountNamespaceIfNeeded() { |
| 227 | if (IsRecoveryMode()) { |
| 228 | // we don't have multiple namespaces in recovery mode |
| 229 | return true; |
| 230 | } |
| 231 | if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 && |
Jiyong Park | 8502ed3 | 2019-02-25 22:18:37 +0900 | [diff] [blame] | 232 | IsApexUpdatable()) { |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 233 | if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 234 | PLOG(ERROR) << "Failed to switch to bootstrap mount namespace."; |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | } // namespace init |
| 242 | } // namespace android |