blob: 4161df22b9f96dbe819250a18689b8ad63c92bf2 [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>
28#include <android-base/unique_fd.h>
29
30#include "util.h"
31
32namespace android {
33namespace init {
34namespace {
35
36static constexpr const char* kLinkerMountPoint = "/bionic/bin/linker";
37static constexpr const char* kBootstrapLinkerPath = "/system/bin/bootstrap/linker";
38static constexpr const char* kRuntimeLinkerPath = "/apex/com.android.runtime/bin/linker";
39
40static constexpr const char* kBionicLibsMountPointDir = "/bionic/lib/";
41static constexpr const char* kBootstrapBionicLibsDir = "/system/lib/bootstrap/";
42static constexpr const char* kRuntimeBionicLibsDir = "/apex/com.android.runtime/lib/bionic/";
43
44static constexpr const char* kLinkerMountPoint64 = "/bionic/bin/linker64";
45static constexpr const char* kBootstrapLinkerPath64 = "/system/bin/bootstrap/linker64";
46static constexpr const char* kRuntimeLinkerPath64 = "/apex/com.android.runtime/bin/linker64";
47
48static constexpr const char* kBionicLibsMountPointDir64 = "/bionic/lib64/";
49static constexpr const char* kBootstrapBionicLibsDir64 = "/system/lib64/bootstrap/";
50static constexpr const char* kRuntimeBionicLibsDir64 = "/apex/com.android.runtime/lib64/bionic/";
51
52static const std::vector<std::string> kBionicLibFileNames = {"libc.so", "libm.so", "libdl.so"};
53
54static bool BindMount(const std::string& source, const std::string& mount_point,
55 bool recursive = false) {
56 unsigned long mountflags = MS_BIND;
57 if (recursive) {
58 mountflags |= MS_REC;
59 }
60 if (mount(source.c_str(), mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
61 PLOG(ERROR) << "Could not bind-mount " << source << " to " << mount_point;
62 return false;
63 }
64 return true;
65}
66
67static bool MakeShared(const std::string& mount_point, bool recursive = false) {
68 unsigned long mountflags = MS_SHARED;
69 if (recursive) {
70 mountflags |= MS_REC;
71 }
72 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
73 PLOG(ERROR) << "Failed to change propagation type to shared";
74 return false;
75 }
76 return true;
77}
78
79static bool MakePrivate(const std::string& mount_point, bool recursive = false) {
80 unsigned long mountflags = MS_PRIVATE;
81 if (recursive) {
82 mountflags |= MS_REC;
83 }
84 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
85 PLOG(ERROR) << "Failed to change propagation type to private";
86 return false;
87 }
88 return true;
89}
90
91static int OpenMountNamespace() {
92 int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
93 if (fd < 0) {
94 PLOG(ERROR) << "Cannot open fd for current mount namespace";
95 }
96 return fd;
97}
98
99static std::string GetMountNamespaceId() {
100 std::string ret;
101 if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) {
102 PLOG(ERROR) << "Failed to read namespace ID";
103 return "";
104 }
105 return ret;
106}
107
108static bool BindMountBionic(const std::string& linker_source, const std::string& lib_dir_source,
109 const std::string& linker_mount_point,
110 const std::string& lib_mount_dir) {
111 if (access(linker_source.c_str(), F_OK) != 0) {
112 PLOG(INFO) << linker_source << " does not exist. skipping mounting bionic there.";
113 // This can happen for 64-bit bionic in 32-bit only device.
114 // It is okay to skip mounting the 64-bit bionic.
115 return true;
116 }
117 if (!BindMount(linker_source, linker_mount_point)) {
118 return false;
119 }
120 if (!MakePrivate(linker_mount_point)) {
121 return false;
122 }
123 for (const auto& libname : kBionicLibFileNames) {
124 std::string mount_point = lib_mount_dir + libname;
125 std::string source = lib_dir_source + libname;
126 if (!BindMount(source, mount_point)) {
127 return false;
128 }
129 if (!MakePrivate(mount_point)) {
130 return false;
131 }
132 }
133 return true;
134}
135
Jiyong Park8502ed32019-02-25 22:18:37 +0900136static bool IsApexUpdatable() {
137 static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false);
138 return updatable;
Jiyong Park68660412019-01-16 23:00:59 +0900139}
140
141static android::base::unique_fd bootstrap_ns_fd;
142static android::base::unique_fd default_ns_fd;
143
144static std::string bootstrap_ns_id;
145static std::string default_ns_id;
146
147} // namespace
148
149bool SetupMountNamespaces() {
150 // Set the propagation type of / as shared so that any mounting event (e.g.
151 // /data) is by default visible to all processes. When private mounting is
152 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
153 // bind-mounting by to itself) and set the propagation type of the mount
154 // point to private.
155 if (!MakeShared("/", true /*recursive*/)) return false;
156
157 // Since different files (bootstrap or runtime APEX) should be mounted to
158 // the same mount point paths (e.g. /bionic/bin/linker, /bionic/lib/libc.so,
159 // etc.) across the two mount namespaces, we create a private mount point at
160 // /bionic so that a mount event for the bootstrap bionic in the mount
161 // namespace for pre-apexd processes is not propagated to the other mount
162 // namespace for post-apexd process, and vice versa.
163 //
164 // Other mount points other than /bionic, however, are all still shared.
165 if (!BindMount("/bionic", "/bionic", true /*recursive*/)) return false;
166 if (!MakePrivate("/bionic")) return false;
167
168 // Bind-mount bootstrap bionic.
169 if (!BindMountBionic(kBootstrapLinkerPath, kBootstrapBionicLibsDir, kLinkerMountPoint,
170 kBionicLibsMountPointDir))
171 return false;
172 if (!BindMountBionic(kBootstrapLinkerPath64, kBootstrapBionicLibsDir64, kLinkerMountPoint64,
173 kBionicLibsMountPointDir64))
174 return false;
175
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900176 // /apex is also a private mountpoint to give different sets of APEXes for
177 // the bootstrap and default mount namespaces. The processes running with
178 // the bootstrap namespace get APEXes from the read-only partition.
179 if (!(MakePrivate("/apex"))) return false;
180
Jiyong Park68660412019-01-16 23:00:59 +0900181 bootstrap_ns_fd.reset(OpenMountNamespace());
182 bootstrap_ns_id = GetMountNamespaceId();
183
184 // When bionic is updatable via the runtime APEX, we create separate mount
185 // namespaces for processes that are started before and after the APEX is
186 // activated by apexd. In the namespace for pre-apexd processes, the bionic
187 // from the /system partition (that we call bootstrap bionic) is
188 // bind-mounted. In the namespace for post-apexd processes, the bionic from
189 // the runtime APEX is bind-mounted.
190 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900191 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900192 // Creating a new namespace by cloning, saving, and switching back to
193 // the original namespace.
194 if (unshare(CLONE_NEWNS) == -1) {
195 PLOG(ERROR) << "Cannot create mount namespace";
196 return false;
197 }
198 default_ns_fd.reset(OpenMountNamespace());
199 default_ns_id = GetMountNamespaceId();
200
201 // By this unmount, the bootstrap bionic are not mounted in the default
202 // mount namespace.
203 if (umount2("/bionic", MNT_DETACH) == -1) {
204 PLOG(ERROR) << "Cannot unmount /bionic";
205 // Don't return here. We have to switch back to the bootstrap
206 // namespace.
207 success = false;
208 }
209
210 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
211 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
212 return false;
213 }
214 } else {
215 // Otherwise, default == bootstrap
216 default_ns_fd.reset(OpenMountNamespace());
217 default_ns_id = GetMountNamespaceId();
218 }
219
220 LOG(INFO) << "SetupMountNamespaces done";
221 return success;
222}
223
224bool SwitchToDefaultMountNamespace() {
225 if (IsRecoveryMode()) {
226 // we don't have multiple namespaces in recovery mode
227 return true;
228 }
229 if (default_ns_id != GetMountNamespaceId()) {
230 if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) {
231 PLOG(ERROR) << "Failed to switch back to the default mount namespace.";
232 return false;
233 }
234 }
235
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900236 LOG(INFO) << "Switched to default mount namespace";
237 return true;
238}
239
240// TODO(jiyong): remove this when /system/lib/libc.so becomes
241// a symlink to /apex/com.android.runtime/lib/bionic/libc.so
242bool SetupRuntimeBionic() {
243 if (IsRecoveryMode()) {
244 // We don't have multiple namespaces in recovery mode
245 return true;
246 }
Jiyong Park68660412019-01-16 23:00:59 +0900247 // Bind-mount bionic from the runtime APEX since it is now available. Note
Jiyong Park8502ed32019-02-25 22:18:37 +0900248 // that in case of IsApexUpdatable() == false, these mounts are over the
Jiyong Park68660412019-01-16 23:00:59 +0900249 // existing existing bind mounts for the bootstrap bionic, which effectively
250 // becomes hidden.
251 if (!BindMountBionic(kRuntimeLinkerPath, kRuntimeBionicLibsDir, kLinkerMountPoint,
252 kBionicLibsMountPointDir))
253 return false;
254 if (!BindMountBionic(kRuntimeLinkerPath64, kRuntimeBionicLibsDir64, kLinkerMountPoint64,
255 kBionicLibsMountPointDir64))
256 return false;
257
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900258 LOG(INFO) << "Runtime bionic is set up";
Jiyong Park68660412019-01-16 23:00:59 +0900259 return true;
260}
261
262bool SwitchToBootstrapMountNamespaceIfNeeded() {
263 if (IsRecoveryMode()) {
264 // we don't have multiple namespaces in recovery mode
265 return true;
266 }
267 if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 &&
Jiyong Park8502ed32019-02-25 22:18:37 +0900268 IsApexUpdatable()) {
Jiyong Park68660412019-01-16 23:00:59 +0900269 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
270 PLOG(ERROR) << "Failed to switch to bootstrap mount namespace.";
271 return false;
272 }
273 }
274 return true;
275}
276
277} // namespace init
278} // namespace android