blob: 59cc140e42338a71dd00e7b6d3480ffdb37f82ff [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
Jooyung Han1d951b72020-07-05 03:33:36 +090033#ifndef RECOVERY
34#define ACTIVATE_FLATTENED_APEX 1
35#endif
36
37#ifdef ACTIVATE_FLATTENED_APEX
38#include <apex_manifest.pb.h>
39#include <com_android_apex.h>
40#include <selinux/android.h>
41#endif // ACTIVATE_FLATTENED_APEX
42
Jiyong Park68660412019-01-16 23:00:59 +090043namespace android {
44namespace init {
45namespace {
46
Elliott Hughese79b8c22020-07-28 11:09:03 -070047static bool BindMount(const std::string& source, const std::string& mount_point) {
48 if (mount(source.c_str(), mount_point.c_str(), nullptr, MS_BIND | MS_REC, nullptr) == -1) {
Martijn Coenenc70c0662020-01-10 15:42:15 +010049 PLOG(ERROR) << "Failed to bind mount " << source;
50 return false;
51 }
52 return true;
53}
54
Elliott Hughese79b8c22020-07-28 11:09:03 -070055static bool ChangeMount(const std::string& mount_point, unsigned long mountflags) {
Jiyong Park68660412019-01-16 23:00:59 +090056 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
Elliott Hughese79b8c22020-07-28 11:09:03 -070057 PLOG(ERROR) << "Failed to remount " << mount_point << " as " << std::hex << mountflags;
Jiyong Park68660412019-01-16 23:00:59 +090058 return false;
59 }
60 return true;
61}
62
63static int OpenMountNamespace() {
64 int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
65 if (fd < 0) {
66 PLOG(ERROR) << "Cannot open fd for current mount namespace";
67 }
68 return fd;
69}
70
71static std::string GetMountNamespaceId() {
72 std::string ret;
73 if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) {
74 PLOG(ERROR) << "Failed to read namespace ID";
75 return "";
76 }
77 return ret;
78}
79
Jiyong Park8502ed32019-02-25 22:18:37 +090080static bool IsApexUpdatable() {
81 static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false);
82 return updatable;
Jiyong Park68660412019-01-16 23:00:59 +090083}
84
Jooyung Han1d951b72020-07-05 03:33:36 +090085#ifdef ACTIVATE_FLATTENED_APEX
86
Jooyung Han5bb9d212019-11-25 13:50:44 +090087static Result<void> MountDir(const std::string& path, const std::string& mount_path) {
Satoshi Niwa1eb300d2020-01-20 18:00:49 +090088 if (int ret = mkdir(mount_path.c_str(), 0755); ret != 0 && errno != EEXIST) {
Jooyung Han5bb9d212019-11-25 13:50:44 +090089 return ErrnoError() << "Could not create mount point " << mount_path;
90 }
91 if (mount(path.c_str(), mount_path.c_str(), nullptr, MS_BIND, nullptr) != 0) {
92 return ErrnoError() << "Could not bind mount " << path << " to " << mount_path;
93 }
94 return {};
95}
96
Jooyung Han1d951b72020-07-05 03:33:36 +090097static Result<apex::proto::ApexManifest> GetApexManifest(const std::string& apex_dir) {
Jiyong Park648ae3a2019-12-08 00:25:15 +090098 const std::string manifest_path = apex_dir + "/apex_manifest.pb";
99 std::string content;
100 if (!android::base::ReadFileToString(manifest_path, &content)) {
101 return Error() << "Failed to read manifest file: " << manifest_path;
102 }
103 apex::proto::ApexManifest manifest;
104 if (!manifest.ParseFromString(content)) {
105 return Error() << "Can't parse manifest file: " << manifest_path;
106 }
Jooyung Han1d951b72020-07-05 03:33:36 +0900107 return manifest;
Jiyong Park648ae3a2019-12-08 00:25:15 +0900108}
109
Jooyung Han1d951b72020-07-05 03:33:36 +0900110template <typename Fn>
Jooyung Han5bb9d212019-11-25 13:50:44 +0900111static Result<void> ActivateFlattenedApexesFrom(const std::string& from_dir,
Jooyung Han1d951b72020-07-05 03:33:36 +0900112 const std::string& to_dir, Fn on_activate) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900113 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(from_dir.c_str()), closedir);
114 if (!dir) {
115 return {};
116 }
117 dirent* entry;
118 while ((entry = readdir(dir.get())) != nullptr) {
119 if (entry->d_name[0] == '.') continue;
120 if (entry->d_type == DT_DIR) {
121 const std::string apex_path = from_dir + "/" + entry->d_name;
Jooyung Han1d951b72020-07-05 03:33:36 +0900122 const auto apex_manifest = GetApexManifest(apex_path);
123 if (!apex_manifest.ok()) {
124 LOG(ERROR) << apex_path << " is not an APEX directory: " << apex_manifest.error();
Jiyong Park648ae3a2019-12-08 00:25:15 +0900125 continue;
126 }
Jooyung Han1d951b72020-07-05 03:33:36 +0900127 const std::string mount_path = to_dir + "/" + apex_manifest->name();
Bernie Innocenticecebbb2020-02-06 03:49:33 +0900128 if (auto result = MountDir(apex_path, mount_path); !result.ok()) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900129 return result;
130 }
Jooyung Han1d951b72020-07-05 03:33:36 +0900131 on_activate(apex_path, *apex_manifest);
Jooyung Han5bb9d212019-11-25 13:50:44 +0900132 }
133 }
134 return {};
135}
136
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900137static bool ActivateFlattenedApexesIfPossible() {
138 if (IsRecoveryMode() || IsApexUpdatable()) {
139 return true;
140 }
141
Jooyung Han5bb9d212019-11-25 13:50:44 +0900142 const std::string kApexTop = "/apex";
143 const std::vector<std::string> kBuiltinDirsForApexes = {
144 "/system/apex",
145 "/system_ext/apex",
146 "/product/apex",
147 "/vendor/apex",
148 };
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900149
Jooyung Han1d951b72020-07-05 03:33:36 +0900150 std::vector<com::android::apex::ApexInfo> apex_infos;
151 auto on_activate = [&](const std::string& apex_path,
152 const apex::proto::ApexManifest& apex_manifest) {
153 apex_infos.emplace_back(apex_manifest.name(), apex_path, apex_path, apex_manifest.version(),
154 apex_manifest.versionname(), /*isFactory=*/true, /*isActive=*/true);
155 };
156
Jooyung Han5bb9d212019-11-25 13:50:44 +0900157 for (const auto& dir : kBuiltinDirsForApexes) {
Jooyung Han1d951b72020-07-05 03:33:36 +0900158 if (auto result = ActivateFlattenedApexesFrom(dir, kApexTop, on_activate); !result.ok()) {
Jooyung Han5bb9d212019-11-25 13:50:44 +0900159 LOG(ERROR) << result.error();
160 return false;
161 }
162 }
Jooyung Han1d951b72020-07-05 03:33:36 +0900163
164 std::ostringstream oss;
165 com::android::apex::ApexInfoList apex_info_list(apex_infos);
166 com::android::apex::write(oss, apex_info_list);
167 const std::string kApexInfoList = kApexTop + "/apex-info-list.xml";
168 if (!android::base::WriteStringToFile(oss.str(), kApexInfoList)) {
169 PLOG(ERROR) << "Failed to write " << kApexInfoList;
170 return false;
171 }
172 if (selinux_android_restorecon(kApexInfoList.c_str(), 0) != 0) {
173 PLOG(ERROR) << "selinux_android_restorecon(" << kApexInfoList << ") failed";
174 }
175
Jiyong Park648ae3a2019-12-08 00:25:15 +0900176 return true;
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900177}
178
Jooyung Han1d951b72020-07-05 03:33:36 +0900179#endif // ACTIVATE_FLATTENED_APEX
180
Jiyong Park68660412019-01-16 23:00:59 +0900181static android::base::unique_fd bootstrap_ns_fd;
182static android::base::unique_fd default_ns_fd;
183
184static std::string bootstrap_ns_id;
185static std::string default_ns_id;
186
187} // namespace
188
189bool SetupMountNamespaces() {
190 // Set the propagation type of / as shared so that any mounting event (e.g.
191 // /data) is by default visible to all processes. When private mounting is
192 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
193 // bind-mounting by to itself) and set the propagation type of the mount
194 // point to private.
Elliott Hughese79b8c22020-07-28 11:09:03 -0700195 if (!ChangeMount("/", MS_SHARED | MS_REC)) return false;
Jiyong Park68660412019-01-16 23:00:59 +0900196
Jiyong Park7b4801a2019-02-25 16:41:38 +0900197 // /apex is a private mountpoint to give different sets of APEXes for
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900198 // the bootstrap and default mount namespaces. The processes running with
199 // the bootstrap namespace get APEXes from the read-only partition.
Elliott Hughese79b8c22020-07-28 11:09:03 -0700200 if (!(ChangeMount("/apex", MS_PRIVATE))) return false;
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900201
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900202 // /linkerconfig is a private mountpoint to give a different linker configuration
203 // based on the mount namespace. Subdirectory will be bind-mounted based on current mount
204 // namespace
Elliott Hughese79b8c22020-07-28 11:09:03 -0700205 if (!(ChangeMount("/linkerconfig", MS_PRIVATE))) return false;
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900206
Martijn Coenenc70c0662020-01-10 15:42:15 +0100207 // The two mount namespaces present challenges for scoped storage, because
208 // vold, which is responsible for most of the mounting, lives in the
209 // bootstrap mount namespace, whereas most other daemons and all apps live
210 // in the default namespace. Scoped storage has a need for a
211 // /mnt/installer view that is a slave bind mount of /mnt/user - in other
212 // words, all mounts under /mnt/user should automatically show up under
213 // /mnt/installer. However, additional mounts done under /mnt/installer
214 // should not propagate back to /mnt/user. In a single mount namespace
215 // this is easy to achieve, by simply marking the /mnt/installer a slave
216 // bind mount. Unfortunately, if /mnt/installer is only created and
217 // bind mounted after the two namespaces are created below, we end up
218 // with the following situation:
219 // /mnt/user and /mnt/installer share the same peer group in both the
220 // bootstrap and default namespaces. Marking /mnt/installer slave in either
221 // namespace means that it won't propagate events to the /mnt/installer in
222 // the other namespace, which is still something we require - vold is the
223 // one doing the mounting under /mnt/installer, and those mounts should
224 // show up in the default namespace as well.
225 //
226 // The simplest solution is to do the bind mount before the two namespaces
227 // are created: the effect is that in both namespaces, /mnt/installer is a
228 // slave to the /mnt/user mount, and at the same time /mnt/installer in the
229 // bootstrap namespace shares a peer group with /mnt/installer in the
230 // default namespace.
Ricky Waia4c163d2020-04-21 12:16:43 +0100231 // /mnt/androidwritable is similar to /mnt/installer but serves for
232 // MOUNT_EXTERNAL_ANDROID_WRITABLE apps.
Martijn Coenenc70c0662020-01-10 15:42:15 +0100233 if (!mkdir_recursive("/mnt/user", 0755)) return false;
234 if (!mkdir_recursive("/mnt/installer", 0755)) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100235 if (!mkdir_recursive("/mnt/androidwritable", 0755)) return false;
Elliott Hughese79b8c22020-07-28 11:09:03 -0700236 if (!(BindMount("/mnt/user", "/mnt/installer"))) return false;
237 if (!(BindMount("/mnt/user", "/mnt/androidwritable"))) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100238 // First, make /mnt/installer and /mnt/androidwritable a slave bind mount
Elliott Hughese79b8c22020-07-28 11:09:03 -0700239 if (!(ChangeMount("/mnt/installer", MS_SLAVE))) return false;
240 if (!(ChangeMount("/mnt/androidwritable", MS_SLAVE))) return false;
Martijn Coenenc70c0662020-01-10 15:42:15 +0100241 // Then, make it shared again - effectively creating a new peer group, that
242 // will be inherited by new mount namespaces.
Elliott Hughese79b8c22020-07-28 11:09:03 -0700243 if (!(ChangeMount("/mnt/installer", MS_SHARED))) return false;
244 if (!(ChangeMount("/mnt/androidwritable", MS_SHARED))) return false;
Martijn Coenenc70c0662020-01-10 15:42:15 +0100245
Jiyong Park68660412019-01-16 23:00:59 +0900246 bootstrap_ns_fd.reset(OpenMountNamespace());
247 bootstrap_ns_id = GetMountNamespaceId();
248
Jiyong Park7b4801a2019-02-25 16:41:38 +0900249 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900250 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900251 // activated by apexd. In the namespace for pre-apexd processes, small
252 // number of essential APEXes (e.g. com.android.runtime) are activated.
253 // In the namespace for post-apexd processes, all APEXes are activated.
Jiyong Park68660412019-01-16 23:00:59 +0900254 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900255 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900256 // Creating a new namespace by cloning, saving, and switching back to
257 // the original namespace.
258 if (unshare(CLONE_NEWNS) == -1) {
259 PLOG(ERROR) << "Cannot create mount namespace";
260 return false;
261 }
262 default_ns_fd.reset(OpenMountNamespace());
263 default_ns_id = GetMountNamespaceId();
264
Jiyong Park68660412019-01-16 23:00:59 +0900265 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
266 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
267 return false;
268 }
269 } else {
270 // Otherwise, default == bootstrap
271 default_ns_fd.reset(OpenMountNamespace());
272 default_ns_id = GetMountNamespaceId();
273 }
Jooyung Han1d951b72020-07-05 03:33:36 +0900274#ifdef ACTIVATE_FLATTENED_APEX
Jiyong Parkd7f7c202019-05-10 21:12:15 +0900275 success &= ActivateFlattenedApexesIfPossible();
Jooyung Han1d951b72020-07-05 03:33:36 +0900276#endif
Jiyong Park68660412019-01-16 23:00:59 +0900277 LOG(INFO) << "SetupMountNamespaces done";
278 return success;
279}
280
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900281Result<void> SwitchToMountNamespaceIfNeeded(MountNamespace target_mount_namespace) {
282 if (IsRecoveryMode() || !IsApexUpdatable()) {
283 // we don't have multiple namespaces in recovery mode or if apex is not updatable
284 return {};
Jiyong Park68660412019-01-16 23:00:59 +0900285 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900286 const auto& ns_id = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_id : default_ns_id;
287 const auto& ns_fd = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_fd : default_ns_fd;
288 const auto& ns_name = target_mount_namespace == NS_BOOTSTRAP ? "bootstrap" : "default";
289 if (ns_id != GetMountNamespaceId() && ns_fd.get() != -1) {
290 if (setns(ns_fd.get(), CLONE_NEWNS) == -1) {
291 return ErrnoError() << "Failed to switch to " << ns_name << " mount namespace.";
Kiyoung Kime4d3f212019-12-16 14:31:04 +0900292 }
Jiyong Park68660412019-01-16 23:00:59 +0900293 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900294 return {};
Jiyong Park68660412019-01-16 23:00:59 +0900295}
296
297} // namespace init
298} // namespace android