gspawn: fix hangs when duping child_err_report_fd

In case child_err_report_fd conflicts with one of the target_fds, the
code here is careful to dup child_err_report_fd in order to avoid
conflating the two. It was a good idea, but evidently was not tested,
because the newly-created fd is not created with CLOEXEC set. This means
it stays open in the child process, causing the parent to hang forever
waiting to read from the other end of the pipe. Oops!

The fix is simple: just set CLOEXEC. This removes our only usage of the
safe_dup() function, so it can be dropped.

Fixes #2506
This commit is contained in:
Michael Catanzaro 2021-12-14 13:36:26 -06:00
parent 33f15d9dd0
commit e2700c7638

View File

@ -1588,20 +1588,6 @@ safe_closefrom (int lowfd)
#endif
}
/* This function is called between fork() and exec() and hence must be
* async-signal-safe (see signal-safety(7)). */
static gint
safe_dup (gint fd)
{
gint ret;
do
ret = dup (fd);
while (ret < 0 && (errno == EINTR || errno == EBUSY));
return ret;
}
/* This function is called between fork() and exec() and hence must be
* async-signal-safe (see signal-safety(7)). */
static gint
@ -1795,7 +1781,7 @@ do_exec (gint child_err_report_fd,
else
{
if (target_fds[i] == child_err_report_fd)
child_err_report_fd = safe_dup (child_err_report_fd);
child_err_report_fd = dupfd_cloexec (child_err_report_fd);
safe_dup2 (source_fds[i], target_fds[i]);
close_and_invalidate (&source_fds[i]);