From 535903696c339b71edd3575ab44bbf2e5eab689a Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Mon, 5 Nov 2018 21:02:22 +0100 Subject: Cygwin: honor SOCK_NONBLOCK/SOCK_CLOEXEC in socket(2)/socketpair(2) fhandler_socket_wsock::set_socket_handle calls set_flags after setting the O_NONBLOCK/O_CLOEXEC flags, thus overwriting them. It also turns out that fhandler_socket_wsock::init_events is called too late. The inheritence flags are changed before creating the socket event handling objects. Thus, inheritence flags for those objects are wrong with SOCK_CLOEXEC. Fix this by reordering the calls and setting the file flags through fhandler_base::set_flags. Signed-off-by: Corinna Vinschen --- winsup/cygwin/fhandler_socket_inet.cc | 16 ++++++++++------ winsup/cygwin/release/2.11.2 | 4 ++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/winsup/cygwin/fhandler_socket_inet.cc b/winsup/cygwin/fhandler_socket_inet.cc index 63abf7303..dbfbcf588 100644 --- a/winsup/cygwin/fhandler_socket_inet.cc +++ b/winsup/cygwin/fhandler_socket_inet.cc @@ -592,6 +592,7 @@ fhandler_socket_wsock::set_socket_handle (SOCKET sock, int af, int type, { DWORD hdl_flags; bool lsp_fixup = false; + int file_flags = O_RDWR | O_BINARY; /* Usually sockets are inheritable IFS objects. Unfortunately some virus scanners or other network-oriented software replace normal sockets @@ -644,18 +645,21 @@ fhandler_socket_wsock::set_socket_handle (SOCKET sock, int af, int type, } } } + set_io_handle ((HANDLE) sock); set_addr_family (af); set_socket_type (type); - if (flags & SOCK_NONBLOCK) - set_nonblocking (true); - if (flags & SOCK_CLOEXEC) - set_close_on_exec (true); - set_io_handle ((HANDLE) sock); if (!init_events ()) return -1; + if (flags & SOCK_NONBLOCK) + file_flags |= O_NONBLOCK; + if (flags & SOCK_CLOEXEC) + { + set_close_on_exec (true); + file_flags |= O_CLOEXEC; + } + set_flags (file_flags); if (lsp_fixup) init_fixup_before (); - set_flags (O_RDWR | O_BINARY); set_unique_id (); if (get_socket_type () == SOCK_DGRAM) { diff --git a/winsup/cygwin/release/2.11.2 b/winsup/cygwin/release/2.11.2 index 2ae5fbc9b..24ec5e2d6 100644 --- a/winsup/cygwin/release/2.11.2 +++ b/winsup/cygwin/release/2.11.2 @@ -18,3 +18,7 @@ Bug Fixes - Fix potential memory corruption and SEGV if socket(2), socketpair(2), accept(2), or accept4(2) fail. Addresses: https://cygwin.com/ml/cygwin/2018-10/msg00229.html + +- Fix socket(2) and socketpair(2) calls mishandling SOCK_NONBLOCK and + SOCK_CLOEXEC flags. + Addresses: https://cygwin.com/ml/cygwin/2018-11/msg00017.html -- cgit v1.2.3