587dbb
From a879d08e912a4421786b44af479f94f7b4503f5a Mon Sep 17 00:00:00 2001
587dbb
From: Philip Withnall <pwithnall@endlessos.org>
587dbb
Date: Mon, 17 Jan 2022 15:27:24 +0000
587dbb
Subject: [PATCH] gspawn: Report errors with closing file descriptors between
587dbb
 fork/exec
587dbb
MIME-Version: 1.0
587dbb
Content-Type: text/plain; charset=UTF-8
587dbb
Content-Transfer-Encoding: 8bit
587dbb
587dbb
If a seccomp policy is set up incorrectly so that it returns `EPERM` for
587dbb
`close_range()` rather than `ENOSYS` due to it not being recognised, no
587dbb
error would previously be reported from GLib, but some file descriptors
587dbb
wouldn’t be closed, and that would cause a hung zombie process. The
587dbb
zombie process would be waiting for one half of a socket to be closed.
587dbb
587dbb
Fix that by correctly propagating errors from `close_range()` back to the
587dbb
parent process so they can be reported correctly.
587dbb
587dbb
Distributions which aren’t yet carrying the Docker fix to correctly
587dbb
return `ENOSYS` from unrecognised syscalls may want to temporarily carry
587dbb
an additional patch to fall back to `safe_fdwalk()` if `close_range()`
587dbb
fails with `EPERM`. This change will not be accepted upstream as `EPERM`
587dbb
is not the right error for `close_range()` to be returning.
587dbb
587dbb
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
587dbb
587dbb
Fixes: #2580
587dbb
---
587dbb
 glib/gspawn.c | 35 ++++++++++++++++++++++++++---------
587dbb
 1 file changed, 26 insertions(+), 9 deletions(-)
587dbb
587dbb
diff --git a/glib/gspawn.c b/glib/gspawn.c
587dbb
index c2fe306dc..9c2f7ba7b 100644
587dbb
--- a/glib/gspawn.c
587dbb
+++ b/glib/gspawn.c
587dbb
@@ -1457,8 +1457,10 @@ safe_fdwalk (int (*cb)(void *data, int fd), void *data)
587dbb
 }
587dbb
 
587dbb
 /* This function is called between fork() and exec() and hence must be
587dbb
- * async-signal-safe (see signal-safety(7)). */
587dbb
-static void
587dbb
+ * async-signal-safe (see signal-safety(7)).
587dbb
+ *
587dbb
+ * On failure, `-1` will be returned and errno will be set. */
587dbb
+static int
587dbb
 safe_closefrom (int lowfd)
587dbb
 {
587dbb
 #if defined(__FreeBSD__) || defined(__OpenBSD__)
587dbb
@@ -1472,6 +1474,7 @@ safe_closefrom (int lowfd)
587dbb
    * should be safe to use.
587dbb
    */
587dbb
   (void) closefrom (lowfd);
587dbb
+  return 0;
587dbb
 #elif defined(__DragonFly__)
587dbb
   /* It is unclear whether closefrom function included in DragonFlyBSD libc_r
587dbb
    * is safe to use because it calls a lot of library functions. It is also
587dbb
@@ -1479,12 +1482,13 @@ safe_closefrom (int lowfd)
587dbb
    * direct system call here ourselves to avoid possible issues.
587dbb
    */
587dbb
   (void) syscall (SYS_closefrom, lowfd);
587dbb
+  return 0;
587dbb
 #elif defined(F_CLOSEM)
587dbb
   /* NetBSD and AIX have a special fcntl command which does the same thing as
587dbb
    * closefrom. NetBSD also includes closefrom function, which seems to be a
587dbb
    * simple wrapper of the fcntl command.
587dbb
    */
587dbb
-  (void) fcntl (lowfd, F_CLOSEM);
587dbb
+  return fcntl (lowfd, F_CLOSEM);
587dbb
 #else
587dbb
 
587dbb
 #if defined(HAVE_CLOSE_RANGE)
587dbb
@@ -1494,9 +1498,11 @@ safe_closefrom (int lowfd)
587dbb
    *
587dbb
    * Handle ENOSYS in case it’s supported in libc but not the kernel; if so,
587dbb
    * fall back to safe_fdwalk(). */
587dbb
-  if (close_range (lowfd, G_MAXUINT, 0) != 0 && errno == ENOSYS)
587dbb
+  int ret = close_range (lowfd, G_MAXUINT, 0);
587dbb
+  if (ret == 0 || errno != ENOSYS)
587dbb
+    return ret;
587dbb
 #endif  /* HAVE_CLOSE_RANGE */
587dbb
-  (void) safe_fdwalk (close_func, GINT_TO_POINTER (lowfd));
587dbb
+  return safe_fdwalk (close_func, GINT_TO_POINTER (lowfd));
587dbb
 #endif
587dbb
 }
587dbb
 
587dbb
@@ -1534,7 +1540,8 @@ enum
587dbb
   CHILD_EXEC_FAILED,
587dbb
   CHILD_OPEN_FAILED,
587dbb
   CHILD_DUP2_FAILED,
587dbb
-  CHILD_FORK_FAILED
587dbb
+  CHILD_FORK_FAILED,
587dbb
+  CHILD_CLOSE_FAILED,
587dbb
 };
587dbb
 
587dbb
 /* This function is called between fork() and exec() and hence must be
587dbb
@@ -1650,12 +1657,14 @@ do_exec (gint                  child_err_report_fd,
587dbb
           if (safe_dup2 (child_err_report_fd, 3) < 0)
587dbb
             write_err_and_exit (child_err_report_fd, CHILD_DUP2_FAILED);
587dbb
           set_cloexec (GINT_TO_POINTER (0), 3);
587dbb
-          safe_closefrom (4);
587dbb
+          if (safe_closefrom (4) < 0)
587dbb
+            write_err_and_exit (child_err_report_fd, CHILD_CLOSE_FAILED);
587dbb
           child_err_report_fd = 3;
587dbb
         }
587dbb
       else
587dbb
         {
587dbb
-          safe_fdwalk (set_cloexec, GINT_TO_POINTER (3));
587dbb
+          if (safe_fdwalk (set_cloexec, GINT_TO_POINTER (3)) < 0)
587dbb
+            write_err_and_exit (child_err_report_fd, CHILD_CLOSE_FAILED);
587dbb
         }
587dbb
     }
587dbb
   else
587dbb
@@ -2446,7 +2455,15 @@ fork_exec (gboolean              intermediate_child,
587dbb
                            _("Failed to fork child process (%s)"),
587dbb
                            g_strerror (buf[1]));
587dbb
               break;
587dbb
-              
587dbb
+
587dbb
+            case CHILD_CLOSE_FAILED:
587dbb
+              g_set_error (error,
587dbb
+                           G_SPAWN_ERROR,
587dbb
+                           G_SPAWN_ERROR_FAILED,
587dbb
+                           _("Failed to close file descriptor for child process (%s)"),
587dbb
+                           g_strerror (buf[1]));
587dbb
+              break;
587dbb
+
587dbb
             default:
587dbb
               g_set_error (error,
587dbb
                            G_SPAWN_ERROR,
587dbb
-- 
587dbb
2.34.1
587dbb