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

Commit b518c9c

Browse files
committed
Do not inherit socket handles #2789
1 parent 01b35bc commit b518c9c

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.Diagnostics;
6+
using System.IO;
7+
using System.Net.Sockets;
8+
using System.Runtime.InteropServices;
9+
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Builder;
11+
using Microsoft.AspNetCore.Hosting;
12+
using Microsoft.AspNetCore.Http;
13+
using Microsoft.AspNetCore.Testing;
14+
using Microsoft.AspNetCore.Testing.xunit;
15+
using Xunit;
16+
17+
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
18+
{
19+
public class HandleInheritanceTests : TestApplicationErrorLoggerLoggedTest
20+
{
21+
[ConditionalFact]
22+
public async Task SpawnChildProcess_DoesNotInheritListenHandle()
23+
{
24+
var hostBuilder = TransportSelector.GetWebHostBuilder()
25+
.UseKestrel()
26+
.ConfigureServices(AddTestLogging)
27+
.UseUrls("http://127.0.0.1:0")
28+
.Configure(app =>
29+
{
30+
app.Run(context =>
31+
{
32+
return context.Response.WriteAsync("Hello World");
33+
});
34+
});
35+
36+
using (var host = hostBuilder.Build())
37+
{
38+
await host.StartAsync();
39+
40+
var processInfo = new ProcessStartInfo
41+
{
42+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "vi",
43+
CreateNoWindow = true,
44+
};
45+
using (var process = Process.Start(processInfo))
46+
{
47+
var port = host.GetPort();
48+
await host.StopAsync();
49+
50+
// We should not be able to connect if the handle was correctly closed and not inherited by the child process.
51+
using (var client = new TcpClient())
52+
{
53+
await Assert.ThrowsAnyAsync<SocketException>(() => client.ConnectAsync("127.0.0.1", port));
54+
}
55+
56+
process.Kill();
57+
}
58+
}
59+
}
60+
61+
[ConditionalFact]
62+
public async Task SpawnChildProcess_DoesNotInheritConnectionHandle()
63+
{
64+
var hostBuilder = TransportSelector.GetWebHostBuilder()
65+
.UseKestrel()
66+
.ConfigureServices(AddTestLogging)
67+
.UseUrls("http://127.0.0.1:0")
68+
.Configure(app =>
69+
{
70+
app.Run(context =>
71+
{
72+
return context.Response.WriteAsync("Hello World");
73+
});
74+
});
75+
76+
using (var host = hostBuilder.Build())
77+
{
78+
await host.StartAsync();
79+
80+
using (var client = new TcpClient())
81+
{
82+
// First connect and then spawn a child process
83+
await client.ConnectAsync("127.0.0.1", host.GetPort());
84+
var stream = client.GetStream();
85+
86+
var processInfo = new ProcessStartInfo
87+
{
88+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "vi",
89+
CreateNoWindow = true,
90+
};
91+
using (var process = Process.Start(processInfo))
92+
{
93+
await host.StopAsync();
94+
95+
// The connection should fail when the server shuts down if it wasn't inherited by the child process.
96+
try
97+
{
98+
var read = await stream.ReadAsync(new byte[100], 0, 100).DefaultTimeout();
99+
Assert.Equal(0, read);
100+
}
101+
catch (IOException)
102+
{
103+
}
104+
105+
process.Kill();
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)