RFR 8067105: Socket returned by ServerSocket.accept() is inherited by child process on Windows

Chris Hegarty chris.hegarty at oracle.com
Thu Dec 18 12:52:08 UTC 2014

On 18 Dec 2014, at 12:22, Dmitry Samersoff <dmitry.samersoff at oracle.com> wrote:

> Chris,
> In a windows world it's rather common to rely on socket inheritance
> e.g. set socket to stdin(stdout) of child process and do CreateProcess.

OK, but there can be no expectation that something like this can work with java.net.Socket, or SocketChannel. I am only proposing to change these two specific implementations.

> So please make sure your changes don't have side effects,

The changes will have an effect, accepted sockets/channels will no longer be inherited. No other potential side-effect is possible.

> also it might
> be better to control handler inheritance in one place only (at
> CreateProcess call) and don't manage it explicitly in other places.

There is some handling of a small set of “special” handles in the native process implementation. I don’t think we want to prevent all handles from being inherited. Are you suggesting something different ?


> -Dmitry
> On 2014-12-17 18:47, Chris Hegarty wrote:
>> A socket connection which is returned by ServerSocket.accept() is
>> inherited by a child process. The expected behavior is that the socket
>> connection is not inherited by the child process. This is an oversight
>> in the original implementation, that only sets HANDLE_FLAG_INHERIT for
>> newly created sockets.
>> The native socket returned by ServerSocket.accept() should be configured
>> so it will not be inherited by a child process,
>> SetHandleInformation(<HANDLE>, HANDLE_FLAG_INHERIT, FALSE) .
>> The change is in Java_java_net_DualStackPlainSocketImpl_accept0
>> diff --git
>> a/src/java.base/windows/native/libnet/DualStackPlainSocketImpl.c
>> b/src/java.base/windows/native/libnet/DualStackPlainSocketImpl.c
>> --- a/src/java.base/windows/native/libnet/DualStackPlainSocketImpl.c
>> +++ b/src/java.base/windows/native/libnet/DualStackPlainSocketImpl.c
>> @@ -294,6 +294,8 @@
>>         return -1;
>>     }
>> +    SetHandleInformation((HANDLE)(UINT_PTR)newfd, HANDLE_FLAG_INHERIT,
>> FALSE);
>> +
>>     ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, &port);
>>     isa = (*env)->NewObject(env, isa_class, isa_ctorID, ia, port);
>>     (*env)->SetObjectArrayElement(env, isaa, 0, isa);
>> -Chris.
> -- 
> Dmitry Samersoff
> Oracle Java development team, Saint Petersburg, Russia
> * I would love to change the world, but they won't give me the sources.

More information about the net-dev mailing list