-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add support for setting security attributes on Http.Sys RequestQueue #61325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ea3e527
0c2197c
0037152
cabcc2f
e38f7d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ | |
|
||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Security.AccessControl; | ||
using Microsoft.Extensions.Logging; | ||
using Windows.Win32; | ||
using Windows.Win32.Networking.HttpServer; | ||
using Windows.Win32.Security; | ||
|
||
namespace Microsoft.AspNetCore.Server.HttpSys; | ||
|
||
|
@@ -16,22 +18,25 @@ internal sealed partial class RequestQueue | |
private bool _disposed; | ||
|
||
internal RequestQueue(string requestQueueName, ILogger logger) | ||
: this(requestQueueName, RequestQueueMode.Attach, logger, receiver: true) | ||
: this(requestQueueName, RequestQueueMode.Attach, securityDescriptor: null, logger, receiver: true) | ||
{ | ||
} | ||
|
||
internal RequestQueue(string? requestQueueName, RequestQueueMode mode, ILogger logger) | ||
: this(requestQueueName, mode, logger, false) | ||
internal RequestQueue(string? requestQueueName, RequestQueueMode mode, GenericSecurityDescriptor? securityDescriptor, ILogger logger) | ||
: this(requestQueueName, mode, securityDescriptor, logger, false) | ||
{ } | ||
|
||
private RequestQueue(string? requestQueueName, RequestQueueMode mode, ILogger logger, bool receiver) | ||
private RequestQueue(string? requestQueueName, RequestQueueMode mode, GenericSecurityDescriptor? securityDescriptor, ILogger logger, bool receiver) | ||
{ | ||
_mode = mode; | ||
_logger = logger; | ||
|
||
var flags = 0u; | ||
Created = true; | ||
|
||
SECURITY_ATTRIBUTES? securityAttributes = null; | ||
nint? pSecurityDescriptor = null; | ||
|
||
if (_mode == RequestQueueMode.Attach) | ||
{ | ||
flags = PInvoke.HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING; | ||
|
@@ -41,11 +46,31 @@ private RequestQueue(string? requestQueueName, RequestQueueMode mode, ILogger lo | |
flags |= PInvoke.HTTP_CREATE_REQUEST_QUEUE_FLAG_DELEGATION; | ||
} | ||
} | ||
else if (securityDescriptor is not null) // Create or CreateOrAttach | ||
{ | ||
// Convert the security descriptor to a byte array | ||
byte[] securityDescriptorBytes = new byte[securityDescriptor.BinaryLength]; | ||
securityDescriptor.GetBinaryForm(securityDescriptorBytes, 0); | ||
|
||
// Allocate native memory for the security descriptor | ||
pSecurityDescriptor = Marshal.AllocHGlobal(securityDescriptorBytes.Length); | ||
Marshal.Copy(securityDescriptorBytes, 0, pSecurityDescriptor.Value, securityDescriptorBytes.Length); | ||
|
||
unsafe | ||
{ | ||
securityAttributes = new SECURITY_ATTRIBUTES | ||
{ | ||
nLength = (uint)Marshal.SizeOf<SECURITY_ATTRIBUTES>(), | ||
lpSecurityDescriptor = pSecurityDescriptor.Value.ToPointer(), | ||
bInheritHandle = false | ||
}; | ||
} | ||
} | ||
|
||
var statusCode = PInvoke.HttpCreateRequestQueue( | ||
HttpApi.Version, | ||
requestQueueName, | ||
default, | ||
securityAttributes, | ||
flags, | ||
out var requestQueueHandle); | ||
|
||
|
@@ -57,11 +82,17 @@ private RequestQueue(string? requestQueueName, RequestQueueMode mode, ILogger lo | |
statusCode = PInvoke.HttpCreateRequestQueue( | ||
HttpApi.Version, | ||
requestQueueName, | ||
default, | ||
SecurityAttributes: default, // Attaching should not pass any security attributes | ||
flags, | ||
out requestQueueHandle); | ||
} | ||
|
||
if (pSecurityDescriptor is not null) | ||
{ | ||
// Free the allocated memory for the security descriptor | ||
Marshal.FreeHGlobal(pSecurityDescriptor.Value); | ||
} | ||
|
||
if ((flags & PInvoke.HTTP_CREATE_REQUEST_QUEUE_FLAG_OPEN_EXISTING) != 0 && statusCode == ErrorCodes.ERROR_FILE_NOT_FOUND) | ||
{ | ||
throw new HttpSysException((int)statusCode, $"Failed to attach to the given request queue '{requestQueueName}', the queue could not be found."); | ||
|
@@ -143,6 +174,9 @@ public void Dispose() | |
} | ||
|
||
_disposed = true; | ||
|
||
PInvoke.HttpCloseRequestQueue(Handle); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like a reasonable change but unrelated to this PR. Drive-by bug fix or is it related somehow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drive-by, was just reading the docs for HttpCreateRequestQueue to make sure I was doing the correct thing with the security attribute and noticed it said you should close the queue handle with HttpCloseRequestQueue. |
||
|
||
BoundHandle.Dispose(); | ||
Handle.Dispose(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ HTTPAPI_VERSION | |
HttpCancelHttpRequest | ||
HttpCloseServerSession | ||
HttpCloseUrlGroup | ||
HttpCloseRequestQueue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this file for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's how the cswin32 source generator decides what methods to create PInvokes for |
||
HttpCreateRequestQueue | ||
HttpCreateServerSession | ||
HttpCreateUrlGroup | ||
|
@@ -59,3 +60,6 @@ HttpSetUrlGroupProperty | |
SetFileCompletionNotificationModes | ||
SOCKADDR_IN | ||
SOCKADDR_IN6 | ||
GetSecurityInfo | ||
GetSecurityDescriptorLength | ||
LocalFree |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.TlsClientHelloBytesCallback.get -> System.Action<Microsoft.AspNetCore.Http.Features.IFeatureCollection!, System.ReadOnlySpan<byte>>? | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.TlsClientHelloBytesCallback.set -> void | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.RequestQueueSecurityDescriptor.get -> System.Security.AccessControl.GenericSecurityDescriptor? | ||
Microsoft.AspNetCore.Server.HttpSys.HttpSysOptions.RequestQueueSecurityDescriptor.set -> void |
Uh oh!
There was an error while loading. Please reload this page.