blob: c33e0de7a7e2e084d30a44515586f5f4a2a204c5 [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>
30
31#include "util.h"
32
33namespace android {
34namespace init {
35namespace {
36
Jiyong Park68660412019-01-16 23:00:59 +090037static bool MakeShared(const std::string& mount_point, bool recursive = false) {
38 unsigned long mountflags = MS_SHARED;
39 if (recursive) {
40 mountflags |= MS_REC;
41 }
42 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
43 PLOG(ERROR) << "Failed to change propagation type to shared";
44 return false;
45 }
46 return true;
47}
48
49static bool MakePrivate(const std::string& mount_point, bool recursive = false) {
50 unsigned long mountflags = MS_PRIVATE;
51 if (recursive) {
52 mountflags |= MS_REC;
53 }
54 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
55 PLOG(ERROR) << "Failed to change propagation type to private";
56 return false;
57 }
58 return true;
59}
60
61static int OpenMountNamespace() {
62 int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
63 if (fd < 0) {
64 PLOG(ERROR) << "Cannot open fd for current mount namespace";
65 }
66 return fd;
67}
68
69static std::string GetMountNamespaceId() {
70 std::string ret;
71 if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) {
72 PLOG(ERROR) << "Failed to read namespace ID";
73 return "";
74 }
75 return ret;
76}
77
Jiyong Park8502ed32019-02-25 22:18:37 +090078static bool IsApexUpdatable() {
79 static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false);
80 return updatable;
Jiyong Park68660412019-01-16 23:00:59 +090081}
82
Jooyung Han5bb9d212019-11-25 13:50:44 +090083static Result<void> MountDir(const std::string& path, const std::string& mount_path) {
84 if (int ret = mkdir(mount_path.c_str(), 0755); ret != 0 && ret != EEXIST) {
85 return ErrnoError() << "Could not create mount point " << mount_path;
86 }
87 if (mount(path.c_str(), mount_path.c_str(), nullptr, MS_BIND, nullptr) != 0) {
88 return ErrnoError() << "Could not bind mount " << path << " to " << mount_path;
89 }
90 return {};
91}
92
93static Result<void> ActivateFlattenedApexesFrom(const std::string& from_dir,
94 const std::string& to_dir) {
95 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(from_dir.c_str()), closedir);
96 if (!dir) {
97 return {};
98 }
99 dirent* entry;
100 while ((entry = readdir(dir.get())) != nullptr) {
101 if (entry->d_name[0] == '.') continue;
102 if (entry->d_type == DT_DIR) {
103 const std::string apex_path = from_dir + "/" + entry->d_name;
104 const std::string mount_path = to_dir + "/" + entry->d_name;
105 if (auto result = MountDir(apex_path, mount_path); !result) {
106 return result;
107 }
108 }
109 }
110 return {};
111}
112
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900113static bool ActivateFlattenedApexesIfPossible() {
114 if (IsRecoveryMode() || IsApexUpdatable()) {
115 return true;
116 }
117
Jooyung Han5bb9d212019-11-25 13:50:44 +0900118 const std::string kApexTop = "/apex";
119 const std::vector<std::string> kBuiltinDirsForApexes = {
120 "/system/apex",
121 "/system_ext/apex",
122 "/product/apex",
123 "/vendor/apex",
124 };
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900125
Jooyung Han5bb9d212019-11-25 13:50:44 +0900126 for (const auto& dir : kBuiltinDirsForApexes) {
127 if (auto result = ActivateFlattenedApexesFrom(dir, kApexTop); !result) {
128 LOG(ERROR) << result.error();
129 return false;
130 }
131 }
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +0100132 // Special casing for the ART APEX
Jooyung Han5bb9d212019-11-25 13:50:44 +0900133 constexpr const char kArtApexMountPath[] = "/apex/com.android.art";
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +0100134 static const std::vector<std::string> kArtApexDirNames = {"com.android.art.release",
135 "com.android.art.debug"};
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900136 bool success = false;
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +0100137 for (const auto& name : kArtApexDirNames) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900138 std::string path = kApexTop + "/" + name;
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900139 if (access(path.c_str(), F_OK) == 0) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900140 if (auto result = MountDir(path, kArtApexMountPath); !result) {
141 LOG(ERROR) << result.error();
142 return false;
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900143 }
Jooyung Han5bb9d212019-11-25 13:50:44 +0900144 success = true;
145 break;
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900146 }
147 }
148 if (!success) {
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +0100149 PLOG(ERROR) << "Failed to bind mount the ART APEX to " << kArtApexMountPath;
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900150 }
151 return success;
152}
153
Jiyong Park68660412019-01-16 23:00:59 +0900154static android::base::unique_fd bootstrap_ns_fd;
155static android::base::unique_fd default_ns_fd;
156
157static std::string bootstrap_ns_id;
158static std::string default_ns_id;
159
160} // namespace
161
162bool SetupMountNamespaces() {
163 // Set the propagation type of / as shared so that any mounting event (e.g.
164 // /data) is by default visible to all processes. When private mounting is
165 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
166 // bind-mounting by to itself) and set the propagation type of the mount
167 // point to private.
168 if (!MakeShared("/", true /*recursive*/)) return false;
169
Jiyong Park7b4801a2019-02-25 16:41:38 +0900170 // /apex is a private mountpoint to give different sets of APEXes for
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900171 // the bootstrap and default mount namespaces. The processes running with
172 // the bootstrap namespace get APEXes from the read-only partition.
173 if (!(MakePrivate("/apex"))) return false;
174
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900175 // /linkerconfig is a private mountpoint to give a different linker configuration
176 // based on the mount namespace. Subdirectory will be bind-mounted based on current mount
177 // namespace
178 if (!(MakePrivate("/linkerconfig"))) return false;
179
Jiyong Park68660412019-01-16 23:00:59 +0900180 bootstrap_ns_fd.reset(OpenMountNamespace());
181 bootstrap_ns_id = GetMountNamespaceId();
182
Jiyong Park7b4801a2019-02-25 16:41:38 +0900183 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900184 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900185 // activated by apexd. In the namespace for pre-apexd processes, small
186 // number of essential APEXes (e.g. com.android.runtime) are activated.
187 // In the namespace for post-apexd processes, all APEXes are activated.
Jiyong Park68660412019-01-16 23:00:59 +0900188 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900189 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900190 // Creating a new namespace by cloning, saving, and switching back to
191 // the original namespace.
192 if (unshare(CLONE_NEWNS) == -1) {
193 PLOG(ERROR) << "Cannot create mount namespace";
194 return false;
195 }
196 default_ns_fd.reset(OpenMountNamespace());
197 default_ns_id = GetMountNamespaceId();
198
Jiyong Park68660412019-01-16 23:00:59 +0900199 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
200 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
201 return false;
202 }
203 } else {
204 // Otherwise, default == bootstrap
205 default_ns_fd.reset(OpenMountNamespace());
206 default_ns_id = GetMountNamespaceId();
207 }
208
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900209 success &= ActivateFlattenedApexesIfPossible();
210
Jiyong Park68660412019-01-16 23:00:59 +0900211 LOG(INFO) << "SetupMountNamespaces done";
212 return success;
213}
214
215bool SwitchToDefaultMountNamespace() {
216 if (IsRecoveryMode()) {
217 // we don't have multiple namespaces in recovery mode
218 return true;
219 }
220 if (default_ns_id != GetMountNamespaceId()) {
221 if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) {
222 PLOG(ERROR) << "Failed to switch back to the default mount namespace.";
223 return false;
224 }
225 }
226
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900227 LOG(INFO) << "Switched to default mount namespace";
228 return true;
229}
230
Jiyong Park68660412019-01-16 23:00:59 +0900231bool SwitchToBootstrapMountNamespaceIfNeeded() {
232 if (IsRecoveryMode()) {
233 // we don't have multiple namespaces in recovery mode
234 return true;
235 }
236 if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 &&
Jiyong Park8502ed32019-02-25 22:18:37 +0900237 IsApexUpdatable()) {
Jiyong Park68660412019-01-16 23:00:59 +0900238 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
239 PLOG(ERROR) << "Failed to switch to bootstrap mount namespace.";
240 return false;
241 }
242 }
243 return true;
244}
245
246} // namespace init
247} // namespace android