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