Use GeneratedDllImport in System.Security.Principal.Windows (#53903)
authorElinor Fung <elfung@microsoft.com>
Tue, 8 Jun 2021 22:40:10 +0000 (15:40 -0700)
committerGitHub <noreply@github.com>
Tue, 8 Jun 2021 22:40:10 +0000 (15:40 -0700)
24 files changed:
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.AllocateLocallyUniqueId.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.CheckTokenMembership.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ConvertStringSidToSid.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.CreateWellKnownSid.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.DuplicateTokenEx.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.GetTokenInformation.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.GetWindowsAccountDomainSid.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.ImpersonateLoggedOnUser.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.IsEqualDomainSid.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.IsWellKnownSid.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaClose.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaFreeMemory.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaLookupSids.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.LsaOpenPolicy.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.OpenProcessToken_SafeAccessTokenHandle.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.OpenThreadToken.cs
src/libraries/Common/src/Interop/Windows/Advapi32/Interop.RevertToSelf.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.DuplicateHandle_SafeAccessTokenHandle.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaConnectUntrusted.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaFreeReturnBuffer.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaGetLogonSessionData.cs
src/libraries/Common/src/Interop/Windows/SspiCli/Interop.LsaLookupAuthenticationPackage.cs
src/libraries/System.Security.Principal.Windows/src/System.Security.Principal.Windows.csproj
src/libraries/System.Security.Principal.Windows/src/System/Security/Principal/WindowsIdentity.cs

index 689f832..594464a 100644 (file)
@@ -9,6 +9,6 @@ internal static partial class Interop
     internal static partial class Advapi32
     {
         [DllImport(Libraries.Advapi32)]
-        internal static extern bool AllocateLocallyUniqueId(out LUID Luid);
+        internal static unsafe extern int AllocateLocallyUniqueId(LUID* Luid);
     }
 }
index 1af4dba..44cd007 100644 (file)
@@ -9,7 +9,15 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        internal static partial bool CheckTokenMembership(
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
-        internal static extern bool CheckTokenMembership(SafeAccessTokenHandle TokenHandle, byte[] SidToCheck, ref bool IsMember);
+        internal static extern bool CheckTokenMembership(
+#endif
+            SafeAccessTokenHandle TokenHandle,
+            byte[] SidToCheck,
+            ref bool IsMember);
     }
 }
index 39a9559..b017d16 100644 (file)
@@ -8,7 +8,14 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "ConvertStringSidToSidW", SetLastError = true, CharSet = CharSet.Unicode)]
+        internal static partial int ConvertStringSidToSid(
+#else
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "ConvertStringSidToSidW", SetLastError = true, CharSet = CharSet.Unicode)]
-        internal static extern int ConvertStringSidToSid(string stringSid, out IntPtr ByteArray);
+        internal static extern int ConvertStringSidToSid(
+#endif
+            string stringSid,
+            out IntPtr ByteArray);
     }
 }
index 132f4a3..3ed45cf 100644 (file)
@@ -8,7 +8,16 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "CreateWellKnownSid", SetLastError = true, CharSet = CharSet.Unicode)]
+        internal static partial int CreateWellKnownSid(
+#else
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "CreateWellKnownSid", SetLastError = true, CharSet = CharSet.Unicode)]
-        internal static extern int CreateWellKnownSid(int sidType, byte[]? domainSid, byte[] resultSid, ref uint resultSidLength);
+        internal static extern int CreateWellKnownSid(
+#endif
+            int sidType,
+            byte[]? domainSid,
+            byte[] resultSid,
+            ref uint resultSidLength);
     }
 }
index e0fd150..9f4fd71 100644 (file)
@@ -9,8 +9,13 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        internal static partial bool DuplicateTokenEx(
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
         internal static extern bool DuplicateTokenEx(
+#endif
             SafeAccessTokenHandle hExistingToken,
             uint dwDesiredAccess,
             IntPtr lpTokenAttributes,   // LPSECURITY_ATTRIBUTES
index 5423812..d881a59 100644 (file)
@@ -9,24 +9,39 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        internal static partial bool GetTokenInformation(
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
         internal static extern bool GetTokenInformation(
+#endif
             SafeAccessTokenHandle TokenHandle,
             uint TokenInformationClass,
             SafeLocalAllocHandle TokenInformation,
             uint TokenInformationLength,
             out uint ReturnLength);
 
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        internal static partial bool GetTokenInformation(
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
         internal static extern bool GetTokenInformation(
+#endif
             IntPtr TokenHandle,
             uint TokenInformationClass,
             SafeLocalAllocHandle TokenInformation,
             uint TokenInformationLength,
             out uint ReturnLength);
 
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
+        internal static partial bool GetTokenInformation(
+#else
         [DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
         internal static extern bool GetTokenInformation(
+#endif
             IntPtr TokenHandle,
             uint TokenInformationClass,
             IntPtr TokenInformation,
index 9792f1f..b34b47f 100644 (file)
@@ -8,7 +8,15 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "GetWindowsAccountDomainSid", SetLastError = true, CharSet = CharSet.Unicode)]
+        internal static partial int GetWindowsAccountDomainSid(
+#else
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "GetWindowsAccountDomainSid", SetLastError = true, CharSet = CharSet.Unicode)]
-        internal static extern int GetWindowsAccountDomainSid(byte[] sid, byte[] resultSid, ref uint resultSidLength);
+        internal static extern int GetWindowsAccountDomainSid(
+#endif
+            byte[] sid,
+            byte[] resultSid,
+            ref uint resultSidLength);
     }
 }
index cf9ff0b..10ab8ec 100644 (file)
@@ -9,7 +9,12 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
+        internal static partial bool ImpersonateLoggedOnUser(SafeAccessTokenHandle userToken);
+#else
         [DllImport(Interop.Libraries.Advapi32, CharSet = CharSet.Unicode, SetLastError = true)]
         internal static extern bool ImpersonateLoggedOnUser(SafeAccessTokenHandle userToken);
+#endif
     }
 }
index 501ab7a..c18d90e 100644 (file)
@@ -8,7 +8,15 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "EqualDomainSid", SetLastError = true, CharSet = CharSet.Unicode)]
+        internal static partial int IsEqualDomainSid(
+#else
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "EqualDomainSid", SetLastError = true, CharSet = CharSet.Unicode)]
-        internal static extern int IsEqualDomainSid(byte[] sid1, byte[] sid2, out bool result);
+        internal static extern int IsEqualDomainSid(
+#endif
+            byte[] sid1,
+            byte[] sid2,
+            out bool result);
     }
 }
index db37418..c12c218 100644 (file)
@@ -8,7 +8,14 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "IsWellKnownSid", SetLastError = true, CharSet = CharSet.Unicode)]
+        internal static partial int IsWellKnownSid(
+#else
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "IsWellKnownSid", SetLastError = true, CharSet = CharSet.Unicode)]
-        internal static extern int IsWellKnownSid(byte[] sid, int type);
+        internal static extern int IsWellKnownSid(
+#endif
+            byte[] sid,
+            int type);
     }
 }
index bc29d83..e16ad2b 100644 (file)
@@ -8,7 +8,12 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        internal static partial int LsaClose(IntPtr handle);
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
         internal static extern int LsaClose(IntPtr handle);
+#endif
     }
 }
index 5193363..9d4823c 100644 (file)
@@ -8,7 +8,12 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        internal static partial int LsaFreeMemory(IntPtr handle);
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
         internal static extern int LsaFreeMemory(IntPtr handle);
+#endif
     }
 }
index 9986e35..03829ca 100644 (file)
@@ -9,8 +9,13 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "LsaLookupSids", SetLastError = true, CharSet = CharSet.Unicode)]
+        internal static partial uint LsaLookupSids(
+#else
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "LsaLookupSids", SetLastError = true, CharSet = CharSet.Unicode)]
         internal static extern uint LsaLookupSids(
+#endif
             SafeLsaPolicyHandle handle,
             int count,
             IntPtr[] sids,
index 910e6ff..ea6366e 100644 (file)
@@ -9,8 +9,13 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, EntryPoint = "LsaOpenPolicy", SetLastError = true, CharSet = CharSet.Unicode)]
+        private static partial uint LsaOpenPolicy(
+#else
         [DllImport(Interop.Libraries.Advapi32, EntryPoint = "LsaOpenPolicy", SetLastError = true, CharSet = CharSet.Unicode)]
         private static extern uint LsaOpenPolicy(
+#endif
             ref UNICODE_STRING SystemName,
             ref OBJECT_ATTRIBUTES ObjectAttributes,
             int AccessMask,
index b0560ba..bf48ccb 100644 (file)
@@ -10,7 +10,15 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        internal static partial bool OpenProcessToken(
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
-        internal static extern bool OpenProcessToken(IntPtr ProcessToken, TokenAccessLevels DesiredAccess, out SafeAccessTokenHandle TokenHandle);
+        internal static extern bool OpenProcessToken(
+#endif
+            IntPtr ProcessToken,
+            TokenAccessLevels DesiredAccess,
+            out SafeAccessTokenHandle TokenHandle);
     }
 }
index 8bce833..1deda34 100644 (file)
@@ -10,8 +10,17 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true)]
+        private static partial bool OpenThreadToken(
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true)]
-        private static extern bool OpenThreadToken(IntPtr ThreadHandle, TokenAccessLevels dwDesiredAccess, bool bOpenAsSelf, out SafeAccessTokenHandle phThreadToken);
+        private static extern bool OpenThreadToken(
+#endif
+            IntPtr ThreadHandle,
+            TokenAccessLevels dwDesiredAccess,
+            bool bOpenAsSelf,
+            out SafeAccessTokenHandle phThreadToken);
 
         internal static bool OpenThreadToken(TokenAccessLevels desiredAccess, WinSecurityContext openAs, out SafeAccessTokenHandle tokenHandle)
         {
index e18e63f..d314a94 100644 (file)
@@ -8,7 +8,12 @@ internal static partial class Interop
 {
     internal static partial class Advapi32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.Advapi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
+        internal static partial bool RevertToSelf();
+#else
         [DllImport(Interop.Libraries.Advapi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)]
         internal static extern bool RevertToSelf();
+#endif
     }
 }
index fff392d..ddfd30f 100644 (file)
@@ -9,8 +9,13 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static partial bool DuplicateHandle(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
         internal static extern bool DuplicateHandle(
+#endif
             IntPtr hSourceProcessHandle,
             IntPtr hSourceHandle,
             IntPtr hTargetProcessHandle,
index 73b9e1a..1d956fe 100644 (file)
@@ -10,7 +10,12 @@ internal static partial class Interop
 {
     internal static partial class SspiCli
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.SspiCli)]
+        internal static partial int LsaConnectUntrusted(out SafeLsaHandle LsaHandle);
+#else
         [DllImport(Interop.Libraries.SspiCli)]
         internal static extern int LsaConnectUntrusted(out SafeLsaHandle LsaHandle);
+#endif
     }
 }
index 1b0349e..bda15a5 100644 (file)
@@ -8,7 +8,12 @@ internal static partial class Interop
 {
     internal static partial class SspiCli
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.SspiCli, SetLastError = true)]
+        internal static partial int LsaFreeReturnBuffer(IntPtr handle);
+#else
         [DllImport(Interop.Libraries.SspiCli, SetLastError = true)]
         internal static extern int LsaFreeReturnBuffer(IntPtr handle);
+#endif
     }
 }
index f002e37..91e8001 100644 (file)
@@ -9,7 +9,14 @@ internal static partial class Interop
 {
     internal static partial class SspiCli
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Interop.Libraries.SspiCli, SetLastError = true)]
+        internal static partial int LsaGetLogonSessionData(
+#else
         [DllImport(Interop.Libraries.SspiCli, SetLastError = true)]
-        internal static extern int LsaGetLogonSessionData(ref LUID LogonId, out SafeLsaReturnBufferHandle ppLogonSessionData);
+        internal static extern int LsaGetLogonSessionData(
+#endif
+            ref LUID LogonId,
+            out SafeLsaReturnBufferHandle ppLogonSessionData);
     }
 }
index 059eded..dbe9a13 100644 (file)
@@ -9,10 +9,15 @@ internal static partial class Interop
 {
     internal static partial class SspiCli
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.SspiCli)]
+        internal static partial int LsaLookupAuthenticationPackage(
+#else
         [DllImport(Libraries.SspiCli)]
         internal static extern int LsaLookupAuthenticationPackage(
+#endif
             SafeLsaHandle LsaHandle,
-            [In] ref Advapi32.LSA_STRING PackageName,
+            ref Advapi32.LSA_STRING PackageName,
             out int AuthenticationPackage
         );
     }
index 4c9b0a3..8269f4c 100644 (file)
     <Reference Include="System.Collections" />
     <Reference Include="System.Diagnostics.Debug" />
     <Reference Include="System.Diagnostics.Tools" />
+    <Reference Include="System.Memory" />
     <Reference Include="System.Resources.ResourceManager" />
     <Reference Include="System.Runtime" />
     <Reference Include="System.Runtime.Extensions" />
index 4bff4c4..ed3970d 100644 (file)
@@ -128,8 +128,12 @@ namespace System.Security.Principal
                 byte[] sourceName = { (byte)'C', (byte)'L', (byte)'R', (byte)0 };
 
                 TOKEN_SOURCE sourceContext;
-                if (!Interop.Advapi32.AllocateLocallyUniqueId(out sourceContext.SourceIdentifier))
-                    throw new SecurityException(new Win32Exception().Message);
+                unsafe
+                {
+                    if (Interop.Advapi32.AllocateLocallyUniqueId(&sourceContext.SourceIdentifier) == 0)
+                        throw new SecurityException(new Win32Exception().Message);
+                }
+
                 sourceContext.SourceName = new byte[TOKEN_SOURCE.TOKEN_SOURCE_LENGTH];
                 Buffer.BlockCopy(sourceName, 0, sourceContext.SourceName, 0, sourceName.Length);