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 | |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 33 | #ifndef RECOVERY |
| 34 | #define ACTIVATE_FLATTENED_APEX 1 |
| 35 | #endif |
| 36 | |
| 37 | #ifdef ACTIVATE_FLATTENED_APEX |
| 38 | #include <apex_manifest.pb.h> |
| 39 | #include <com_android_apex.h> |
| 40 | #include <selinux/android.h> |
| 41 | #endif // ACTIVATE_FLATTENED_APEX |
| 42 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace init { |
| 45 | namespace { |
| 46 | |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 47 | static bool BindMount(const std::string& source, const std::string& mount_point) { |
| 48 | if (mount(source.c_str(), mount_point.c_str(), nullptr, MS_BIND | MS_REC, nullptr) == -1) { |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 49 | PLOG(ERROR) << "Failed to bind mount " << source; |
| 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 55 | static bool ChangeMount(const std::string& mount_point, unsigned long mountflags) { |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 56 | if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 57 | PLOG(ERROR) << "Failed to remount " << mount_point << " as " << std::hex << mountflags; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | static int OpenMountNamespace() { |
| 64 | int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC); |
| 65 | if (fd < 0) { |
| 66 | PLOG(ERROR) << "Cannot open fd for current mount namespace"; |
| 67 | } |
| 68 | return fd; |
| 69 | } |
| 70 | |
| 71 | static std::string GetMountNamespaceId() { |
| 72 | std::string ret; |
| 73 | if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) { |
| 74 | PLOG(ERROR) << "Failed to read namespace ID"; |
| 75 | return ""; |
| 76 | } |
| 77 | return ret; |
| 78 | } |
| 79 | |
Jiyong Park | 8502ed3 | 2019-02-25 22:18:37 +0900 | [diff] [blame] | 80 | static bool IsApexUpdatable() { |
| 81 | static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 82 | return updatable; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Jooyung Han | 653b063 | 2021-07-29 17:11:23 +0900 | [diff] [blame] | 85 | // In case we have two sets of APEXes (non-updatable, updatable), we need two separate mount |
| 86 | // namespaces. |
| 87 | static bool NeedsTwoMountNamespaces() { |
| 88 | if (!IsApexUpdatable()) return false; |
| 89 | if (IsRecoveryMode()) return false; |
| 90 | // In microdroid, there's only one set of APEXes in built-in directories include block devices. |
| 91 | if (IsMicrodroid()) return false; |
| 92 | return true; |
| 93 | } |
| 94 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 95 | static android::base::unique_fd bootstrap_ns_fd; |
| 96 | static android::base::unique_fd default_ns_fd; |
| 97 | |
| 98 | static std::string bootstrap_ns_id; |
| 99 | static std::string default_ns_id; |
| 100 | |
| 101 | } // namespace |
| 102 | |
| 103 | bool SetupMountNamespaces() { |
| 104 | // Set the propagation type of / as shared so that any mounting event (e.g. |
| 105 | // /data) is by default visible to all processes. When private mounting is |
| 106 | // needed for /foo/bar, then we will make /foo/bar as a mount point (by |
| 107 | // bind-mounting by to itself) and set the propagation type of the mount |
| 108 | // point to private. |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 109 | if (!ChangeMount("/", MS_SHARED | MS_REC)) return false; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 110 | |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 111 | // /apex is a private mountpoint to give different sets of APEXes for |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 112 | // the bootstrap and default mount namespaces. The processes running with |
| 113 | // the bootstrap namespace get APEXes from the read-only partition. |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 114 | if (!(ChangeMount("/apex", MS_PRIVATE))) return false; |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 115 | |
Kiyoung Kim | 99df54b | 2019-11-22 16:14:10 +0900 | [diff] [blame] | 116 | // /linkerconfig is a private mountpoint to give a different linker configuration |
| 117 | // based on the mount namespace. Subdirectory will be bind-mounted based on current mount |
| 118 | // namespace |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 119 | if (!(ChangeMount("/linkerconfig", MS_PRIVATE))) return false; |
Kiyoung Kim | 99df54b | 2019-11-22 16:14:10 +0900 | [diff] [blame] | 120 | |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 121 | // The two mount namespaces present challenges for scoped storage, because |
| 122 | // vold, which is responsible for most of the mounting, lives in the |
| 123 | // bootstrap mount namespace, whereas most other daemons and all apps live |
| 124 | // in the default namespace. Scoped storage has a need for a |
| 125 | // /mnt/installer view that is a slave bind mount of /mnt/user - in other |
| 126 | // words, all mounts under /mnt/user should automatically show up under |
| 127 | // /mnt/installer. However, additional mounts done under /mnt/installer |
| 128 | // should not propagate back to /mnt/user. In a single mount namespace |
| 129 | // this is easy to achieve, by simply marking the /mnt/installer a slave |
| 130 | // bind mount. Unfortunately, if /mnt/installer is only created and |
| 131 | // bind mounted after the two namespaces are created below, we end up |
| 132 | // with the following situation: |
| 133 | // /mnt/user and /mnt/installer share the same peer group in both the |
| 134 | // bootstrap and default namespaces. Marking /mnt/installer slave in either |
| 135 | // namespace means that it won't propagate events to the /mnt/installer in |
| 136 | // the other namespace, which is still something we require - vold is the |
| 137 | // one doing the mounting under /mnt/installer, and those mounts should |
| 138 | // show up in the default namespace as well. |
| 139 | // |
| 140 | // The simplest solution is to do the bind mount before the two namespaces |
| 141 | // are created: the effect is that in both namespaces, /mnt/installer is a |
| 142 | // slave to the /mnt/user mount, and at the same time /mnt/installer in the |
| 143 | // bootstrap namespace shares a peer group with /mnt/installer in the |
| 144 | // default namespace. |
Ricky Wai | a4c163d | 2020-04-21 12:16:43 +0100 | [diff] [blame] | 145 | // /mnt/androidwritable is similar to /mnt/installer but serves for |
| 146 | // MOUNT_EXTERNAL_ANDROID_WRITABLE apps. |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 147 | if (!mkdir_recursive("/mnt/user", 0755)) return false; |
| 148 | if (!mkdir_recursive("/mnt/installer", 0755)) return false; |
Ricky Wai | a4c163d | 2020-04-21 12:16:43 +0100 | [diff] [blame] | 149 | if (!mkdir_recursive("/mnt/androidwritable", 0755)) return false; |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 150 | if (!(BindMount("/mnt/user", "/mnt/installer"))) return false; |
| 151 | if (!(BindMount("/mnt/user", "/mnt/androidwritable"))) return false; |
Ricky Wai | a4c163d | 2020-04-21 12:16:43 +0100 | [diff] [blame] | 152 | // First, make /mnt/installer and /mnt/androidwritable a slave bind mount |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 153 | if (!(ChangeMount("/mnt/installer", MS_SLAVE))) return false; |
| 154 | if (!(ChangeMount("/mnt/androidwritable", MS_SLAVE))) return false; |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 155 | // Then, make it shared again - effectively creating a new peer group, that |
| 156 | // will be inherited by new mount namespaces. |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 157 | if (!(ChangeMount("/mnt/installer", MS_SHARED))) return false; |
| 158 | if (!(ChangeMount("/mnt/androidwritable", MS_SHARED))) return false; |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 159 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 160 | bootstrap_ns_fd.reset(OpenMountNamespace()); |
| 161 | bootstrap_ns_id = GetMountNamespaceId(); |
| 162 | |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 163 | // When APEXes are updatable (e.g. not-flattened), we create separate mount |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 164 | // namespaces for processes that are started before and after the APEX is |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 165 | // activated by apexd. In the namespace for pre-apexd processes, small |
| 166 | // number of essential APEXes (e.g. com.android.runtime) are activated. |
| 167 | // In the namespace for post-apexd processes, all APEXes are activated. |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 168 | bool success = true; |
Jooyung Han | 653b063 | 2021-07-29 17:11:23 +0900 | [diff] [blame] | 169 | if (NeedsTwoMountNamespaces()) { |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 170 | // Creating a new namespace by cloning, saving, and switching back to |
| 171 | // the original namespace. |
| 172 | if (unshare(CLONE_NEWNS) == -1) { |
| 173 | PLOG(ERROR) << "Cannot create mount namespace"; |
| 174 | return false; |
| 175 | } |
| 176 | default_ns_fd.reset(OpenMountNamespace()); |
| 177 | default_ns_id = GetMountNamespaceId(); |
| 178 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 179 | if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 180 | PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace"; |
| 181 | return false; |
| 182 | } |
| 183 | } else { |
| 184 | // Otherwise, default == bootstrap |
| 185 | default_ns_fd.reset(OpenMountNamespace()); |
| 186 | default_ns_id = GetMountNamespaceId(); |
| 187 | } |
Shikha Malhotra | 720694d | 2021-07-30 12:35:27 +0000 | [diff] [blame] | 188 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 189 | LOG(INFO) << "SetupMountNamespaces done"; |
| 190 | return success; |
| 191 | } |
| 192 | |
Jooyung Han | 5eb441c | 2022-07-23 01:41:18 +0900 | [diff] [blame^] | 193 | // Switch the mount namespace of the current process from bootstrap to default OR from default to |
| 194 | // bootstrap. If the current mount namespace is neither bootstrap nor default, keep it that way. |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 195 | Result<void> SwitchToMountNamespaceIfNeeded(MountNamespace target_mount_namespace) { |
| 196 | if (IsRecoveryMode() || !IsApexUpdatable()) { |
| 197 | // we don't have multiple namespaces in recovery mode or if apex is not updatable |
| 198 | return {}; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 199 | } |
Jooyung Han | 5eb441c | 2022-07-23 01:41:18 +0900 | [diff] [blame^] | 200 | |
| 201 | const std::string current_namespace_id = GetMountNamespaceId(); |
| 202 | MountNamespace current_mount_namespace; |
| 203 | if (current_namespace_id == bootstrap_ns_id) { |
| 204 | current_mount_namespace = NS_BOOTSTRAP; |
| 205 | } else if (current_namespace_id == default_ns_id) { |
| 206 | current_mount_namespace = NS_DEFAULT; |
| 207 | } else { |
| 208 | // services with `namespace mnt` start in its own mount namespace. So we need to keep it. |
| 209 | return {}; |
| 210 | } |
| 211 | |
| 212 | // We're already in the target mount namespace. |
| 213 | if (current_mount_namespace == target_mount_namespace) { |
| 214 | return {}; |
| 215 | } |
| 216 | |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 217 | const auto& ns_fd = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_fd : default_ns_fd; |
| 218 | const auto& ns_name = target_mount_namespace == NS_BOOTSTRAP ? "bootstrap" : "default"; |
Jooyung Han | 5eb441c | 2022-07-23 01:41:18 +0900 | [diff] [blame^] | 219 | if (ns_fd.get() != -1) { |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 220 | if (setns(ns_fd.get(), CLONE_NEWNS) == -1) { |
| 221 | return ErrnoError() << "Failed to switch to " << ns_name << " mount namespace."; |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 222 | } |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 223 | } |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 224 | return {}; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 225 | } |
| 226 | |
Kiyoung Kim | 0cbee0d | 2021-03-02 16:45:27 +0900 | [diff] [blame] | 227 | base::Result<MountNamespace> GetCurrentMountNamespace() { |
| 228 | std::string current_namespace_id = GetMountNamespaceId(); |
| 229 | if (current_namespace_id == "") { |
| 230 | return Error() << "Failed to get current mount namespace ID"; |
| 231 | } |
| 232 | |
| 233 | if (current_namespace_id == bootstrap_ns_id) { |
| 234 | return NS_BOOTSTRAP; |
| 235 | } else if (current_namespace_id == default_ns_id) { |
| 236 | return NS_DEFAULT; |
| 237 | } |
| 238 | |
| 239 | return Error() << "Failed to find current mount namespace"; |
| 240 | } |
| 241 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 242 | } // namespace init |
| 243 | } // namespace android |