4bff0a
From 4bc21bbc61acd1ce114da381a9742f6bcd4ffde8 Mon Sep 17 00:00:00 2001
4bff0a
From: Lennart Poettering <lennart@poettering.net>
4bff0a
Date: Wed, 17 Jul 2019 18:57:13 +0200
4bff0a
Subject: [PATCH] mount: rescan /proc/self/mountinfo before processing waitid()
4bff0a
 results
4bff0a
4bff0a
(The interesting bits about the what and why are in a comment in the
4bff0a
patch, please have a look there instead of looking here in the commit
4bff0a
msg).
4bff0a
4bff0a
Fixes: #10872
4bff0a
(cherry picked from commit 350804867dbcc9b7ccabae1187d730d37e2d8a21)
4bff0a
4bff0a
Conflicts:
4bff0a
	src/core/mount.c
4bff0a
4bff0a
Resolves: #1696178
4bff0a
---
4bff0a
 src/core/mount.c | 30 +++++++++++++++++++++++++++---
4bff0a
 1 file changed, 27 insertions(+), 3 deletions(-)
4bff0a
4bff0a
diff --git a/src/core/mount.c b/src/core/mount.c
4bff0a
index 85b07375e2..2ac04e3874 100644
4bff0a
--- a/src/core/mount.c
4bff0a
+++ b/src/core/mount.c
4bff0a
@@ -53,6 +53,7 @@ static const UnitActiveState state_translation_table[_MOUNT_STATE_MAX] = {
4bff0a
 
4bff0a
 static int mount_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
4bff0a
 static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
4bff0a
+static int mount_process_proc_self_mountinfo(Manager *m);
4bff0a
 
4bff0a
 static bool MOUNT_STATE_WITH_PROCESS(MountState state) {
4bff0a
         return IN_SET(state,
4bff0a
@@ -1241,6 +1242,22 @@ static void mount_sigchld_event(Unit *u, pid_t pid, int code, int status) {
4bff0a
         if (pid != m->control_pid)
4bff0a
                 return;
4bff0a
 
4bff0a
+        /* So here's the thing, we really want to know before /usr/bin/mount or /usr/bin/umount exit whether
4bff0a
+         * they established/remove a mount. This is important when mounting, but even more so when unmounting
4bff0a
+         * since we need to deal with nested mounts and otherwise cannot safely determine whether to repeat
4bff0a
+         * the unmounts. In theory, the kernel fires /proc/self/mountinfo changes off before returning from
4bff0a
+         * the mount() or umount() syscalls, and thus we should see the changes to the proc file before we
4bff0a
+         * process the waitid() for the /usr/bin/(u)mount processes. However, this is unfortunately racy: we
4bff0a
+         * have to waitid() for processes using P_ALL (since we need to reap unexpected children that got
4bff0a
+         * reparented to PID 1), but when using P_ALL we might end up reaping processes that terminated just
4bff0a
+         * instants ago, i.e. already after our last event loop iteration (i.e. after the last point we might
4bff0a
+         * have noticed /proc/self/mountinfo events via epoll). This means event loop priorities for
4bff0a
+         * processing SIGCHLD vs. /proc/self/mountinfo IO events are not as relevant as we want. To fix that
4bff0a
+         * race, let's explicitly scan /proc/self/mountinfo before we start processing /usr/bin/(u)mount
4bff0a
+         * dying. It's ugly, but it makes our ordering systematic again, and makes sure we always see
4bff0a
+         * /proc/self/mountinfo changes before our mount/umount exits. */
4bff0a
+        (void) mount_process_proc_self_mountinfo(u->manager);
4bff0a
+
4bff0a
         m->control_pid = 0;
4bff0a
 
4bff0a
         if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
4bff0a
@@ -1781,16 +1798,14 @@ static int drain_libmount(Manager *m) {
4bff0a
         return rescan;
4bff0a
 }
4bff0a
 
4bff0a
-static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
4bff0a
+static int mount_process_proc_self_mountinfo(Manager *m) {
4bff0a
         _cleanup_set_free_ Set *around = NULL, *gone = NULL;
4bff0a
-        Manager *m = userdata;
4bff0a
         const char *what;
4bff0a
         Iterator i;
4bff0a
         Unit *u;
4bff0a
         int r;
4bff0a
 
4bff0a
         assert(m);
4bff0a
-        assert(revents & EPOLLIN);
4bff0a
 
4bff0a
         r = drain_libmount(m);
4bff0a
         if (r <= 0)
4bff0a
@@ -1898,6 +1913,15 @@ static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
4bff0a
         return 0;
4bff0a
 }
4bff0a
 
4bff0a
+static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
4bff0a
+        Manager *m = userdata;
4bff0a
+
4bff0a
+        assert(m);
4bff0a
+        assert(revents & EPOLLIN);
4bff0a
+
4bff0a
+        return mount_process_proc_self_mountinfo(m);
4bff0a
+}
4bff0a
+
4bff0a
 static void mount_reset_failed(Unit *u) {
4bff0a
         Mount *m = MOUNT(u);
4bff0a