1 /*
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 <ApexProperties.sysprop.h>
25 #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
32 namespace android {
33 namespace init {
34 namespace {
35
MakeShared(const std::string & mount_point,bool recursive=false)36 static bool MakeShared(const std::string& mount_point, bool recursive = false) {
37 unsigned long mountflags = MS_SHARED;
38 if (recursive) {
39 mountflags |= MS_REC;
40 }
41 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
42 PLOG(ERROR) << "Failed to change propagation type to shared";
43 return false;
44 }
45 return true;
46 }
47
MakePrivate(const std::string & mount_point,bool recursive=false)48 static bool MakePrivate(const std::string& mount_point, bool recursive = false) {
49 unsigned long mountflags = MS_PRIVATE;
50 if (recursive) {
51 mountflags |= MS_REC;
52 }
53 if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
54 PLOG(ERROR) << "Failed to change propagation type to private";
55 return false;
56 }
57 return true;
58 }
59
OpenMountNamespace()60 static int OpenMountNamespace() {
61 int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC);
62 if (fd < 0) {
63 PLOG(ERROR) << "Cannot open fd for current mount namespace";
64 }
65 return fd;
66 }
67
GetMountNamespaceId()68 static std::string GetMountNamespaceId() {
69 std::string ret;
70 if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) {
71 PLOG(ERROR) << "Failed to read namespace ID";
72 return "";
73 }
74 return ret;
75 }
76
IsApexUpdatable()77 static bool IsApexUpdatable() {
78 static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false);
79 return updatable;
80 }
81
82 static android::base::unique_fd bootstrap_ns_fd;
83 static android::base::unique_fd default_ns_fd;
84
85 static std::string bootstrap_ns_id;
86 static std::string default_ns_id;
87
88 } // namespace
89
SetupMountNamespaces()90 bool SetupMountNamespaces() {
91 // Set the propagation type of / as shared so that any mounting event (e.g.
92 // /data) is by default visible to all processes. When private mounting is
93 // needed for /foo/bar, then we will make /foo/bar as a mount point (by
94 // bind-mounting by to itself) and set the propagation type of the mount
95 // point to private.
96 if (!MakeShared("/", true /*recursive*/)) return false;
97
98 // /apex is a private mountpoint to give different sets of APEXes for
99 // the bootstrap and default mount namespaces. The processes running with
100 // the bootstrap namespace get APEXes from the read-only partition.
101 if (!(MakePrivate("/apex"))) return false;
102
103 bootstrap_ns_fd.reset(OpenMountNamespace());
104 bootstrap_ns_id = GetMountNamespaceId();
105
106 // When APEXes are updatable (e.g. not-flattened), we create separate mount
107 // namespaces for processes that are started before and after the APEX is
108 // activated by apexd. In the namespace for pre-apexd processes, small
109 // number of essential APEXes (e.g. com.android.runtime) are activated.
110 // In the namespace for post-apexd processes, all APEXes are activated.
111 bool success = true;
112 if (IsApexUpdatable() && !IsRecoveryMode()) {
113 // Creating a new namespace by cloning, saving, and switching back to
114 // the original namespace.
115 if (unshare(CLONE_NEWNS) == -1) {
116 PLOG(ERROR) << "Cannot create mount namespace";
117 return false;
118 }
119 default_ns_fd.reset(OpenMountNamespace());
120 default_ns_id = GetMountNamespaceId();
121
122 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
123 PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace";
124 return false;
125 }
126 } else {
127 // Otherwise, default == bootstrap
128 default_ns_fd.reset(OpenMountNamespace());
129 default_ns_id = GetMountNamespaceId();
130 }
131
132 LOG(INFO) << "SetupMountNamespaces done";
133 return success;
134 }
135
SwitchToDefaultMountNamespace()136 bool SwitchToDefaultMountNamespace() {
137 if (IsRecoveryMode()) {
138 // we don't have multiple namespaces in recovery mode
139 return true;
140 }
141 if (default_ns_id != GetMountNamespaceId()) {
142 if (setns(default_ns_fd.get(), CLONE_NEWNS) == -1) {
143 PLOG(ERROR) << "Failed to switch back to the default mount namespace.";
144 return false;
145 }
146 }
147
148 LOG(INFO) << "Switched to default mount namespace";
149 return true;
150 }
151
SwitchToBootstrapMountNamespaceIfNeeded()152 bool SwitchToBootstrapMountNamespaceIfNeeded() {
153 if (IsRecoveryMode()) {
154 // we don't have multiple namespaces in recovery mode
155 return true;
156 }
157 if (bootstrap_ns_id != GetMountNamespaceId() && bootstrap_ns_fd.get() != -1 &&
158 IsApexUpdatable()) {
159 if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) {
160 PLOG(ERROR) << "Failed to switch to bootstrap mount namespace.";
161 return false;
162 }
163 }
164 return true;
165 }
166
167 } // namespace init
168 } // namespace android
169