blob: 1a474fbf349da50b687512d0e7d21f4125e8bc45 [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);
144 if (!apex_name) {
145 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);
Jooyung Han5bb9d212019-11-25 13:50:44 +0900149 if (auto result = MountDir(apex_path, mount_path); !result) {
150 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) {
171 if (auto result = ActivateFlattenedApexesFrom(dir, kApexTop); !result) {
172 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
Kiyoung Kime4d3f212019-12-16 14:31:04 +0900179static Result<void> MountLinkerConfigForDefaultNamespace() {
180 // No need to mount linkerconfig for default mount namespace if the path does not exist (which
181 // would mean it is already mounted)
182 if (access("/linkerconfig/default", 0) != 0) {
183 return {};
184 }
185
186 if (mount("/linkerconfig/default", "/linkerconfig", nullptr, MS_BIND | MS_REC, nullptr) != 0) {
187 return ErrnoError() << "Failed to mount linker configuration for default mount namespace.";
188 }
189
190 return {};
191}
192
Jiyong Park68660412019-01-16 23:00:59 +0900193static android::base::unique_fd bootstrap_ns_fd;
194static android::base::unique_fd default_ns_fd;
195
196static std::string bootstrap_ns_id;
197static std::string default_ns_id;
198
199} // namespace
200
201bool SetupMountNamespaces() {
202 // Set the propagation type of / as shared so that any mounting event (e.g.
203 // /data) is by default visible to all processes. When private mounting is
204 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
205 // bind-mounting by to itself) and set the propagation type of the mount
206 // point to private.
207 if (!MakeShared("/", true /*recursive*/)) return false;
208
Jiyong Park7b4801a2019-02-25 16:41:38 +0900209 // /apex is a private mountpoint to give different sets of APEXes for
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900210 // the bootstrap and default mount namespaces. The processes running with
211 // the bootstrap namespace get APEXes from the read-only partition.
212 if (!(MakePrivate("/apex"))) return false;
213
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900214 // /linkerconfig is a private mountpoint to give a different linker configuration
215 // based on the mount namespace. Subdirectory will be bind-mounted based on current mount
216 // namespace
217 if (!(MakePrivate("/linkerconfig"))) return false;
218
Martijn Coenenc70c0662020-01-10 15:42:15 +0100219 // The two mount namespaces present challenges for scoped storage, because
220 // vold, which is responsible for most of the mounting, lives in the
221 // bootstrap mount namespace, whereas most other daemons and all apps live
222 // in the default namespace. Scoped storage has a need for a
223 // /mnt/installer view that is a slave bind mount of /mnt/user - in other
224 // words, all mounts under /mnt/user should automatically show up under
225 // /mnt/installer. However, additional mounts done under /mnt/installer
226 // should not propagate back to /mnt/user. In a single mount namespace
227 // this is easy to achieve, by simply marking the /mnt/installer a slave
228 // bind mount. Unfortunately, if /mnt/installer is only created and
229 // bind mounted after the two namespaces are created below, we end up
230 // with the following situation:
231 // /mnt/user and /mnt/installer share the same peer group in both the
232 // bootstrap and default namespaces. Marking /mnt/installer slave in either
233 // namespace means that it won't propagate events to the /mnt/installer in
234 // the other namespace, which is still something we require - vold is the
235 // one doing the mounting under /mnt/installer, and those mounts should
236 // show up in the default namespace as well.
237 //
238 // The simplest solution is to do the bind mount before the two namespaces
239 // are created: the effect is that in both namespaces, /mnt/installer is a
240 // slave to the /mnt/user mount, and at the same time /mnt/installer in the
241 // bootstrap namespace shares a peer group with /mnt/installer in the
242 // default namespace.
243 if (!mkdir_recursive("/mnt/user", 0755)) return false;
244 if (!mkdir_recursive("/mnt/installer", 0755)) return false;
245 if (!(BindMount("/mnt/user", "/mnt/installer", true))) return false;
246 // First, make /mnt/installer a slave bind mount
247 if (!(MakeSlave("/mnt/installer"))) return false;
248 // Then, make it shared again - effectively creating a new peer group, that
249 // will be inherited by new mount namespaces.
250 if (!(MakeShared("/mnt/installer"))) return false;
251
Jiyong Park68660412019-01-16 23:00:59 +0900252 bootstrap_ns_fd.reset(OpenMountNamespace());
253 bootstrap_ns_id = GetMountNamespaceId();
254
Jiyong Park7b4801a2019-02-25 16:41:38 +0900255 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900256 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900257 // activated by apexd. In the namespace for pre-apexd processes, small
258 // number of essential APEXes (e.g. com.android.runtime) are activated.
259 // In the namespace for post-apexd processes, all APEXes are activated.
Jiyong Park68660412019-01-16 23:00:59 +0900260 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900261 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900262 // Creating a new namespace by cloning, saving, and switching back to
263 // the original namespace.
264 if (unshare(CLONE_NEWNS) == -1) {
265 PLOG(ERROR) << "Cannot create mount namespace";
266 return false;
267 }
268 default_ns_fd.reset(OpenMountNamespace());
269 default_ns_id = GetMountNamespaceId();
270
Jiyong Park68660412019-01-16 23:00:59 +0900271 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
272 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
273 return false;
274 }
275 } else {
276 // Otherwise, default == bootstrap
277 default_ns_fd.reset(OpenMountNamespace());
278 default_ns_id = GetMountNamespaceId();
279 }
280
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900281 success &= ActivateFlattenedApexesIfPossible();
282
Jiyong Park68660412019-01-16 23:00:59 +0900283 LOG(INFO) << "SetupMountNamespaces done";
284 return success;
285}
286
287bool SwitchToDefaultMountNamespace() {
288 if (IsRecoveryMode()) {
289 // we don't have multiple namespaces in recovery mode
290 return true;
291 }
292 if (default_ns_id != GetMountNamespaceId()) {
293 if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) {
294 PLOG(ERROR) << "Failed to switch back to the default mount namespace.";
295 return false;
296 }
Kiyoung Kime4d3f212019-12-16 14:31:04 +0900297
298 if (auto result = MountLinkerConfigForDefaultNamespace(); !result) {
299 LOG(ERROR) << result.error();
300 return false;
301 }
Jiyong Park68660412019-01-16 23:00:59 +0900302 }
303
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900304 LOG(INFO) << "Switched to default mount namespace";
305 return true;
306}
307
Jiyong Park68660412019-01-16 23:00:59 +0900308bool SwitchToBootstrapMountNamespaceIfNeeded() {
309 if (IsRecoveryMode()) {
310 // we don't have multiple namespaces in recovery mode
311 return true;
312 }
313 if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 &&
Jiyong Park8502ed32019-02-25 22:18:37 +0900314 IsApexUpdatable()) {
Jiyong Park68660412019-01-16 23:00:59 +0900315 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
316 PLOG(ERROR) << "Failed to switch to bootstrap mount namespace.";
317 return false;
318 }
319 }
320 return true;
321}
322
323} // namespace init
324} // namespace android