fusermount: close inherited fds
authorMJ Harvey <mharvey@jumptrading.com>
Wed, 21 Aug 2024 12:05:20 +0000 (07:05 -0500)
committerBernd Schubert <bernd.schubert@fastmail.fm>
Mon, 16 Sep 2024 11:59:27 +0000 (13:59 +0200)
When using the auto_unmount option, the fusermount3
watchdog process retains any inherited file descriptors.
This PR ensures they are closed.

Reason is that FDs that are kept open for a long time might
cause issues for applications using libfuse, for example
if these expect a pipe close, but the pipe is kept open
through the inherited file descriptor.
See for example: https://github.com/cvmfs/cvmfs/issues/3645

Signed-off-by: MJ Harvey <mharvey@jumptrading.com>
Signed-off-by: Bernd Schubert <bschubert@ddn.com>
util/fusermount.c

index f37c616a7eeacf7f7b296d6b1a166c095c72a7f8..b8437a06d1b0a0b5c05c44a7d0357333ed1502b6 100644 (file)
@@ -1448,6 +1448,21 @@ static void show_version(void)
        exit(0);
 }
 
+/*
+ * Close all inherited fds that are not needed
+ * Ideally these wouldn't come up at all, applications should better
+ * use FD_CLOEXEC / O_CLOEXEC
+ */
+static void close_inherited_fds(int cfd)
+{
+       int max_fd = sysconf(_SC_OPEN_MAX);
+
+       for (int fd = 3; fd <= max_fd; fd++) {
+               if (fd != cfd)
+                       close(fd);
+       }
+}
+
 int main(int argc, char *argv[])
 {
        sigset_t sigset;
@@ -1599,8 +1614,11 @@ int main(int argc, char *argv[])
 wait_for_auto_unmount:
        /* Become a daemon and wait for the parent to exit or die.
           ie For the control socket to get closed.
-          btw We don't want to use daemon() function here because
+          Btw, we don't want to use daemon() function here because
           it forks and messes with the file descriptors. */
+
+       close_inherited_fds(cfd);
+
        setsid();
        res = chdir("/");
        if (res == -1) {