Unify SECURITY_ATTRIBUTES code across the codebase#125709
Conversation
…ileHandle Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SECURITY_ATTRIBUTES.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.FileSystem.AccessControl/src/System/IO/FileSystemAclExtensions.cs
Outdated
Show resolved
Hide resolved
|
@copilot address feedback |
…ITY_ATTRIBUTES instances Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
src/libraries/Common/src/System/IO/FileSystem.DirectoryCreation.Windows.cs
Outdated
Show resolved
Hide resolved
|
@copilot do more of your thing ...address feedback |
…iptor into factory calls Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
...ries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SECURITY_ATTRIBUTES.cs
Outdated
Show resolved
Hide resolved
…eate calls directly Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
…essary null check in RegistryKey Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot fix following build failures:
##[error]src\libraries\System.Private.CoreLib\src\System\Threading\Semaphore.Windows.cs(58,50): error IDE0059: (NETCORE_ENGINEERING_TELEMETRY=Build) Unnecessary assignment of a value to 'securityAttributes' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059)
D:\a\_work\1\s\src\libraries\System.Private.CoreLib\src\System\Threading\Semaphore.Windows.cs(58,50): error IDE0059: Unnecessary assignment of a value to 'securityAttributes' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059) [D:\a\_work\1\s\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]
##[error]src\libraries\System.Private.CoreLib\src\System\Threading\Mutex.Windows.cs(43,50): error IDE0059: (NETCORE_ENGINEERING_TELEMETRY=Build) Unnecessary assignment of a value to 'securityAttributes' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059)
D:\a\_work\1\s\src\libraries\System.Private.CoreLib\src\System\Threading\Mutex.Windows.cs(43,50): error IDE0059: Unnecessary assignment of a value to 'securityAttributes' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059) [D:\a\_work\1\s\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]
##[error]src\libraries\System.Private.CoreLib\src\System\Threading\EventWaitHandle.Windows.cs(54,50): error IDE0059: (NETCORE_ENGINEERING_TELEMETRY=Build) Unnecessary assignment of a value to 'securityAttributes' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059)
D:\a\_work\1\s\src\libraries\System.Private.CoreLib\src\System\Threading\EventWaitHandle.Windows.cs(54,50): error IDE0059: Unnecessary assignment of a value to 'securityAttributes' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0059) [D:\a\_work\1\s\src\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]
…ttributes Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR centralizes Win32 SECURITY_ATTRIBUTES initialization by introducing Interop.Kernel32.SECURITY_ATTRIBUTES.Create(...) helpers and updating multiple Windows-specific call sites to use them, reducing repeated struct setup code across threading primitives and I/O.
Changes:
- Add
SECURITY_ATTRIBUTES.Create()/Create(void*)/Create(bool)factory helpers in the Kernel32 interop layer. - Replace repeated
SECURITY_ATTRIBUTESfield assignments with the new helpers across wait handle, file, pipe, registry, directory, and memory-mapped file code paths. - Remove now-redundant local helper(s) that previously built
SECURITY_ATTRIBUTES.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Threading.AccessControl/src/System/Threading/SemaphoreAcl.cs | Use SECURITY_ATTRIBUTES.Create(void*) when creating named semaphores with custom security. |
| src/libraries/System.Threading.AccessControl/src/System/Threading/MutexAcl.cs | Use SECURITY_ATTRIBUTES.Create(void*) when creating mutexes with custom security. |
| src/libraries/System.Threading.AccessControl/src/System/Threading/EventWaitHandleAcl.cs | Use SECURITY_ATTRIBUTES.Create(void*) when creating events with custom security. |
| src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs | Use SECURITY_ATTRIBUTES.Create(void*) for “current user only” named semaphore creation. |
| src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Windows.cs | Use SECURITY_ATTRIBUTES.Create(void*) for “current user only” named mutex creation. |
| src/libraries/System.Private.CoreLib/src/System/Threading/EventWaitHandle.Windows.cs | Use SECURITY_ATTRIBUTES.Create(void*) for “current user only” named event creation. |
| src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs | Use SECURITY_ATTRIBUTES.Create(bool) to control inheritability for file handles. |
| src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs | Simplify inheritability SECURITY_ATTRIBUTES creation with Create(bool). |
| src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs | Switch to Create(bool) and remove old helper, but introduces a compile-breaking block/bracing issue. |
| src/libraries/System.IO.FileSystem.AccessControl/src/System/IO/FileSystemAclExtensions.cs | Use SECURITY_ATTRIBUTES.Create(bool) and continue setting descriptor when provided. |
| src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.cs | Use SECURITY_ATTRIBUTES.Create(void*) when creating subkeys (with optional security descriptor). |
| src/libraries/Common/src/System/IO/FileSystem.DirectoryCreation.Windows.cs | Use SECURITY_ATTRIBUTES.Create(void*) for directory creation security descriptor. |
| src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SECURITY_ATTRIBUTES.cs | Add SECURITY_ATTRIBUTES.Create(...) factory methods and remove unused using System;. |
You can also share your feedback on Copilot code review. Take the survey.
...ries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs
Show resolved
Hide resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
LGTM assuming the tests are going to pass.
|
/ba-g the failure is unrelated (chrome failing to start) |
Description
All places in the codebase that manually initialized
SECURITY_ATTRIBUTESwithnLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES)duplicated the same boilerplate. This adds three factory methods to the struct itself to eliminate all such duplication.Changes
Interop.SECURITY_ATTRIBUTES.cs: Added three factory methods:Create()— sets onlynLength = sizeof(SECURITY_ATTRIBUTES), for callers that need no other fields initialized.Create(void* securityDescriptor)— setsnLengthandlpSecurityDescriptor, for callers that provide a security descriptor.Create(bool inheritable)— setsnLengthandbInheritHandle, for callers that only need to control handle inheritance. The parameter isbool(notFileShare) sinceSECURITY_ATTRIBUTESis not file-specific.SafeFileHandle.Windows.cs: Replaced 9-line if/else block withSECURITY_ATTRIBUTES.Create((share & FileShare.Inheritable) != 0). Also fixes a subtle correctness issue: previously whenFileShare.Inheritablewas not set, the struct was still pointer-passed toCreateFilewithnLength = 0, which violates the Windows API contract.FileSystemAclExtensions.cs: Replaced manual struct initializer withSECURITY_ATTRIBUTES.Create((share & FileShare.Inheritable) != 0). Also changed fromvarto explicit type per coding conventions.MemoryMappedFile.Windows.cs: Deleted theGetSecAttrs(HandleInheritability)helper method entirely; both call sites now callInterop.Kernel32.SECURITY_ATTRIBUTES.Create(...)directly.PipeStream.Windows.cs: Simplified existingGetSecAttrshelper to a one-liner usingCreate(bool).FileSystem.DirectoryCreation.Windows.cs,EventWaitHandleAcl.cs,MutexAcl.cs,SemaphoreAcl.cs,EventWaitHandle.Windows.cs,Mutex.Windows.cs,Semaphore.Windows.cs: Replaced two-lineCreate()+secAttrs.lpSecurityDescriptor = ...pattern with a singleCreate(securityDescriptor)call. Also removed unnecessary= defaultinitializations ofsecurityAttributeslocal variables (IDE0059).RegistryKey.cs: Simplified to callCreate(pSecurityDescriptor)unconditionally inside thefixedblock — the null check was unnecessary since passingnullis valid.Testing
All affected libraries (
System.IO.FileSystem.AccessControl,System.IO.Pipes,System.IO.MemoryMappedFiles,System.Threading.AccessControl,Microsoft.Win32.Registry,System.Private.CoreLib) build successfully with 0 errors. All projects that linkInterop.SECURITY_ATTRIBUTES.cshaveAllowUnsafeBlocks=true, so the newunsafefactory methods are compatible with all consumers.Original prompt
💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.