blob: 5305dc7ef9489b0a1328914516e9a2447d5cd974 [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
Jiyong Park68660412019-01-16 23:00:59 +090036static 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
48static 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
60static 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
68static 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
Jiyong Park8502ed32019-02-25 22:18:37 +090077static bool IsApexUpdatable() {
78 static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false);
79 return updatable;
Jiyong Park68660412019-01-16 23:00:59 +090080}
81
82static android::base::unique_fd bootstrap_ns_fd;
83static android::base::unique_fd default_ns_fd;
84
85static std::string bootstrap_ns_id;
86static std::string default_ns_id;
87
88} // namespace
89
90bool 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
Jiyong Park7b4801a2019-02-25 16:41:38 +090098 // /apex is a private mountpoint to give different sets of APEXes for
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +090099 // 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
Jiyong Park68660412019-01-16 23:00:59 +0900103 bootstrap_ns_fd.reset(OpenMountNamespace());
104 bootstrap_ns_id = GetMountNamespaceId();
105
Jiyong Park7b4801a2019-02-25 16:41:38 +0900106 // When APEXes are updatable (e.g. not-flattened), we create separate mount
Jiyong Park68660412019-01-16 23:00:59 +0900107 // namespaces for processes that are started before and after the APEX is
Jiyong Park7b4801a2019-02-25 16:41:38 +0900108 // 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.
Jiyong Park68660412019-01-16 23:00:59 +0900111 bool success = true;
Jiyong Park8502ed32019-02-25 22:18:37 +0900112 if (IsApexUpdatable() && !IsRecoveryMode()) {
Jiyong Park68660412019-01-16 23:00:59 +0900113 // 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
Jiyong Park68660412019-01-16 23:00:59 +0900122 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
136bool 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
Jiyong Parkdcbaf9f2019-02-22 22:15:25 +0900148 LOG(INFO) << "Switched to default mount namespace";
149 return true;
150}
151
Jiyong Park68660412019-01-16 23:00:59 +0900152bool 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 &&
Jiyong Park8502ed32019-02-25 22:18:37 +0900158 IsApexUpdatable()) {
Jiyong Park68660412019-01-16 23:00:59 +0900159 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