From dcf9887f56aae4adbd156dd8c93f06a2e6705895 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 14 Nov 2021 19:41:04 +0100 Subject: [PATCH 1/2] Mark sockets as non-inheritable on Windows To be able to communicate with a child process via stdin, stdout and stderr, the CreateProcess() parameter bInheritHandles must be set to TRUE. But this causes *all* handles to be inherited, including sockets. One possibility could be to use an extended API to set extra attributes on process creation: - - But it seems that this API is not available on MinGW (it does not compile). As an alternative, explicitly mark all sockets as non-inheritable. Fixes #2779 --- app/src/util/net.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/src/util/net.c b/app/src/util/net.c index 8595bc7950..9766ba85a1 100644 --- a/app/src/util/net.c +++ b/app/src/util/net.c @@ -96,6 +96,27 @@ net_perror(const char *s) { sc_socket net_socket(void) { sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0); + +#ifdef _WIN32 + /* To be able to communicate with a child process via stdin, stdout and + * stderr, the CreateProcess() parameter bInheritHandles must be set to + * TRUE. But this causes *all* handles to be inherited, including sockets. + * + * One possibility could be to use an extended API to set extra attributes + * on process creation: + * - + * - + * But it seems that this API is not available on MinGW (it does not + * compile). + * + * As an alternative, explicitly mark all sockets as non-inheritable. + */ + if (!SetHandleInformation((HANDLE) raw_sock, HANDLE_FLAG_INHERIT, 0)) { + closesocket(raw_sock); + return SC_SOCKET_NONE; + } +#endif + sc_socket sock = wrap(raw_sock); if (sock == SC_SOCKET_NONE) { net_perror("socket"); From 020b3510ae69fcb4ea93bd9e678cac940b3c814a Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 14 Nov 2021 19:51:42 +0100 Subject: [PATCH 2/2] Inherit handles only if necessary on Windows Refs #2779 --- app/src/sys/win/process.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/sys/win/process.c b/app/src/sys/win/process.c index 4dcd542ee6..43ac4c7321 100644 --- a/app/src/sys/win/process.c +++ b/app/src/sys/win/process.c @@ -26,6 +26,8 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, HANDLE *pin, HANDLE *pout, HANDLE *perr) { enum sc_process_result ret = SC_PROCESS_ERROR_GENERIC; + bool inherit_handles = pin || pout || perr; + SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; @@ -69,7 +71,7 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, PROCESS_INFORMATION pi; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); - if (pin || pout || perr) { + if (inherit_handles) { si.dwFlags = STARTF_USESTDHANDLES; if (pin) { si.hStdInput = stdin_read_handle; @@ -95,8 +97,8 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle, goto error_close_stderr; } - if (!CreateProcessW(NULL, wide, NULL, NULL, TRUE, 0, NULL, NULL, &si, - &pi)) { + if (!CreateProcessW(NULL, wide, NULL, NULL, inherit_handles, 0, NULL, NULL, + &si, &pi)) { free(wide); *handle = NULL;