blob: 940fb6b5dbc004c852119376a0848895da59eb28 [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
Jiyong Park68660412019-01-16 23:00:59 +0900175 bootstrap_ns_fd.reset(OpenMountNamespace());
176 bootstrap_ns_id = GetMountNamespaceId();
177
Jiyong Park7b4801a2019-02-25 16:41:38 +0900178 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900179 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900180 // activated by apexd. In the namespace for pre-apexd processes, small
181 // number of essential APEXes (e.g. com.android.runtime) are activated.
182 // In the namespace for post-apexd processes, all APEXes are activated.
Jiyong Park68660412019-01-16 23:00:59 +0900183 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900184 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900185 // Creating a new namespace by cloning, saving, and switching back to
186 // the original namespace.
187 if (unshare(CLONE_NEWNS) == -1) {
188 PLOG(ERROR) << "Cannot create mount namespace";
189 return false;
190 }
191 default_ns_fd.reset(OpenMountNamespace());
192 default_ns_id = GetMountNamespaceId();
193
Jiyong Park68660412019-01-16 23:00:59 +0900194 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
195 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
196 return false;
197 }
198 } else {
199 // Otherwise, default == bootstrap
200 default_ns_fd.reset(OpenMountNamespace());
201 default_ns_id = GetMountNamespaceId();
202 }
203
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900204 success &= ActivateFlattenedApexesIfPossible();
205
Jiyong Park68660412019-01-16 23:00:59 +0900206 LOG(INFO) << "SetupMountNamespaces done";
207 return success;
208}
209
210bool SwitchToDefaultMountNamespace() {
211 if (IsRecoveryMode()) {
212 // we don't have multiple namespaces in recovery mode
213 return true;
214 }
215 if (default_ns_id != GetMountNamespaceId()) {
216 if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) {
217 PLOG(ERROR) << "Failed to switch back to the default mount namespace.";
218 return false;
219 }
220 }
221
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900222 LOG(INFO) << "Switched to default mount namespace";
223 return true;
224}
225
Jiyong Park68660412019-01-16 23:00:59 +0900226bool SwitchToBootstrapMountNamespaceIfNeeded() {
227 if (IsRecoveryMode()) {
228 // we don't have multiple namespaces in recovery mode
229 return true;
230 }
231 if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 &&
Jiyong Park8502ed32019-02-25 22:18:37 +0900232 IsApexUpdatable()) {
Jiyong Park68660412019-01-16 23:00:59 +0900233 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
234 PLOG(ERROR) << "Failed to switch to bootstrap mount namespace.";
235 return false;
236 }
237 }
238 return true;
239}
240
241} // namespace init
242} // namespace android