blob: 93eb2440cdd43584d34a476d9de783d4c9937c72 [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
Jiyong Park68660412019-01-16 23:00:59 +090038static bool MakeShared(const std::string& mount_point, bool recursive = false) {
39 unsigned long mountflags = MS_SHARED;
40 if (recursive) {
41 mountflags |= MS_REC;
42 }
43 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
44 PLOG(ERROR) << "Failed to change propagation type to shared";
45 return false;
46 }
47 return true;
48}
49
50static bool MakePrivate(const std::string& mount_point, bool recursive = false) {
51 unsigned long mountflags = MS_PRIVATE;
52 if (recursive) {
53 mountflags |= MS_REC;
54 }
55 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
56 PLOG(ERROR) << "Failed to change propagation type to private";
57 return false;
58 }
59 return true;
60}
61
62static int OpenMountNamespace() {
63 int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
64 if (fd < 0) {
65 PLOG(ERROR) << "Cannot open fd for current mount namespace";
66 }
67 return fd;
68}
69
70static std::string GetMountNamespaceId() {
71 std::string ret;
72 if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) {
73 PLOG(ERROR) << "Failed to read namespace ID";
74 return "";
75 }
76 return ret;
77}
78
Jiyong Park8502ed32019-02-25 22:18:37 +090079static bool IsApexUpdatable() {
80 static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false);
81 return updatable;
Jiyong Park68660412019-01-16 23:00:59 +090082}
83
Jooyung Han5bb9d212019-11-25 13:50:44 +090084static Result<void> MountDir(const std::string& path, const std::string& mount_path) {
85 if (int ret = mkdir(mount_path.c_str(), 0755); ret != 0 && ret != EEXIST) {
86 return ErrnoError() << "Could not create mount point " << mount_path;
87 }
88 if (mount(path.c_str(), mount_path.c_str(), nullptr, MS_BIND, nullptr) != 0) {
89 return ErrnoError() << "Could not bind mount " << path << " to " << mount_path;
90 }
91 return {};
92}
93
Jiyong Park648ae3a2019-12-08 00:25:15 +090094static Result<std::string> GetApexName(const std::string& apex_dir) {
95 const std::string manifest_path = apex_dir + "/apex_manifest.pb";
96 std::string content;
97 if (!android::base::ReadFileToString(manifest_path, &content)) {
98 return Error() << "Failed to read manifest file: " << manifest_path;
99 }
100 apex::proto::ApexManifest manifest;
101 if (!manifest.ParseFromString(content)) {
102 return Error() << "Can't parse manifest file: " << manifest_path;
103 }
104 return manifest.name();
105}
106
Jooyung Han5bb9d212019-11-25 13:50:44 +0900107static Result<void> ActivateFlattenedApexesFrom(const std::string& from_dir,
108 const std::string& to_dir) {
109 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(from_dir.c_str()), closedir);
110 if (!dir) {
111 return {};
112 }
113 dirent* entry;
114 while ((entry = readdir(dir.get())) != nullptr) {
115 if (entry->d_name[0] == '.') continue;
116 if (entry->d_type == DT_DIR) {
117 const std::string apex_path = from_dir + "/" + entry->d_name;
Jiyong Park648ae3a2019-12-08 00:25:15 +0900118 const auto apex_name = GetApexName(apex_path);
119 if (!apex_name) {
120 LOG(ERROR) << apex_path << " is not an APEX directory: " << apex_name.error();
121 continue;
122 }
123 const std::string mount_path = to_dir + "/" + (*apex_name);
Jooyung Han5bb9d212019-11-25 13:50:44 +0900124 if (auto result = MountDir(apex_path, mount_path); !result) {
125 return result;
126 }
127 }
128 }
129 return {};
130}
131
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900132static bool ActivateFlattenedApexesIfPossible() {
133 if (IsRecoveryMode() || IsApexUpdatable()) {
134 return true;
135 }
136
Jooyung Han5bb9d212019-11-25 13:50:44 +0900137 const std::string kApexTop = "/apex";
138 const std::vector<std::string> kBuiltinDirsForApexes = {
139 "/system/apex",
140 "/system_ext/apex",
141 "/product/apex",
142 "/vendor/apex",
143 };
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900144
Jooyung Han5bb9d212019-11-25 13:50:44 +0900145 for (const auto& dir : kBuiltinDirsForApexes) {
146 if (auto result = ActivateFlattenedApexesFrom(dir, kApexTop); !result) {
147 LOG(ERROR) << result.error();
148 return false;
149 }
150 }
Jiyong Park648ae3a2019-12-08 00:25:15 +0900151 return true;
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900152}
153
Kiyoung Kime4d3f212019-12-16 14:31:04 +0900154static Result<void> MountLinkerConfigForDefaultNamespace() {
155 // No need to mount linkerconfig for default mount namespace if the path does not exist (which
156 // would mean it is already mounted)
157 if (access("/linkerconfig/default", 0) != 0) {
158 return {};
159 }
160
161 if (mount("/linkerconfig/default", "/linkerconfig", nullptr, MS_BIND | MS_REC, nullptr) != 0) {
162 return ErrnoError() << "Failed to mount linker configuration for default mount namespace.";
163 }
164
165 return {};
166}
167
Jiyong Park68660412019-01-16 23:00:59 +0900168static android::base::unique_fd bootstrap_ns_fd;
169static android::base::unique_fd default_ns_fd;
170
171static std::string bootstrap_ns_id;
172static std::string default_ns_id;
173
174} // namespace
175
176bool SetupMountNamespaces() {
177 // Set the propagation type of / as shared so that any mounting event (e.g.
178 // /data) is by default visible to all processes. When private mounting is
179 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
180 // bind-mounting by to itself) and set the propagation type of the mount
181 // point to private.
182 if (!MakeShared("/", true /*recursive*/)) return false;
183
Jiyong Park7b4801a2019-02-25 16:41:38 +0900184 // /apex is a private mountpoint to give different sets of APEXes for
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900185 // the bootstrap and default mount namespaces. The processes running with
186 // the bootstrap namespace get APEXes from the read-only partition.
187 if (!(MakePrivate("/apex"))) return false;
188
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900189 // /linkerconfig is a private mountpoint to give a different linker configuration
190 // based on the mount namespace. Subdirectory will be bind-mounted based on current mount
191 // namespace
192 if (!(MakePrivate("/linkerconfig"))) return false;
193
Jiyong Park68660412019-01-16 23:00:59 +0900194 bootstrap_ns_fd.reset(OpenMountNamespace());
195 bootstrap_ns_id = GetMountNamespaceId();
196
Jiyong Park7b4801a2019-02-25 16:41:38 +0900197 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900198 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900199 // activated by apexd. In the namespace for pre-apexd processes, small
200 // number of essential APEXes (e.g. com.android.runtime) are activated.
201 // In the namespace for post-apexd processes, all APEXes are activated.
Jiyong Park68660412019-01-16 23:00:59 +0900202 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900203 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900204 // Creating a new namespace by cloning, saving, and switching back to
205 // the original namespace.
206 if (unshare(CLONE_NEWNS) == -1) {
207 PLOG(ERROR) << "Cannot create mount namespace";
208 return false;
209 }
210 default_ns_fd.reset(OpenMountNamespace());
211 default_ns_id = GetMountNamespaceId();
212
Jiyong Park68660412019-01-16 23:00:59 +0900213 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
214 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
215 return false;
216 }
217 } else {
218 // Otherwise, default == bootstrap
219 default_ns_fd.reset(OpenMountNamespace());
220 default_ns_id = GetMountNamespaceId();
221 }
222
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900223 success &= ActivateFlattenedApexesIfPossible();
224
Jiyong Park68660412019-01-16 23:00:59 +0900225 LOG(INFO) << "SetupMountNamespaces done";
226 return success;
227}
228
229bool SwitchToDefaultMountNamespace() {
230 if (IsRecoveryMode()) {
231 // we don't have multiple namespaces in recovery mode
232 return true;
233 }
234 if (default_ns_id != GetMountNamespaceId()) {
235 if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) {
236 PLOG(ERROR) << "Failed to switch back to the default mount namespace.";
237 return false;
238 }
Kiyoung Kime4d3f212019-12-16 14:31:04 +0900239
240 if (auto result = MountLinkerConfigForDefaultNamespace(); !result) {
241 LOG(ERROR) << result.error();
242 return false;
243 }
Jiyong Park68660412019-01-16 23:00:59 +0900244 }
245
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900246 LOG(INFO) << "Switched to default mount namespace";
247 return true;
248}
249
Jiyong Park68660412019-01-16 23:00:59 +0900250bool SwitchToBootstrapMountNamespaceIfNeeded() {
251 if (IsRecoveryMode()) {
252 // we don't have multiple namespaces in recovery mode
253 return true;
254 }
255 if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 &&
Jiyong Park8502ed32019-02-25 22:18:37 +0900256 IsApexUpdatable()) {
Jiyong Park68660412019-01-16 23:00:59 +0900257 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
258 PLOG(ERROR) << "Failed to switch to bootstrap mount namespace.";
259 return false;
260 }
261 }
262 return true;
263}
264
265} // namespace init
266} // namespace android