Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 6581290

Browse files
committed
Do not inherit socket handles #2789
1 parent 10458d5 commit 6581290

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Net.Sockets;
6+
using System.Runtime.InteropServices;
7+
8+
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
9+
{
10+
internal static class NativeMethods
11+
{
12+
[DllImport("kernel32.dll", SetLastError = true)]
13+
private static extern bool SetHandleInformation(IntPtr hObject, HANDLE_FLAGS dwMask, HANDLE_FLAGS dwFlags);
14+
15+
[Flags]
16+
private enum HANDLE_FLAGS : uint
17+
{
18+
None = 0,
19+
INHERIT = 1,
20+
PROTECT_FROM_CLOSE = 2
21+
}
22+
23+
internal static void DisableHandleInheritance(Socket socket)
24+
{
25+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
26+
{
27+
SetHandleInformation(socket.Handle, HANDLE_FLAGS.INHERIT, 0);
28+
}
29+
}
30+
}
31+
}

src/Kestrel.Transport.Sockets/SocketTransport.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public Task BindAsync()
8080
IPEndPoint endPoint = _endPointInformation.IPEndPoint;
8181

8282
var listenSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
83+
NativeMethods.DisableHandleInheritance(listenSocket);
8384

8485
// Kestrel expects IPv6Any to bind to both IPv6 and IPv4
8586
if (endPoint.Address == IPAddress.IPv6Any)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Net.Sockets;
7+
using System.Runtime.InteropServices;
8+
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Builder;
10+
using Microsoft.AspNetCore.Hosting;
11+
using Microsoft.AspNetCore.Http;
12+
using Microsoft.AspNetCore.Testing;
13+
using Xunit;
14+
15+
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
16+
{
17+
public class HandleInheritanceTest : TestApplicationErrorLoggerLoggedTest
18+
{
19+
[Fact]
20+
public async Task SpawnChildProcess_DoesNotInheritListenHandle()
21+
{
22+
var hostBuilder = TransportSelector.GetWebHostBuilder()
23+
.UseKestrel()
24+
.ConfigureServices(AddTestLogging)
25+
.UseUrls("http://127.0.0.1:0")
26+
.Configure(app =>
27+
{
28+
app.Run(context =>
29+
{
30+
return context.Response.WriteAsync("Hello World");
31+
});
32+
});
33+
34+
using (var host = hostBuilder.Build())
35+
{
36+
await host.StartAsync();
37+
38+
var processInfo = new ProcessStartInfo
39+
{
40+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "sh",
41+
CreateNoWindow = true,
42+
};
43+
using (var process = Process.Start(processInfo))
44+
{
45+
var port = host.GetPort();
46+
await host.StopAsync();
47+
48+
using (var client = new TcpClient())
49+
{
50+
await Assert.ThrowsAnyAsync<SocketException>(() => client.ConnectAsync("127.0.0.1", port));
51+
}
52+
53+
process.Kill();
54+
}
55+
}
56+
}
57+
58+
[Fact]
59+
public async Task SpawnChildProcess_DoesNotInheritConnectionHandle()
60+
{
61+
var hostBuilder = TransportSelector.GetWebHostBuilder()
62+
.UseKestrel()
63+
.ConfigureServices(AddTestLogging)
64+
.UseUrls("http://127.0.0.1:0")
65+
.Configure(app =>
66+
{
67+
app.Run(context =>
68+
{
69+
return context.Response.WriteAsync("Hello World");
70+
});
71+
});
72+
73+
using (var host = hostBuilder.Build())
74+
{
75+
await host.StartAsync();
76+
77+
var processInfo = new ProcessStartInfo
78+
{
79+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "bash",
80+
CreateNoWindow = true,
81+
};
82+
using (var process = Process.Start(processInfo))
83+
{
84+
using (var client = new TcpClient())
85+
{
86+
await client.ConnectAsync("127.0.0.1", host.GetPort());
87+
88+
await host.StopAsync();
89+
90+
try
91+
{
92+
var read = await client.GetStream().ReadAsync(new byte[100], 0, 100).DefaultTimeout();
93+
Assert.Equal(0, read);
94+
}
95+
catch (IOException)
96+
{
97+
}
98+
}
99+
100+
process.Kill();
101+
}
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)