Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Make WindowsIdentity.RunImpersonated closer in behavior to Netfx#30377

Merged
kouvel merged 3 commits intodotnet:masterfrom
kouvel:ImpersonateFix
Jun 14, 2018
Merged

Make WindowsIdentity.RunImpersonated closer in behavior to Netfx#30377
kouvel merged 3 commits intodotnet:masterfrom
kouvel:ImpersonateFix

Conversation

@kouvel
Copy link

@kouvel kouvel commented Jun 14, 2018

Followup to #30346, this is closer to Netfx behavior for invalid handles

Followup to #30346, this is closer to Netfx behavior for invalid handles
SafeLocalAllocHandle.InvalidHandle, 0, out dwLength);
if (Marshal.GetLastWin32Error() == Interop.Errors.ERROR_INVALID_HANDLE)
throw new ArgumentException(SR.Argument_InvalidImpersonationToken);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What impact does moving this code have? Is there a test that fails before the change and passes after and on .NET Framework (and can you add that to the PR)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional check makes it possible for an ArgumentException to be thrown in cases where GetTokenInformation fails due to invalid handle or handle that is not an access token. Mainly it's just about making the checks similar to those done in netfx. Added a test.

(uint)TokenInformationClass.TokenType,
IntPtr.Zero,
0,
out dwLength) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange that we're initializing dwLength to sizeof(uint) but then passing it as out, which suggests it didn't need to be initialized. We should either a) just leave it as uint dwLength; or b) change the GetTokenInformation declaration to take the last argument as ref instead of out, based on whatever the Win32 function expects.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I just left it the way it was. It's a out-only value an in some places the local is assigned to the current buffer size and is passed in as (..., dwLength, ref dwLength). Anyway it's not incorrect and there are several places to clean up, I'll leave this for a separate PR.

[Fact]
public static void RunImpersonatedTest_InvalidHandle()
{
Assert.Throws<ArgumentException>(() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: consider moving this to just wrap the RunImpersonated call rather than the whole using

@kouvel kouvel merged commit ca6adc3 into dotnet:master Jun 14, 2018
@kouvel kouvel deleted the ImpersonateFix branch June 14, 2018 22:34
stephentoub pushed a commit that referenced this pull request Jun 27, 2018
…nImpersonated (#30346) (#30379)

* Duplicate the access token passed to WindowsIdentity.RunImpersonated (#30346)

So that callbacks for async work initiated while impersonated may continue to impersonate even after the original access token had been disposed.

Port of #30346 and #30377 to release/2.1
Fixes https://github.com/dotnet/corefx/issues/30275

* Small fix

* Address feedback

* Change to use ref counting, remove assert (no null check on access token parameter)

* Manual add/release

* Add test

* Change test
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…net/corefx#30377)

Make WindowsIdentity.RunImpersonated closer in behavior to Netfx

Followup to dotnet/corefx#30346, this is closer to Netfx behavior for invalid handles

Commit migrated from dotnet/corefx@ca6adc3
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants