Skip to content

Commit e79b8c2

Browse files
committed
Cleanup for #inclusivefixit.
Test: treehugger Change-Id: I651689e2d59c017a9bde44251d95b57e594f0b5b
1 parent 5ce3499 commit e79b8c2

File tree

2 files changed

+19
-51
lines changed

2 files changed

+19
-51
lines changed

init/mount_namespace.cpp

+13-46
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,17 @@ namespace android {
4444
namespace init {
4545
namespace {
4646

47-
static bool BindMount(const std::string& source, const std::string& mount_point,
48-
bool recursive = false) {
49-
unsigned long mountflags = MS_BIND;
50-
if (recursive) {
51-
mountflags |= MS_REC;
52-
}
53-
if (mount(source.c_str(), mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
47+
static 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) {
5449
PLOG(ERROR) << "Failed to bind mount " << source;
5550
return false;
5651
}
5752
return true;
5853
}
5954

60-
static bool MakeShared(const std::string& mount_point, bool recursive = false) {
61-
unsigned long mountflags = MS_SHARED;
62-
if (recursive) {
63-
mountflags |= MS_REC;
64-
}
65-
if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
66-
PLOG(ERROR) << "Failed to change propagation type to shared";
67-
return false;
68-
}
69-
return true;
70-
}
71-
72-
static bool MakeSlave(const std::string& mount_point, bool recursive = false) {
73-
unsigned long mountflags = MS_SLAVE;
74-
if (recursive) {
75-
mountflags |= MS_REC;
76-
}
77-
if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
78-
PLOG(ERROR) << "Failed to change propagation type to slave";
79-
return false;
80-
}
81-
return true;
82-
}
83-
84-
static bool MakePrivate(const std::string& mount_point, bool recursive = false) {
85-
unsigned long mountflags = MS_PRIVATE;
86-
if (recursive) {
87-
mountflags |= MS_REC;
88-
}
55+
static bool ChangeMount(const std::string& mount_point, unsigned long mountflags) {
8956
if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) {
90-
PLOG(ERROR) << "Failed to change propagation type to private";
57+
PLOG(ERROR) << "Failed to remount " << mount_point << " as " << std::hex << mountflags;
9158
return false;
9259
}
9360
return true;
@@ -225,17 +192,17 @@ bool SetupMountNamespaces() {
225192
// needed for /foo/bar, then we will make /foo/bar as a mount point (by
226193
// bind-mounting by to itself) and set the propagation type of the mount
227194
// point to private.
228-
if (!MakeShared("/", true /*recursive*/)) return false;
195+
if (!ChangeMount("/", MS_SHARED | MS_REC)) return false;
229196

230197
// /apex is a private mountpoint to give different sets of APEXes for
231198
// the bootstrap and default mount namespaces. The processes running with
232199
// the bootstrap namespace get APEXes from the read-only partition.
233-
if (!(MakePrivate("/apex"))) return false;
200+
if (!(ChangeMount("/apex", MS_PRIVATE))) return false;
234201

235202
// /linkerconfig is a private mountpoint to give a different linker configuration
236203
// based on the mount namespace. Subdirectory will be bind-mounted based on current mount
237204
// namespace
238-
if (!(MakePrivate("/linkerconfig"))) return false;
205+
if (!(ChangeMount("/linkerconfig", MS_PRIVATE))) return false;
239206

240207
// The two mount namespaces present challenges for scoped storage, because
241208
// vold, which is responsible for most of the mounting, lives in the
@@ -266,15 +233,15 @@ bool SetupMountNamespaces() {
266233
if (!mkdir_recursive("/mnt/user", 0755)) return false;
267234
if (!mkdir_recursive("/mnt/installer", 0755)) return false;
268235
if (!mkdir_recursive("/mnt/androidwritable", 0755)) return false;
269-
if (!(BindMount("/mnt/user", "/mnt/installer", true))) return false;
270-
if (!(BindMount("/mnt/user", "/mnt/androidwritable", true))) return false;
236+
if (!(BindMount("/mnt/user", "/mnt/installer"))) return false;
237+
if (!(BindMount("/mnt/user", "/mnt/androidwritable"))) return false;
271238
// First, make /mnt/installer and /mnt/androidwritable a slave bind mount
272-
if (!(MakeSlave("/mnt/installer"))) return false;
273-
if (!(MakeSlave("/mnt/androidwritable"))) return false;
239+
if (!(ChangeMount("/mnt/installer", MS_SLAVE))) return false;
240+
if (!(ChangeMount("/mnt/androidwritable", MS_SLAVE))) return false;
274241
// Then, make it shared again - effectively creating a new peer group, that
275242
// will be inherited by new mount namespaces.
276-
if (!(MakeShared("/mnt/installer"))) return false;
277-
if (!(MakeShared("/mnt/androidwritable"))) return false;
243+
if (!(ChangeMount("/mnt/installer", MS_SHARED))) return false;
244+
if (!(ChangeMount("/mnt/androidwritable", MS_SHARED))) return false;
278245

279246
bootstrap_ns_fd.reset(OpenMountNamespace());
280247
bootstrap_ns_id = GetMountNamespaceId();

init/service_utils.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ Result<void> EnterNamespace(int nstype, const char* path) {
6060
Result<void> SetUpMountNamespace(bool remount_proc, bool remount_sys) {
6161
constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
6262

63-
// Recursively remount / as slave like zygote does so unmounting and mounting /proc
64-
// doesn't interfere with the parent namespace's /proc mount. This will also
65-
// prevent any other mounts/unmounts initiated by the service from interfering
66-
// with the parent namespace but will still allow mount events from the parent
63+
// Recursively remount / as MS_SLAVE like zygote does so that
64+
// unmounting and mounting /proc doesn't interfere with the parent
65+
// namespace's /proc mount. This will also prevent any other
66+
// mounts/unmounts initiated by the service from interfering with the
67+
// parent namespace but will still allow mount events from the parent
6768
// namespace to propagate to the child.
6869
if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
69-
return ErrnoError() << "Could not remount(/) recursively as slave";
70+
return ErrnoError() << "Could not remount(/) recursively as MS_SLAVE";
7071
}
7172

7273
// umount() then mount() /proc and/or /sys

0 commit comments

Comments
 (0)