Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit 2f2e409

Browse files
committed
Disable socket inheritance on Windows and macOS
We already disabled it on Linux. Doing the same for Windows and macOS.
1 parent 1d9c459 commit 2f2e409

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/Common/src/Interop/Windows/Winsock/Interop.WSASocketW.SafeCloseSocket.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal enum SocketConstructorFlags
1919
WSA_FLAG_MULTIPOINT_C_LEAF = 0x04,
2020
WSA_FLAG_MULTIPOINT_D_ROOT = 0x08,
2121
WSA_FLAG_MULTIPOINT_D_LEAF = 0x10,
22+
WSA_FLAG_NO_HANDLE_INHERIT = 0x80,
2223
}
2324

2425
[DllImport(Interop.Libraries.Ws2_32, CharSet = CharSet.Unicode, SetLastError = true)]

src/Common/src/System/Net/SafeCloseSocket.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private SocketError InnerReleaseHandle()
217217

218218
internal static InnerSafeCloseSocket CreateWSASocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
219219
{
220-
InnerSafeCloseSocket result = Interop.Winsock.WSASocketW(addressFamily, socketType, protocolType, IntPtr.Zero, 0, Interop.Winsock.SocketConstructorFlags.WSA_FLAG_OVERLAPPED);
220+
InnerSafeCloseSocket result = Interop.Winsock.WSASocketW(addressFamily, socketType, protocolType, IntPtr.Zero, 0, Interop.Winsock.SocketConstructorFlags.WSA_FLAG_OVERLAPPED | Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
221221
if (result.IsInvalid)
222222
{
223223
result.SetHandleAsInvalid();

src/Common/src/System/Net/SocketProtocolSupportPal.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static bool IsProtocolSupported(AddressFamily af)
6363

6464
try
6565
{
66-
s = Interop.Winsock.WSASocketW(af, SocketType.Dgram, 0, IntPtr.Zero, 0, 0);
66+
s = Interop.Winsock.WSASocketW(af, SocketType.Dgram, 0, IntPtr.Zero, 0, (int)Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
6767

6868
if (s == IntPtr.Zero)
6969
{

src/Native/Unix/System.Native/pal_networking.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,15 @@ int32_t SystemNative_Socket(int32_t addressFamily, int32_t socketType, int32_t p
19781978
platformSocketType |= SOCK_CLOEXEC;
19791979
#endif
19801980
*createdSocket = socket(platformAddressFamily, platformSocketType, platformProtocolType);
1981-
return *createdSocket != -1 ? Error_SUCCESS : SystemNative_ConvertErrorPlatformToPal(errno);
1981+
if (*createdSocket == -1)
1982+
{
1983+
return SystemNative_ConvertErrorPlatformToPal(errno);
1984+
}
1985+
1986+
#ifndef SOCK_CLOEXEC
1987+
fcntl(ToFileDescriptor(*createdSocket), F_SETFD, FD_CLOEXEC); // ignore any failures; this is best effort
1988+
#endif
1989+
return Error_SUCCESS;
19821990
}
19831991

19841992
int32_t SystemNative_GetAtOutOfBandMark(intptr_t socket, int32_t* atMark)

0 commit comments

Comments
 (0)