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 <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2018-11-05 21:02:22 +01:00
parent 8ac94ca7bb
commit 535903696c
2 changed files with 14 additions and 6 deletions

View File

@ -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)
{

View File

@ -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