Skip to content

Commit

Permalink
Replace SafeLocalAllocHandle in `System.Diagnostics.PerformanceCoun…
Browse files Browse the repository at this point in the history
…ter` (#82456)

* Replace `SafeLocalAllocHandle` in `Diagnostics.PerformanceCounter`

* Update SharedPerformanceCounter.cs

* Update System.Diagnostics.PerformanceCounter.csproj
  • Loading branch information
xtqqczze authored Mar 9, 2023
1 parent 5c7e6d6 commit 5f94bff
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ internal static partial class Advapi32
{
[LibraryImport(Interop.Libraries.Advapi32, EntryPoint = "ConvertStringSecurityDescriptorToSecurityDescriptorW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool ConvertStringSecurityDescriptorToSecurityDescriptor(
internal static unsafe partial bool ConvertStringSecurityDescriptorToSecurityDescriptor(
string StringSecurityDescriptor,
int StringSDRevision,
out SafeLocalAllocHandle pSecurityDescriptor,
IntPtr SecurityDescriptorSize);
out void* pSecurityDescriptor,
uint* SecurityDescriptorSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static partial class Kernel32
internal struct SECURITY_ATTRIBUTES
{
internal uint nLength;
internal IntPtr lpSecurityDescriptor;
internal unsafe void* lpSecurityDescriptor;
internal BOOL bInheritHandle;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static unsafe void CreateDirectory(string fullPath, byte[]? securityDescr
Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = new Interop.Kernel32.SECURITY_ATTRIBUTES
{
nLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES),
lpSecurityDescriptor = (IntPtr)pSecurityDescriptor
lpSecurityDescriptor = pSecurityDescriptor
};

while (stackDir.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ System.Diagnostics.PerformanceCounter</PackageDescription>
Link="Common\Interop\Windows\Pdh\Interop.PdhFormatFromRawValue.cs" />
<Compile Include="$(CommonPath)Interop\Windows\PerfCounter\Interop.PerformanceData.cs"
Link="Common\Interop\Windows\PerfCounter\Interop.PerformanceData.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafeLocalAllocHandle.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafeLocalAllocHandle.cs" />
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\SafePerfProviderHandle.cs"
Link="Common\Microsoft\Win32\SafeHandles\SafePerfProviderHandle.cs" />
<Compile Include="$(CommonPath)System\Diagnostics\NetFrameworkUtils.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,7 @@ private unsafe void Initialize(string fileMappingName, int fileMappingSize, int
{
string mappingName = fileMappingName;

SafeLocalAllocHandle securityDescriptorPointer = null;
void* pSecurityDescriptor = null;
try
{
// The sddl string consists of these parts:
Expand All @@ -1681,12 +1681,17 @@ private unsafe void Initialize(string fileMappingName, int fileMappingSize, int
// ;S-1-5-33) the same permission granted to AU is also granted to restricted services
string sddlString = "D:(A;OICI;FRFWGRGW;;;AU)(A;OICI;FRFWGRGW;;;S-1-5-33)";

if (!Interop.Advapi32.ConvertStringSecurityDescriptorToSecurityDescriptor(sddlString, Interop.Kernel32.PerformanceCounterOptions.SDDL_REVISION_1,
out securityDescriptorPointer, IntPtr.Zero))
if (!Interop.Advapi32.ConvertStringSecurityDescriptorToSecurityDescriptor(
sddlString,
Interop.Kernel32.PerformanceCounterOptions.SDDL_REVISION_1,
out pSecurityDescriptor,
null))
{
throw new InvalidOperationException(SR.SetSecurityDescriptorFailed);
}

Interop.Kernel32.SECURITY_ATTRIBUTES securityAttributes = default;
securityAttributes.lpSecurityDescriptor = securityDescriptorPointer.DangerousGetHandle();
securityAttributes.lpSecurityDescriptor = pSecurityDescriptor;
securityAttributes.bInheritHandle = Interop.BOOL.FALSE;

//
Expand Down Expand Up @@ -1756,7 +1761,7 @@ private unsafe void Initialize(string fileMappingName, int fileMappingSize, int
}
finally
{
securityDescriptorPointer?.Close();
Marshal.FreeHGlobal((IntPtr)pSecurityDescriptor);
}

Interlocked.CompareExchange(ref *(int*)_fileViewAddress.DangerousGetHandle().ToPointer(), initialOffset, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private static unsafe SafeFileHandle CreateFileHandle(string fullPath, FileMode
{
fixed (byte* pSecurityDescriptor = security.GetSecurityDescriptorBinaryForm())
{
secAttrs.lpSecurityDescriptor = (IntPtr)pSecurityDescriptor;
secAttrs.lpSecurityDescriptor = pSecurityDescriptor;
handle = CreateFileHandleInternal(fullPath, mode, rights, share, flagsAndAttributes, &secAttrs);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ internal static unsafe Interop.Kernel32.SECURITY_ATTRIBUTES GetSecAttrs(HandleIn
pinningHandle = GCHandle.Alloc(securityDescriptor, GCHandleType.Pinned);
fixed (byte* pSecurityDescriptor = securityDescriptor)
{
secAttrs.lpSecurityDescriptor = (IntPtr)pSecurityDescriptor;
secAttrs.lpSecurityDescriptor = pSecurityDescriptor;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ private static unsafe SafeFileHandle OpenSafeFileHandle(string path, int flags)
path,
dwDesiredAccess: 0,
FileShare.ReadWrite | FileShare.Delete,
lpSecurityAttributes: (Interop.Kernel32.SECURITY_ATTRIBUTES*)IntPtr.Zero,
lpSecurityAttributes: null,
FileMode.Open,
dwFlagsAndAttributes: flags,
hTemplateFile: IntPtr.Zero);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static unsafe EventWaitHandle Create(bool initialState, EventResetMode mo
var secAttrs = new Interop.Kernel32.SECURITY_ATTRIBUTES
{
nLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES),
lpSecurityDescriptor = (IntPtr)pSecurityDescriptor
lpSecurityDescriptor = pSecurityDescriptor
};

SafeWaitHandle handle = Interop.Kernel32.CreateEventEx(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static unsafe Mutex Create(bool initiallyOwned, string? name, out bool cr
var secAttrs = new Interop.Kernel32.SECURITY_ATTRIBUTES
{
nLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES),
lpSecurityDescriptor = (IntPtr)pSecurityDescriptor
lpSecurityDescriptor = pSecurityDescriptor
};

SafeWaitHandle handle = Interop.Kernel32.CreateMutexEx(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static unsafe Semaphore Create(int initialCount, int maximumCount, string
var secAttrs = new Interop.Kernel32.SECURITY_ATTRIBUTES
{
nLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES),
lpSecurityDescriptor = (IntPtr)pSecurityDescriptor
lpSecurityDescriptor = pSecurityDescriptor
};

SafeWaitHandle handle = Interop.Kernel32.CreateSemaphoreEx(
Expand Down

0 comments on commit 5f94bff

Please sign in to comment.