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