Imported Upstream version 6.0.0.172

Former-commit-id: f3cc9b82f3e5bd8f0fd3ebc098f789556b44e9cd
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-04-12 14:10:50 +00:00
parent 8016999e4d
commit 64ac736ec5
32155 changed files with 3981439 additions and 75368 deletions

View File

@@ -2,7 +2,8 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\dir.props" />
<PropertyGroup>
<AssemblyVersion>4.1.1.0</AssemblyVersion>
<AssemblyVersion>4.1.1.1</AssemblyVersion>
<PackageVersion>4.5.1</PackageVersion>
<AssemblyKey>MSFT</AssemblyKey>
<IsNETCoreApp>true</IsNETCoreApp>
<IsNETCoreAppRef>false</IsNETCoreAppRef>

View File

@@ -2,9 +2,12 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<ProjectReference Include="..\ref\System.Security.Principal.Windows.csproj">
<SupportedFramework>net461;netcoreapp2.0;$(UAPvNextTFM);$(AllXamarinFrameworks)</SupportedFramework>
</ProjectReference>
<ProjectReference Include="..\ref\System.Security.Principal.Windows.csproj">
<SupportedFramework>net461</SupportedFramework>
</ProjectReference>
<SupportedFramework Include="netcoreapp2.0;$(UAPvNextTFM);$(AllXamarinFrameworks)" >
<Version>4.1.1.0</Version>
</SupportedFramework>
<ProjectReference Include="..\src\System.Security.Principal.Windows.csproj" />
<File Include="$(PlaceHolderFile)">
<TargetPath>runtimes/win/lib/uap10.0.16299</TargetPath>
@@ -17,4 +20,4 @@
<SuppressMetaPackage Include="NETStandard.Library" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
</Project>

View File

@@ -210,27 +210,68 @@ namespace System.Security.Principal
_isAuthenticated = isAuthenticated;
}
private static SafeAccessTokenHandle DuplicateAccessToken(IntPtr accessToken)
{
if (accessToken == IntPtr.Zero)
{
throw new ArgumentException(SR.Argument_TokenZero);
}
// Find out if the specified token is a valid.
uint dwLength = sizeof(uint);
if (!Interop.Advapi32.GetTokenInformation(
accessToken,
(uint)TokenInformationClass.TokenType,
IntPtr.Zero,
0,
out dwLength) &&
Marshal.GetLastWin32Error() == Interop.Errors.ERROR_INVALID_HANDLE)
{
throw new ArgumentException(SR.Argument_InvalidImpersonationToken);
}
SafeAccessTokenHandle duplicateAccessToken = SafeAccessTokenHandle.InvalidHandle;
IntPtr currentProcessHandle = Interop.Kernel32.GetCurrentProcess();
if (!Interop.Kernel32.DuplicateHandle(
currentProcessHandle,
accessToken,
currentProcessHandle,
ref duplicateAccessToken,
0,
true,
Interop.DuplicateHandleOptions.DUPLICATE_SAME_ACCESS))
{
throw new SecurityException(new Win32Exception().Message);
}
return duplicateAccessToken;
}
private static SafeAccessTokenHandle DuplicateAccessToken(SafeAccessTokenHandle accessToken)
{
if (accessToken.IsInvalid)
{
return accessToken;
}
bool refAdded = false;
try
{
accessToken.DangerousAddRef(ref refAdded);
return DuplicateAccessToken(accessToken.DangerousGetHandle());
}
finally
{
if (refAdded)
{
accessToken.DangerousRelease();
}
}
}
private void CreateFromToken(IntPtr userToken)
{
if (userToken == IntPtr.Zero)
throw new ArgumentException(SR.Argument_TokenZero);
// Find out if the specified token is a valid.
uint dwLength = (uint)sizeof(uint);
bool result = Interop.Advapi32.GetTokenInformation(userToken, (uint)TokenInformationClass.TokenType,
SafeLocalAllocHandle.InvalidHandle, 0, out dwLength);
if (Marshal.GetLastWin32Error() == Interop.Errors.ERROR_INVALID_HANDLE)
throw new ArgumentException(SR.Argument_InvalidImpersonationToken);
if (!Interop.Kernel32.DuplicateHandle(Interop.Kernel32.GetCurrentProcess(),
userToken,
Interop.Kernel32.GetCurrentProcess(),
ref _safeTokenHandle,
0,
true,
Interop.DuplicateHandleOptions.DUPLICATE_SAME_ACCESS))
throw new SecurityException(new Win32Exception().Message);
_safeTokenHandle = DuplicateAccessToken(userToken);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2229", Justification = "Public API has already shipped.")]
@@ -641,6 +682,8 @@ namespace System.Security.Principal
private static void RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)
{
token = DuplicateAccessToken(token);
bool isImpersonating;
int hr;
SafeAccessTokenHandle previousToken = GetCurrentToken(TokenAccessLevels.MaximumAllowed, false, out isImpersonating, out hr);

View File

@@ -14,5 +14,10 @@
<Link>Common\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)\System\Threading\ThreadTestHelpers.cs">
<Link>CommonTest\System\Threading\ThreadTestHelpers.cs</Link>
</Compile>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -5,8 +5,12 @@
using Microsoft.Win32.SafeHandles;
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tests;
using Xunit;
public class WindowsIdentityTests
@@ -124,6 +128,74 @@ public class WindowsIdentityTests
}
}
[Fact]
public static void RunImpersonatedTest_InvalidHandle()
{
using (var mutex = new Mutex())
{
SafeAccessTokenHandle handle = null;
try
{
handle = new SafeAccessTokenHandle(mutex.SafeWaitHandle.DangerousGetHandle());
Assert.Throws<ArgumentException>(() => WindowsIdentity.RunImpersonated(handle, () => { }));
}
finally
{
handle?.SetHandleAsInvalid();
}
}
}
[Fact]
public static void RunImpersonatedAsyncTest()
{
var testData = new RunImpersonatedAsyncTestInfo();
BeginTask(testData);
// Wait for the SafeHandle that was disposed in BeginTask() to actually be closed
GC.Collect();
GC.WaitForPendingFinalizers();
GC.WaitForPendingFinalizers();
testData.continueTask.Release();
testData.task.Wait(ThreadTestHelpers.UnexpectedTimeoutMilliseconds);
if (testData.exception != null)
{
throw new AggregateException(testData.exception);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void BeginTask(RunImpersonatedAsyncTestInfo testInfo)
{
testInfo.continueTask = new SemaphoreSlim(0, 1);
using (SafeAccessTokenHandle token = WindowsIdentity.GetCurrent().AccessToken)
{
WindowsIdentity.RunImpersonated(token, () =>
{
testInfo.task = Task.Run(async () =>
{
try
{
Task<bool> task = testInfo.continueTask.WaitAsync(ThreadTestHelpers.UnexpectedTimeoutMilliseconds);
Assert.True(await task.ConfigureAwait(false));
}
catch (Exception ex)
{
testInfo.exception = ex;
}
});
});
}
}
private class RunImpersonatedAsyncTestInfo
{
public Task task;
public SemaphoreSlim continueTask;
public Exception exception;
}
private static void CheckDispose(WindowsIdentity identity, bool anonymous = false)
{
Assert.False(identity.AccessToken.IsClosed);