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

Commit 7336c74

Browse files
committed
Do not inherit socket handles #2789
1 parent f3b6430 commit 7336c74

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 Xunit;
13+
14+
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
15+
{
16+
public class HandleInheritanceTest : TestApplicationErrorLoggerLoggedTest
17+
{
18+
[Fact]
19+
public async Task SpawnChildProcess_DoesNotInheritListenHandle()
20+
{
21+
var hostBuilder = TransportSelector.GetWebHostBuilder()
22+
.UseKestrel()
23+
.ConfigureServices(AddTestLogging)
24+
.UseUrls("http://127.0.0.1:0")
25+
.Configure(app =>
26+
{
27+
app.Run(context =>
28+
{
29+
return context.Response.WriteAsync("Hello World");
30+
});
31+
});
32+
33+
using (var host = hostBuilder.Build())
34+
{
35+
await host.StartAsync();
36+
37+
var processInfo = new ProcessStartInfo
38+
{
39+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "bash",
40+
CreateNoWindow = true,
41+
};
42+
using (var process = Process.Start(processInfo))
43+
{
44+
var port = host.GetPort();
45+
await host.StopAsync();
46+
47+
using (var client = new TcpClient())
48+
{
49+
await Assert.ThrowsAnyAsync<SocketException>(() => client.ConnectAsync("127.0.0.1", port));
50+
}
51+
}
52+
}
53+
}
54+
55+
[Fact]
56+
public async Task SpawnChildProcess_DoesNotInheritConnectionHandle()
57+
{
58+
var hostBuilder = TransportSelector.GetWebHostBuilder()
59+
.UseKestrel()
60+
.ConfigureServices(AddTestLogging)
61+
.UseUrls("http://127.0.0.1:0")
62+
.Configure(app =>
63+
{
64+
app.Run(context =>
65+
{
66+
return context.Response.WriteAsync("Hello World");
67+
});
68+
});
69+
70+
using (var host = hostBuilder.Build())
71+
{
72+
await host.StartAsync();
73+
74+
var processInfo = new ProcessStartInfo
75+
{
76+
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "bash",
77+
CreateNoWindow = true,
78+
};
79+
using (var process = Process.Start(processInfo))
80+
{
81+
using (var client = new TcpClient())
82+
{
83+
await client.ConnectAsync("127.0.0.1", host.GetPort());
84+
85+
await host.StopAsync();
86+
87+
var read = await client.GetStream().ReadAsync(new byte[100], 0, 100).DefaultTimeout();
88+
Assert.Equal(0, read);
89+
}
90+
}
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)