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

Fix SafeLsaMemoryHandle.InvalidHandle #32786

Merged
merged 10 commits into from Oct 19, 2018
Merged

Fix SafeLsaMemoryHandle.InvalidHandle #32786

merged 10 commits into from Oct 19, 2018

Conversation

MarcoRossignoli
Copy link
Member

{
public class NTAccountTest
{
[Fact(Skip = "This test needs a machine in a domain but off line.")]
Copy link
Member

Choose a reason for hiding this comment

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

Could you let it always run, and check that it either does not throw or throws IdentityNotMappedException or Win32EXception? That would prove it does not throw ObjectDisposedException any more and occasionally it would likely get run on a machine in such a state. Eg. we often work on laptops with machines that are joined to the domain and do not have connectivity.

Or the test could somehow check for the state (in domain but offline)

[Fact(Skip = "This test needs a machine in a domain but off line.")]
public void Translate_Fail_Domain_Offline()
{
var nta = new NTAccount("foobar");
Copy link
Member

Choose a reason for hiding this comment

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

Somewhere someone has a domain or user named foobar. Best to use something like Guid.NewGuid().ToString("N")

@@ -7,6 +7,7 @@
<Compile Include="WindowsIdentityTests.cs" />
<Compile Include="WindowsPrincipalTests.cs" />
<Compile Include="WellKnownSidTypeTests.cs" />
<Compile Include="NTAccount.cs" />
Copy link
Member

Choose a reason for hiding this comment

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

Please sort. This helps avoid merge conflicts. It's how VS adds entries.

if (exception is Win32Exception win32Exception)
{
// ERROR_TRUSTED_RELATIONSHIP_FAILURE: The trust relationship between this workstation and the primary domain failed.
Assert.Equal(1789, win32Exception.NativeErrorCode);
Copy link
Member Author

Choose a reason for hiding this comment

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

@danmosemsft @jkotas i don't know if this makes sense, maybe is extras.

@MarcoRossignoli
Copy link
Member Author

@dotnet-bot test Linux arm64 Release Build please

@bartonjs
Copy link
Member

I wonder if converting SafeLsaMemoryHandle / SafeLsaPolicyHandle / SafeLsaReturnBufferHandle to use the SafeHandleCache, like SafeX509ChainHandle does, is better.

internal static SafeX509ChainHandle InvalidHandle
{
get { return SafeHandleCache<SafeX509ChainHandle>.GetInvalidHandle(() => new SafeX509ChainHandle()); }
}
protected override bool ReleaseHandle()
{
return ChainPal.ReleaseSafeX509ChainHandle(handle);
}
protected override void Dispose(bool disposing)
{
if (!SafeHandleCache<SafeX509ChainHandle>.IsCachedInvalidHandle(this))
{
base.Dispose(disposing);
}
}

@bartonjs
Copy link
Member

As I get more caught up on email I see that it looks like my comment was already given and disrecommended, but it wasn't visible here.

Personally, I prefer the SafeHandleCache because it keeps writing new code easy (no "am I supposed to not call Dispose() here if IsInvalid?"). The interlocked calls are more expensive, but they are keeping the expense in the calls, not on the finalizer thread/finalizer queue/GC sweep.

Though, restructuring code to not allocate the invalid handle redundantly is also a viable path, and then get rid of the static property altogether.

@MarcoRossignoli
Copy link
Member Author

Though, restructuring code to not allocate the invalid handle redundantly is also a viable path, and then get rid of the static property altogether.

Sorry @bartonjs i don't understand what are you proposing...now SafeLsaMemoryHandle is cached by private _invalidHandle , it's not clear how we get rid of the static property altogether.
The only other thing i could change is other two object SafeLsaPolicyHandle and SafeLsaReturnBufferHandle that allocate of every static property call.
Maybe i'm missing something.

@bartonjs
Copy link
Member

it's not clear how we get rid of the static property altogether

Instead of SafeLsaMemoryHandle ReferencedDomainsPtr = SafeLsaMemoryHandle.InvalidHandle;, start it off as null. Change the P/Invoke that rewrites it to out instead of ref, and change the finally to ReferencedDomainsPtr?.Dispose(); Do the same for the other ReferencedDomainsPtr, and SidsPtr and NamesPtr. Now nothing uses the static property, so delete it.

@MarcoRossignoli
Copy link
Member Author

@bartonjs PTAL!

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks

@stephentoub stephentoub merged commit fc2d6ff into dotnet:master Oct 19, 2018
@MarcoRossignoli MarcoRossignoli deleted the ntaccount branch October 19, 2018 18:21
@karelz karelz added this to the 3.0 milestone Nov 15, 2018
@bartonjs bartonjs removed their assignment Nov 23, 2018
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* fix SafeLsaMemoryHandle.InvalidHandle

* address PR feedback

* update tests

* address PR feedback

* update test

* sort csproj

* update test

* update test

* address PR feedback

* address PR feedback


Commit migrated from dotnet/corefx@fc2d6ff
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
7 participants