summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorAlexey Izbyshev <izbyshev@users.noreply.github.com>2018-02-06 06:09:34 (GMT)
committerGregory P. Smith <greg@krypto.org>2018-02-06 06:09:34 (GMT)
commitc1e46e94de38a92f98736af9a42d89c3975a9919 (patch)
tree90fdbdc92f2b0e90d5bb889b6f921995b51dd43d /Python
parent22864bc8e4a076bbac748ccda6c27f1ec41b53e7 (diff)
downloadcpython-c1e46e94de38a92f98736af9a42d89c3975a9919.zip
cpython-c1e46e94de38a92f98736af9a42d89c3975a9919.tar.gz
cpython-c1e46e94de38a92f98736af9a42d89c3975a9919.tar.bz2
bpo-32777: Fix _Py_set_inheritable async-safety in subprocess (GH-5560)
Fix a rare but potential pre-exec child process deadlock in subprocess on POSIX systems when marking file descriptors inheritable on exec in the child process. This bug appears to have been introduced in 3.4 with the inheritable file descriptors support. This also changes Python/fileutils.c `set_inheritable` to use the "slow" two `fcntl` syscall path instead of the "fast" single `ioctl` syscall path when asked to be async signal safe (by way of being asked not to raise exceptions). `ioctl` is not a POSIX async-signal-safe approved function. ref: http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html
Diffstat (limited to 'Python')
-rw-r--r--Python/fileutils.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index d610639..3cf8b7a 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -913,6 +913,7 @@ _Py_stat(PyObject *path, struct stat *statbuf)
}
+/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
static int
get_inheritable(int fd, int raise)
{
@@ -958,6 +959,8 @@ _Py_get_inheritable(int fd)
return get_inheritable(fd, 1);
}
+
+/* This function MUST be kept async-signal-safe on POSIX when raise=0. */
static int
set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
{
@@ -1014,8 +1017,10 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
#else
#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX)
- if (ioctl_works != 0) {
+ if (ioctl_works != 0 && raise != 0) {
/* fast-path: ioctl() only requires one syscall */
+ /* caveat: raise=0 is an indicator that we must be async-signal-safe
+ * thus avoid using ioctl() so we skip the fast-path. */
if (inheritable)
request = FIONCLEX;
else
@@ -1086,8 +1091,7 @@ make_non_inheritable(int fd)
}
/* Set the inheritable flag of the specified file descriptor.
- On success: return 0, on error: raise an exception if raise is nonzero
- and return -1.
+ On success: return 0, on error: raise an exception and return -1.
If atomic_flag_works is not NULL:
@@ -1108,6 +1112,15 @@ _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works)
return set_inheritable(fd, inheritable, 1, atomic_flag_works);
}
+/* Same as _Py_set_inheritable() but on error, set errno and
+ don't raise an exception.
+ This function is async-signal-safe. */
+int
+_Py_set_inheritable_async_safe(int fd, int inheritable, int *atomic_flag_works)
+{
+ return set_inheritable(fd, inheritable, 0, atomic_flag_works);
+}
+
static int
_Py_open_impl(const char *pathname, int flags, int gil_held)
{