blob: 593e5ae850c50dcbbc0c9c19056aca4339b704dd [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 Han653b0632021-07-29 17:11:23 +090085static bool IsMicrodroid() {
86 static bool is_microdroid = android::base::GetProperty("ro.hardware", "") == "microdroid";
87 return is_microdroid;
88}
89
90// In case we have two sets of APEXes (non-updatable, updatable), we need two separate mount
91// namespaces.
92static bool NeedsTwoMountNamespaces() {
93 if (!IsApexUpdatable()) return false;
94 if (IsRecoveryMode()) return false;
95 // In microdroid, there's only one set of APEXes in built-in directories include block devices.
96 if (IsMicrodroid()) return false;
97 return true;
98}
99
Jiyong Park68660412019-01-16 23:00:59 +0900100static android::base::unique_fd bootstrap_ns_fd;
101static android::base::unique_fd default_ns_fd;
102
103static std::string bootstrap_ns_id;
104static std::string default_ns_id;
105
106} // namespace
107
108bool SetupMountNamespaces() {
109 // Set the propagation type of / as shared so that any mounting event (e.g.
110 // /data) is by default visible to all processes. When private mounting is
111 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
112 // bind-mounting by to itself) and set the propagation type of the mount
113 // point to private.
Elliott Hughese79b8c22020-07-28 11:09:03 -0700114 if (!ChangeMount("/", MS_SHARED | MS_REC)) return false;
Jiyong Park68660412019-01-16 23:00:59 +0900115
Jiyong Park7b4801a2019-02-25 16:41:38 +0900116 // /apex is a private mountpoint to give different sets of APEXes for
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900117 // the bootstrap and default mount namespaces. The processes running with
118 // the bootstrap namespace get APEXes from the read-only partition.
Elliott Hughese79b8c22020-07-28 11:09:03 -0700119 if (!(ChangeMount("/apex", MS_PRIVATE))) return false;
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900120
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900121 // /linkerconfig is a private mountpoint to give a different linker configuration
122 // based on the mount namespace. Subdirectory will be bind-mounted based on current mount
123 // namespace
Elliott Hughese79b8c22020-07-28 11:09:03 -0700124 if (!(ChangeMount("/linkerconfig", MS_PRIVATE))) return false;
Kiyoung Kim99df54b2019-11-22 16:14:10 +0900125
Martijn Coenenc70c0662020-01-10 15:42:15 +0100126 // The two mount namespaces present challenges for scoped storage, because
127 // vold, which is responsible for most of the mounting, lives in the
128 // bootstrap mount namespace, whereas most other daemons and all apps live
129 // in the default namespace. Scoped storage has a need for a
130 // /mnt/installer view that is a slave bind mount of /mnt/user - in other
131 // words, all mounts under /mnt/user should automatically show up under
132 // /mnt/installer. However, additional mounts done under /mnt/installer
133 // should not propagate back to /mnt/user. In a single mount namespace
134 // this is easy to achieve, by simply marking the /mnt/installer a slave
135 // bind mount. Unfortunately, if /mnt/installer is only created and
136 // bind mounted after the two namespaces are created below, we end up
137 // with the following situation:
138 // /mnt/user and /mnt/installer share the same peer group in both the
139 // bootstrap and default namespaces. Marking /mnt/installer slave in either
140 // namespace means that it won't propagate events to the /mnt/installer in
141 // the other namespace, which is still something we require - vold is the
142 // one doing the mounting under /mnt/installer, and those mounts should
143 // show up in the default namespace as well.
144 //
145 // The simplest solution is to do the bind mount before the two namespaces
146 // are created: the effect is that in both namespaces, /mnt/installer is a
147 // slave to the /mnt/user mount, and at the same time /mnt/installer in the
148 // bootstrap namespace shares a peer group with /mnt/installer in the
149 // default namespace.
Ricky Waia4c163d2020-04-21 12:16:43 +0100150 // /mnt/androidwritable is similar to /mnt/installer but serves for
151 // MOUNT_EXTERNAL_ANDROID_WRITABLE apps.
Martijn Coenenc70c0662020-01-10 15:42:15 +0100152 if (!mkdir_recursive("/mnt/user", 0755)) return false;
153 if (!mkdir_recursive("/mnt/installer", 0755)) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100154 if (!mkdir_recursive("/mnt/androidwritable", 0755)) return false;
Elliott Hughese79b8c22020-07-28 11:09:03 -0700155 if (!(BindMount("/mnt/user", "/mnt/installer"))) return false;
156 if (!(BindMount("/mnt/user", "/mnt/androidwritable"))) return false;
Ricky Waia4c163d2020-04-21 12:16:43 +0100157 // First, make /mnt/installer and /mnt/androidwritable a slave bind mount
Elliott Hughese79b8c22020-07-28 11:09:03 -0700158 if (!(ChangeMount("/mnt/installer", MS_SLAVE))) return false;
159 if (!(ChangeMount("/mnt/androidwritable", MS_SLAVE))) return false;
Martijn Coenenc70c0662020-01-10 15:42:15 +0100160 // Then, make it shared again - effectively creating a new peer group, that
161 // will be inherited by new mount namespaces.
Elliott Hughese79b8c22020-07-28 11:09:03 -0700162 if (!(ChangeMount("/mnt/installer", MS_SHARED))) return false;
163 if (!(ChangeMount("/mnt/androidwritable", MS_SHARED))) return false;
Martijn Coenenc70c0662020-01-10 15:42:15 +0100164
Jiyong Park68660412019-01-16 23:00:59 +0900165 bootstrap_ns_fd.reset(OpenMountNamespace());
166 bootstrap_ns_id = GetMountNamespaceId();
167
Jiyong Park7b4801a2019-02-25 16:41:38 +0900168 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900169 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900170 // activated by apexd. In the namespace for pre-apexd processes, small
171 // number of essential APEXes (e.g. com.android.runtime) are activated.
172 // In the namespace for post-apexd processes, all APEXes are activated.
Jiyong Park68660412019-01-16 23:00:59 +0900173 bool success = true;
Jooyung Han653b0632021-07-29 17:11:23 +0900174 if (NeedsTwoMountNamespaces()) {
Jiyong Park68660412019-01-16 23:00:59 +0900175 // Creating a new namespace by cloning, saving, and switching back to
176 // the original namespace.
177 if (unshare(CLONE_NEWNS) == -1) {
178 PLOG(ERROR) << "Cannot create mount namespace";
179 return false;
180 }
181 default_ns_fd.reset(OpenMountNamespace());
182 default_ns_id = GetMountNamespaceId();
183
Jiyong Park68660412019-01-16 23:00:59 +0900184 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
185 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
186 return false;
187 }
188 } else {
189 // Otherwise, default == bootstrap
190 default_ns_fd.reset(OpenMountNamespace());
191 default_ns_id = GetMountNamespaceId();
192 }
Shikha Malhotra720694d2021-07-30 12:35:27 +0000193
Jiyong Park68660412019-01-16 23:00:59 +0900194 LOG(INFO) << "SetupMountNamespaces done";
195 return success;
196}
197
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900198Result<void> SwitchToMountNamespaceIfNeeded(MountNamespace target_mount_namespace) {
199 if (IsRecoveryMode() || !IsApexUpdatable()) {
200 // we don't have multiple namespaces in recovery mode or if apex is not updatable
201 return {};
Jiyong Park68660412019-01-16 23:00:59 +0900202 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900203 const auto& ns_id = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_id : default_ns_id;
204 const auto& ns_fd = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_fd : default_ns_fd;
205 const auto& ns_name = target_mount_namespace == NS_BOOTSTRAP ? "bootstrap" : "default";
206 if (ns_id != GetMountNamespaceId() && ns_fd.get() != -1) {
207 if (setns(ns_fd.get(), CLONE_NEWNS) == -1) {
208 return ErrnoError() << "Failed to switch to " << ns_name << " mount namespace.";
Kiyoung Kime4d3f212019-12-16 14:31:04 +0900209 }
Jiyong Park68660412019-01-16 23:00:59 +0900210 }
Jooyung Han4f23d5a2020-06-09 13:44:17 +0900211 return {};
Jiyong Park68660412019-01-16 23:00:59 +0900212}
213
Kiyoung Kim0cbee0d2021-03-02 16:45:27 +0900214base::Result<MountNamespace> GetCurrentMountNamespace() {
215 std::string current_namespace_id = GetMountNamespaceId();
216 if (current_namespace_id == "") {
217 return Error() << "Failed to get current mount namespace ID";
218 }
219
220 if (current_namespace_id == bootstrap_ns_id) {
221 return NS_BOOTSTRAP;
222 } else if (current_namespace_id == default_ns_id) {
223 return NS_DEFAULT;
224 }
225
226 return Error() << "Failed to find current mount namespace";
227}
228
Jiyong Park68660412019-01-16 23:00:59 +0900229} // namespace init
230} // namespace android