Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

windows: prevent accidental inheritance of sockets pending acceptance

  • Loading branch information...
piscisaureus committed Jun 14, 2012
1 parent 5d5688f commit b1649b6f77a3d4bad40a7d2e280c84681b0ea81e
Showing with 29 additions and 12 deletions.
  1. +29 −12 src/win/tcp.c
@@ -94,12 +94,6 @@ static int uv_tcp_set_socket(uv_loop_t* loop, uv_tcp_t* handle,
return -1;
}

/* Make the socket non-inheritable */
if (!SetHandleInformation((HANDLE)socket, HANDLE_FLAG_INHERIT, 0)) {
uv__set_sys_error(loop, GetLastError());
return -1;
}

/* Associate it with the I/O completion port. */
/* Use uv_handle_t pointer as completion key. */
if (CreateIoCompletionPort((HANDLE)socket,
@@ -258,6 +252,13 @@ static int uv__bind(uv_tcp_t* handle,
return -1;
}

/* Make the socket non-inheritable */
if (!SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0)) {
uv__set_sys_error(handle->loop, GetLastError());
closesocket(sock);
return -1;
}

if (uv_tcp_set_socket(handle->loop, handle, sock, 0) == -1) {
closesocket(sock);
return -1;
@@ -371,6 +372,15 @@ static void uv_tcp_queue_accept(uv_tcp_t* handle, uv_tcp_accept_t* req) {
return;
}

/* Make the socket non-inheritable */
if (!SetHandleInformation((HANDLE) accept_socket, HANDLE_FLAG_INHERIT, 0)) {
SET_REQ_ERROR(req, GetLastError());
uv_insert_pending_req(loop, (uv_req_t*)req);
handle->reqs_pending++;
closesocket(accept_socket);
return;
}

/* Prepare the overlapped structure. */
memset(&(req->overlapped), 0, sizeof(req->overlapped));
if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
@@ -1154,21 +1164,28 @@ int uv_tcp_import(uv_tcp_t* tcp, WSAPROTOCOL_INFOW* socket_protocol_info,
return -1;
}

tcp->flags |= UV_HANDLE_BOUND;
tcp->flags |= UV_HANDLE_SHARED_TCP_SOCKET;
if (!SetHandleInformation((HANDLE) socket, HANDLE_FLAG_INHERIT, 0)) {
uv__set_sys_error(tcp->loop, GetLastError());
closesocket(socket);
return -1;
}

if (uv_tcp_set_socket(tcp->loop, tcp, socket, 1) != 0) {
closesocket(socket);
return -1;
}

if (tcp_connection) {
uv_connection_init((uv_stream_t*)tcp);
}

tcp->flags |= UV_HANDLE_BOUND;
tcp->flags |= UV_HANDLE_SHARED_TCP_SOCKET;

if (socket_protocol_info->iAddressFamily == AF_INET6) {
tcp->flags |= UV_HANDLE_IPV6;
}

if (uv_tcp_set_socket(tcp->loop, tcp, socket, 1) != 0) {
return -1;
}

tcp->loop->active_tcp_streams++;
return 0;
}

0 comments on commit b1649b6

Please sign in to comment.
You can’t perform that action at this time.