blob: b9d5d674ccba6e18a215d1634b53aeba5aa2d654 [file] [log] [blame]
Jiyong Park68660412019-01-16 23:00:59 +09001/*
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 Park8502ed32019-02-25 22:18:37 +090024#include <ApexProperties.sysprop.h>
Jiyong Park68660412019-01-16 23:00:59 +090025#include <android-base/file.h>
26#include <android-base/logging.h>
27#include <android-base/properties.h>
Jooyung Han5bb9d212019-11-25 13:50:44 +090028#include <android-base/result.h>
Jiyong Park68660412019-01-16 23:00:59 +090029#include <android-base/unique_fd.h>
Jiyong Park648ae3a2019-12-08 00:25:15 +090030#include <apex_manifest.pb.h>
Jiyong Park68660412019-01-16 23:00:59 +090031
32#include "util.h"
33
34namespace android {
35namespace init {
36namespace {
37
Martijn Coenenc70c0662020-01-10 15:42:15 +010038static bool BindMount(const std::string& source, const std::string& mount_point,
39 bool recursive = false) {
40 unsigned long mountflags = MS_BIND;
41 if (recursive) {
42 mountflags |= MS_REC;
43 }
44 if (mount(source.c_str(), mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
45 PLOG(ERROR) << "Failed to bind mount " << source;
46 return false;
47 }
48 return true;
49}
50
Jiyong Park68660412019-01-16 23:00:59 +090051static bool MakeShared(const std::string& mount_point, bool recursive = false) {
52 unsigned long mountflags = MS_SHARED;
53 if (recursive) {
54 mountflags |= MS_REC;
55 }
56 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
57 PLOG(ERROR) << "Failed to change propagation type to shared";
58 return false;
59 }
60 return true;
61}
62
Martijn Coenenc70c0662020-01-10 15:42:15 +010063static bool MakeSlave(const std::string& mount_point, bool recursive = false) {
64 unsigned long mountflags = MS_SLAVE;
65 if (recursive) {
66 mountflags |= MS_REC;
67 }
68 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
69 PLOG(ERROR) << "Failed to change propagation type to slave";
70 return false;
71 }
72 return true;
73}
74
Jiyong Park68660412019-01-16 23:00:59 +090075static bool MakePrivate(const std::string& mount_point, bool recursive = false) {
76 unsigned long mountflags = MS_PRIVATE;
77 if (recursive) {
78 mountflags |= MS_REC;
79 }
80 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
81 PLOG(ERROR) << "Failed to change propagation type to private";
82 return false;
83 }
84 return true;
85}
86
87static int OpenMountNamespace() {
88 int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
89 if (fd < 0) {
90 PLOG(ERROR) << "Cannot open fd for current mount namespace";
91 }
92 return fd;
93}
94
95static std::string GetMountNamespaceId() {
96 std::string ret;
97 if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) {
98 PLOG(ERROR) << "Failed to read namespace ID";
99 return "";
100 }
101 return ret;
102}
103
Jiyong Park8502ed32019-02-25 22:18:37 +0900104static bool IsApexUpdatable() {
105 static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false);
106 return updatable;
Jiyong Park68660412019-01-16 23:00:59 +0900107}
108
Jooyung Han5bb9d212019-11-25 13:50:44 +0900109static Result<void> MountDir(const std::string& path, const std::string& mount_path) {
Satoshi Niwa1eb300d2020-01-20 18:00:49 +0900110 if (int ret = mkdir(mount_path.c_str(), 0755); ret != 0 && errno != EEXIST) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900111 return ErrnoError() << "Could not create mount point " << mount_path;
112 }
113 if (mount(path.c_str(), mount_path.c_str(), nullptr, MS_BIND, nullptr) != 0) {
114 return ErrnoError() << "Could not bind mount " << path << " to " << mount_path;
115 }
116 return {};
117}
118
Jiyong Park648ae3a2019-12-08 00:25:15 +0900119static Result<std::string> GetApexName(const std::string& apex_dir) {
120 const std::string manifest_path = apex_dir + "/apex_manifest.pb";
121 std::string content;
122 if (!android::base::ReadFileToString(manifest_path, &content)) {
123 return Error() << "Failed to read manifest file: " << manifest_path;
124 }
125 apex::proto::ApexManifest manifest;
126 if (!manifest.ParseFromString(content)) {
127 return Error() << "Can't parse manifest file: " << manifest_path;
128 }
129 return manifest.name();
130}
131
Jooyung Han5bb9d212019-11-25 13:50:44 +0900132static Result<void> ActivateFlattenedApexesFrom(const std::string& from_dir,
133 const std::string& to_dir) {
134 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(from_dir.c_str()), closedir);
135 if (!dir) {
136 return {};
137 }
138 dirent* entry;
139 while ((entry = readdir(dir.get())) != nullptr) {
140 if (entry->d_name[0] == '.') continue;
141 if (entry->d_type == DT_DIR) {
142 const std::string apex_path = from_dir + "/" + entry->d_name;
Jiyong Park648ae3a2019-12-08 00:25:15 +0900143 const auto apex_name = GetApexName(apex_path);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900144 if (!apex_name.ok()) {
Jiyong Park648ae3a2019-12-08 00:25:15 +0900145 LOG(ERROR) << apex_path << " is not an APEX directory: " << apex_name.error();
146 continue;
147 }
148 const std::string mount_path = to_dir + "/" + (*apex_name);
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900149 if (auto result = MountDir(apex_path, mount_path); !result.ok()) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900150 return result;
151 }
152 }
153 }
154 return {};
155}
156
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900157static bool ActivateFlattenedApexesIfPossible() {
158 if (IsRecoveryMode() || IsApexUpdatable()) {
159 return true;
160 }
161
Jooyung Han5bb9d212019-11-25 13:50:44 +0900162 const std::string kApexTop = "/apex";
163 const std::vector<std::string> kBuiltinDirsForApexes = {
164 "/system/apex",
165 "/system_ext/apex",
166 "/product/apex",
167 "/vendor/apex",
168 };
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900169
Jooyung Han5bb9d212019-11-25 13:50:44 +0900170 for (const auto& dir : kBuiltinDirsForApexes) {
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900171 if (auto result = ActivateFlattenedApexesFrom(dir, kApexTop); !result.ok()) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900172 LOG(ERROR) << result.error();
173 return false;
174 }
175 }
Jiyong Park648ae3a2019-12-08 00:25:15 +0900176 return true;
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900177}
178
Jiyong Park68660412019-01-16 23:00:59 +0900179static android::base::unique_fd bootstrap_ns_fd;
180static android::base::unique_fd default_ns_fd;
181
182static std::string bootstrap_ns_id;
183static std::string default_ns_id;
184
185} // namespace
186
187bool SetupMountNamespaces() {
188 // Set the propagation type of / as shared so that any mounting event (e.g.
189 // /data) is by default visible to all processes. When private mounting is
190 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
191 // bind-mounting by to itself) and set the propagation type of the mount
192 // point to private.
193 if (!MakeShared("/", true /*recursive*/)) return false;
194
Jiyong Park7b4801a2019-02-25 16:41:38 +0900195 // /apex is a private mountpoint to give different sets of APEXes for
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900196 // the bootstrap and default mount namespaces. The processes running with
197 // the bootstrap namespace get APEXes from the read-only partition.
198 if (!(MakePrivate("/apex"))) return false;
199
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900200 // /linkerconfig is a private mountpoint to give a different linker configuration
201 // based on the mount namespace. Subdirectory will be bind-mounted based on current mount
202 // namespace
203 if (!(MakePrivate("/linkerconfig"))) return false;
204
Martijn Coenenc70c0662020-01-10 15:42:15 +0100205 // The two mount namespaces present challenges for scoped storage, because
206 // vold, which is responsible for most of the mounting, lives in the
207 // bootstrap mount namespace, whereas most other daemons and all apps live
208 // in the default namespace. Scoped storage has a need for a
209 // /mnt/installer view that is a slave bind mount of /mnt/user - in other
210 // words, all mounts under /mnt/user should automatically show up under
211 // /mnt/installer. However, additional mounts done under /mnt/installer
212 // should not propagate back to /mnt/user. In a single mount namespace
213 // this is easy to achieve, by simply marking the /mnt/installer a slave
214 // bind mount. Unfortunately, if /mnt/installer is only created and
215 // bind mounted after the two namespaces are created below, we end up
216 // with the following situation:
217 // /mnt/user and /mnt/installer share the same peer group in both the
218 // bootstrap and default namespaces. Marking /mnt/installer slave in either
219 // namespace means that it won't propagate events to the /mnt/installer in
220 // the other namespace, which is still something we require - vold is the
221 // one doing the mounting under /mnt/installer, and those mounts should
222 // show up in the default namespace as well.
223 //
224 // The simplest solution is to do the bind mount before the two namespaces
225 // are created: the effect is that in both namespaces, /mnt/installer is a
226 // slave to the /mnt/user mount, and at the same time /mnt/installer in the
227 // bootstrap namespace shares a peer group with /mnt/installer in the
228 // default namespace.
Ricky Waia4c163d2020-04-21 12:16:43 +0100229 // /mnt/androidwritable is similar to /mnt/installer but serves for
230 // MOUNT_EXTERNAL_ANDROID_WRITABLE apps.
Martijn Coenenc70c0662020-01-10 15:42:15 +0100231 if (!mkdir_recursive("/mnt/user", 0755)) return false;
232 if (!mkdir_recursive("/mnt/installer", 0755)) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100233 if (!mkdir_recursive("/mnt/androidwritable", 0755)) return false;
Martijn Coenenc70c0662020-01-10 15:42:15 +0100234 if (!(BindMount("/mnt/user", "/mnt/installer", true))) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100235 if (!(BindMount("/mnt/user", "/mnt/androidwritable", true))) return false;
236 // First, make /mnt/installer and /mnt/androidwritable a slave bind mount
Martijn Coenenc70c0662020-01-10 15:42:15 +0100237 if (!(MakeSlave("/mnt/installer"))) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100238 if (!(MakeSlave("/mnt/androidwritable"))) return false;
Martijn Coenenc70c0662020-01-10 15:42:15 +0100239 // Then, make it shared again - effectively creating a new peer group, that
240 // will be inherited by new mount namespaces.
241 if (!(MakeShared("/mnt/installer"))) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100242 if (!(MakeShared("/mnt/androidwritable"))) return false;
Martijn Coenenc70c0662020-01-10 15:42:15 +0100243
Jiyong Park68660412019-01-16 23:00:59 +0900244 bootstrap_ns_fd.reset(OpenMountNamespace());
245 bootstrap_ns_id = GetMountNamespaceId();
246
Jiyong Park7b4801a2019-02-25 16:41:38 +0900247 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900248 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900249 // activated by apexd. In the namespace for pre-apexd processes, small
250 // number of essential APEXes (e.g. com.android.runtime) are activated.
251 // In the namespace for post-apexd processes, all APEXes are activated.
Jiyong Park68660412019-01-16 23:00:59 +0900252 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900253 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900254 // Creating a new namespace by cloning, saving, and switching back to
255 // the original namespace.
256 if (unshare(CLONE_NEWNS) == -1) {
257 PLOG(ERROR) << "Cannot create mount namespace";
258 return false;
259 }
260 default_ns_fd.reset(OpenMountNamespace());
261 default_ns_id = GetMountNamespaceId();
262
Jiyong Park68660412019-01-16 23:00:59 +0900263 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
264 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
265 return false;
266 }
267 } else {
268 // Otherwise, default == bootstrap
269 default_ns_fd.reset(OpenMountNamespace());
270 default_ns_id = GetMountNamespaceId();
271 }
272
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900273 success &= ActivateFlattenedApexesIfPossible();
274
Jiyong Park68660412019-01-16 23:00:59 +0900275 LOG(INFO) << "SetupMountNamespaces done";
276 return success;
277}
278
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900279Result<void> SwitchToMountNamespaceIfNeeded(MountNamespace target_mount_namespace) {
280 if (IsRecoveryMode() || !IsApexUpdatable()) {
281 // we don't have multiple namespaces in recovery mode or if apex is not updatable
282 return {};
Jiyong Park68660412019-01-16 23:00:59 +0900283 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900284 const auto& ns_id = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_id : default_ns_id;
285 const auto& ns_fd = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_fd : default_ns_fd;
286 const auto& ns_name = target_mount_namespace == NS_BOOTSTRAP ? "bootstrap" : "default";
287 if (ns_id != GetMountNamespaceId() && ns_fd.get() != -1) {
288 if (setns(ns_fd.get(), CLONE_NEWNS) == -1) {
289 return ErrnoError() << "Failed to switch to " << ns_name << " mount namespace.";
Kiyoung Kime4d3f212019-12-16 14:31:04 +0900290 }
Jiyong Park68660412019-01-16 23:00:59 +0900291 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900292 return {};
Jiyong Park68660412019-01-16 23:00:59 +0900293}
294
295} // namespace init
296} // namespace android