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

Commit d3f2ca9

Browse files
committed
Do not inherit socket handles #2789
1 parent 1d3090f commit d3f2ca9

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.Net.Sockets;
6+
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Builder;
9+
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Testing;
12+
using Microsoft.AspNetCore.Testing.xunit;
13+
using Xunit;
14+
15+
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
16+
{
17+
public class HandleInheritanceTests : TestApplicationErrorLoggerLoggedTest
18+
{
19+
[ConditionalFact]
20+
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "No fix available for Mac https://github.com/aspnet/KestrelHttpServer/pull/2944#issuecomment-426397600")]
21+
public async Task SpawnChildProcess_DoesNotInheritListenHandle()
22+
{
23+
var hostBuilder = TransportSelector.GetWebHostBuilder()
24+
.UseKestrel()
25+
.ConfigureServices(AddTestLogging)
26+
.UseUrls("http://127.0.0.1:0")
27+
.Configure(app =>
28+
{
29+
app.Run(context =>
30+
{
31+
return context.Response.WriteAsync("Hello World");
32+
});
33+
});
34+
35+
using (var host = hostBuilder.Build())
36+
{
37+
await host.StartAsync();
38+
39+
var processInfo = new ProcessStartInfo
40+
{
41+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "vi",
42+
CreateNoWindow = true,
43+
};
44+
using (var process = Process.Start(processInfo))
45+
{
46+
var port = host.GetPort();
47+
await host.StopAsync();
48+
49+
// We should not be able to connect if the handle was correctly closed and not inherited by the child process.
50+
using (var client = new TcpClient())
51+
{
52+
await Assert.ThrowsAnyAsync<SocketException>(() => client.ConnectAsync("127.0.0.1", port));
53+
}
54+
55+
process.Kill();
56+
}
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)