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 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/properties.h> |
| 27 | #include <android-base/unique_fd.h> |
| 28 | |
| 29 | #include "util.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace init { |
| 33 | namespace { |
| 34 | |
| 35 | static constexpr const char* kLinkerMountPoint = "/bionic/bin/linker"; |
| 36 | static constexpr const char* kBootstrapLinkerPath = "/system/bin/bootstrap/linker"; |
| 37 | static constexpr const char* kRuntimeLinkerPath = "/apex/com.android.runtime/bin/linker"; |
| 38 | |
| 39 | static constexpr const char* kBionicLibsMountPointDir = "/bionic/lib/"; |
| 40 | static constexpr const char* kBootstrapBionicLibsDir = "/system/lib/bootstrap/"; |
| 41 | static constexpr const char* kRuntimeBionicLibsDir = "/apex/com.android.runtime/lib/bionic/"; |
| 42 | |
| 43 | static constexpr const char* kLinkerMountPoint64 = "/bionic/bin/linker64"; |
| 44 | static constexpr const char* kBootstrapLinkerPath64 = "/system/bin/bootstrap/linker64"; |
| 45 | static constexpr const char* kRuntimeLinkerPath64 = "/apex/com.android.runtime/bin/linker64"; |
| 46 | |
| 47 | static constexpr const char* kBionicLibsMountPointDir64 = "/bionic/lib64/"; |
| 48 | static constexpr const char* kBootstrapBionicLibsDir64 = "/system/lib64/bootstrap/"; |
| 49 | static constexpr const char* kRuntimeBionicLibsDir64 = "/apex/com.android.runtime/lib64/bionic/"; |
| 50 | |
| 51 | static const std::vector<std::string> kBionicLibFileNames = {"libc.so", "libm.so", "libdl.so"}; |
| 52 | |
| 53 | static bool BindMount(const std::string& source, const std::string& mount_point, |
| 54 | bool recursive = false) { |
| 55 | unsigned long mountflags = MS_BIND; |
| 56 | if (recursive) { |
| 57 | mountflags |= MS_REC; |
| 58 | } |
| 59 | if (mount(source.c_str(), mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
| 60 | PLOG(ERROR) << "Could not bind-mount " << source << " to " << mount_point; |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | static bool MakeShared(const std::string& mount_point, bool recursive = false) { |
| 67 | unsigned long mountflags = MS_SHARED; |
| 68 | if (recursive) { |
| 69 | mountflags |= MS_REC; |
| 70 | } |
| 71 | if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
| 72 | PLOG(ERROR) << "Failed to change propagation type to shared"; |
| 73 | return false; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | static bool MakePrivate(const std::string& mount_point, bool recursive = false) { |
| 79 | unsigned long mountflags = MS_PRIVATE; |
| 80 | if (recursive) { |
| 81 | mountflags |= MS_REC; |
| 82 | } |
| 83 | if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
| 84 | PLOG(ERROR) << "Failed to change propagation type to private"; |
| 85 | return false; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static int OpenMountNamespace() { |
| 91 | int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC); |
| 92 | if (fd < 0) { |
| 93 | PLOG(ERROR) << "Cannot open fd for current mount namespace"; |
| 94 | } |
| 95 | return fd; |
| 96 | } |
| 97 | |
| 98 | static std::string GetMountNamespaceId() { |
| 99 | std::string ret; |
| 100 | if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) { |
| 101 | PLOG(ERROR) << "Failed to read namespace ID"; |
| 102 | return ""; |
| 103 | } |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static bool BindMountBionic(const std::string& linker_source, const std::string& lib_dir_source, |
| 108 | const std::string& linker_mount_point, |
| 109 | const std::string& lib_mount_dir) { |
| 110 | if (access(linker_source.c_str(), F_OK) != 0) { |
| 111 | PLOG(INFO) << linker_source << " does not exist. skipping mounting bionic there."; |
| 112 | // This can happen for 64-bit bionic in 32-bit only device. |
| 113 | // It is okay to skip mounting the 64-bit bionic. |
| 114 | return true; |
| 115 | } |
| 116 | if (!BindMount(linker_source, linker_mount_point)) { |
| 117 | return false; |
| 118 | } |
| 119 | if (!MakePrivate(linker_mount_point)) { |
| 120 | return false; |
| 121 | } |
| 122 | for (const auto& libname : kBionicLibFileNames) { |
| 123 | std::string mount_point = lib_mount_dir + libname; |
| 124 | std::string source = lib_dir_source + libname; |
| 125 | if (!BindMount(source, mount_point)) { |
| 126 | return false; |
| 127 | } |
| 128 | if (!MakePrivate(mount_point)) { |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | static bool IsBionicUpdatable() { |
| 136 | static bool result = android::base::GetBoolProperty("ro.apex.IsBionicUpdatable", false); |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | static android::base::unique_fd bootstrap_ns_fd; |
| 141 | static android::base::unique_fd default_ns_fd; |
| 142 | |
| 143 | static std::string bootstrap_ns_id; |
| 144 | static std::string default_ns_id; |
| 145 | |
| 146 | } // namespace |
| 147 | |
| 148 | bool SetupMountNamespaces() { |
| 149 | // Set the propagation type of / as shared so that any mounting event (e.g. |
| 150 | // /data) is by default visible to all processes. When private mounting is |
| 151 | // needed for /foo/bar, then we will make /foo/bar as a mount point (by |
| 152 | // bind-mounting by to itself) and set the propagation type of the mount |
| 153 | // point to private. |
| 154 | if (!MakeShared("/", true /*recursive*/)) return false; |
| 155 | |
| 156 | // Since different files (bootstrap or runtime APEX) should be mounted to |
| 157 | // the same mount point paths (e.g. /bionic/bin/linker, /bionic/lib/libc.so, |
| 158 | // etc.) across the two mount namespaces, we create a private mount point at |
| 159 | // /bionic so that a mount event for the bootstrap bionic in the mount |
| 160 | // namespace for pre-apexd processes is not propagated to the other mount |
| 161 | // namespace for post-apexd process, and vice versa. |
| 162 | // |
| 163 | // Other mount points other than /bionic, however, are all still shared. |
| 164 | if (!BindMount("/bionic", "/bionic", true /*recursive*/)) return false; |
| 165 | if (!MakePrivate("/bionic")) return false; |
| 166 | |
| 167 | // Bind-mount bootstrap bionic. |
| 168 | if (!BindMountBionic(kBootstrapLinkerPath, kBootstrapBionicLibsDir, kLinkerMountPoint, |
| 169 | kBionicLibsMountPointDir)) |
| 170 | return false; |
| 171 | if (!BindMountBionic(kBootstrapLinkerPath64, kBootstrapBionicLibsDir64, kLinkerMountPoint64, |
| 172 | kBionicLibsMountPointDir64)) |
| 173 | return false; |
| 174 | |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame^] | 175 | // /apex is also a private mountpoint to give different sets of APEXes for |
| 176 | // the bootstrap and default mount namespaces. The processes running with |
| 177 | // the bootstrap namespace get APEXes from the read-only partition. |
| 178 | if (!(MakePrivate("/apex"))) return false; |
| 179 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 180 | bootstrap_ns_fd.reset(OpenMountNamespace()); |
| 181 | bootstrap_ns_id = GetMountNamespaceId(); |
| 182 | |
| 183 | // When bionic is updatable via the runtime APEX, we create separate mount |
| 184 | // namespaces for processes that are started before and after the APEX is |
| 185 | // activated by apexd. In the namespace for pre-apexd processes, the bionic |
| 186 | // from the /system partition (that we call bootstrap bionic) is |
| 187 | // bind-mounted. In the namespace for post-apexd processes, the bionic from |
| 188 | // the runtime APEX is bind-mounted. |
| 189 | bool success = true; |
| 190 | if (IsBionicUpdatable() && !IsRecoveryMode()) { |
| 191 | // Creating a new namespace by cloning, saving, and switching back to |
| 192 | // the original namespace. |
| 193 | if (unshare(CLONE_NEWNS) == -1) { |
| 194 | PLOG(ERROR) << "Cannot create mount namespace"; |
| 195 | return false; |
| 196 | } |
| 197 | default_ns_fd.reset(OpenMountNamespace()); |
| 198 | default_ns_id = GetMountNamespaceId(); |
| 199 | |
| 200 | // By this unmount, the bootstrap bionic are not mounted in the default |
| 201 | // mount namespace. |
| 202 | if (umount2("/bionic", MNT_DETACH) == -1) { |
| 203 | PLOG(ERROR) << "Cannot unmount /bionic"; |
| 204 | // Don't return here. We have to switch back to the bootstrap |
| 205 | // namespace. |
| 206 | success = false; |
| 207 | } |
| 208 | |
| 209 | if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 210 | PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace"; |
| 211 | return false; |
| 212 | } |
| 213 | } else { |
| 214 | // Otherwise, default == bootstrap |
| 215 | default_ns_fd.reset(OpenMountNamespace()); |
| 216 | default_ns_id = GetMountNamespaceId(); |
| 217 | } |
| 218 | |
| 219 | LOG(INFO) << "SetupMountNamespaces done"; |
| 220 | return success; |
| 221 | } |
| 222 | |
| 223 | bool SwitchToDefaultMountNamespace() { |
| 224 | if (IsRecoveryMode()) { |
| 225 | // we don't have multiple namespaces in recovery mode |
| 226 | return true; |
| 227 | } |
| 228 | if (default_ns_id != GetMountNamespaceId()) { |
| 229 | if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 230 | PLOG(ERROR) << "Failed to switch back to the default mount namespace."; |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame^] | 235 | LOG(INFO) << "Switched to default mount namespace"; |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | // TODO(jiyong): remove this when /system/lib/libc.so becomes |
| 240 | // a symlink to /apex/com.android.runtime/lib/bionic/libc.so |
| 241 | bool SetupRuntimeBionic() { |
| 242 | if (IsRecoveryMode()) { |
| 243 | // We don't have multiple namespaces in recovery mode |
| 244 | return true; |
| 245 | } |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 246 | // Bind-mount bionic from the runtime APEX since it is now available. Note |
| 247 | // that in case of IsBionicUpdatable() == false, these mounts are over the |
| 248 | // existing existing bind mounts for the bootstrap bionic, which effectively |
| 249 | // becomes hidden. |
| 250 | if (!BindMountBionic(kRuntimeLinkerPath, kRuntimeBionicLibsDir, kLinkerMountPoint, |
| 251 | kBionicLibsMountPointDir)) |
| 252 | return false; |
| 253 | if (!BindMountBionic(kRuntimeLinkerPath64, kRuntimeBionicLibsDir64, kLinkerMountPoint64, |
| 254 | kBionicLibsMountPointDir64)) |
| 255 | return false; |
| 256 | |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame^] | 257 | LOG(INFO) << "Runtime bionic is set up"; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | |
| 261 | bool SwitchToBootstrapMountNamespaceIfNeeded() { |
| 262 | if (IsRecoveryMode()) { |
| 263 | // we don't have multiple namespaces in recovery mode |
| 264 | return true; |
| 265 | } |
| 266 | if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 && |
| 267 | IsBionicUpdatable()) { |
| 268 | if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 269 | PLOG(ERROR) << "Failed to switch to bootstrap mount namespace."; |
| 270 | return false; |
| 271 | } |
| 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | } // namespace init |
| 277 | } // namespace android |