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

Commit 9adf76c

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

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 Microsoft.AspNetCore.Testing.xunit;
14+
using Xunit;
15+
16+
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
17+
{
18+
[OSSkipCondition(OperatingSystems.MacOSX)] // Not applicable, and vi doesn't work there.
19+
public class HandleInheritanceTest : 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+
var processInfo = new ProcessStartInfo
81+
{
82+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "vi",
83+
CreateNoWindow = true,
84+
};
85+
using (var process = Process.Start(processInfo))
86+
{
87+
using (var client = new TcpClient())
88+
{
89+
await client.ConnectAsync("127.0.0.1", host.GetPort());
90+
91+
await host.StopAsync();
92+
93+
// The connection should fail when the server shuts down if it wasn't inherited by the child process.
94+
try
95+
{
96+
var read = await client.GetStream().ReadAsync(new byte[100], 0, 100).DefaultTimeout();
97+
Assert.Equal(0, read);
98+
}
99+
catch (IOException)
100+
{
101+
}
102+
}
103+
104+
process.Kill();
105+
}
106+
}
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)