Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid access of Target in SafeCredentialReference #36875

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,51 +319,6 @@ public static unsafe int AcquireCredentialsHandle(

}

//
// This is a class holding a Credential handle reference, used for static handles cache
//
#if DEBUG
internal sealed class SafeCredentialReference : DebugCriticalHandleMinusOneIsInvalid
{
#else
internal sealed class SafeCredentialReference : CriticalHandleMinusOneIsInvalid
{
#endif

//
// Static cache will return the target handle if found the reference in the table.
//
internal SafeFreeCredentials Target;

internal static SafeCredentialReference? CreateReference(SafeFreeCredentials target)
{
SafeCredentialReference result = new SafeCredentialReference(target);
if (result.IsInvalid)
{
return null;
}

return result;
}
private SafeCredentialReference(SafeFreeCredentials target) : base()
{
// Bumps up the refcount on Target to signify that target handle is statically cached so
// its dispose should be postponed
bool ignore = false;
target.DangerousAddRef(ref ignore);
Target = target;
SetHandle(new IntPtr(0)); // make this handle valid
}

protected override bool ReleaseHandle()
{
SafeFreeCredentials target = Target;
target?.DangerousRelease();
Target = null!;
return true;
}
}

internal sealed class SafeFreeCredential_SECURITY : SafeFreeCredentials
{
public SafeFreeCredential_SECURITY() : base() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace System.Net.Security
internal static class SSPIHandleCache
{
private const int c_MaxCacheSize = 0x1F; // must a (power of 2) - 1
private static readonly SafeCredentialReference[] s_cacheSlots = new SafeCredentialReference[c_MaxCacheSize + 1];
private static readonly SafeCredentialReference?[] s_cacheSlots = new SafeCredentialReference[c_MaxCacheSize + 1];
private static int s_current = -1;

internal static void CacheCredential(SafeFreeCredentials newHandle)
Expand All @@ -27,7 +27,9 @@ internal static void CacheCredential(SafeFreeCredentials newHandle)
}

int index = Interlocked.Increment(ref s_current) & c_MaxCacheSize;
#pragma warning disable CS8601 // Possible null reference assignment.
newRef = Interlocked.Exchange<SafeCredentialReference>(ref s_cacheSlots[index], newRef);
#pragma warning restore CS8601 // Possible null reference assignment.

newRef?.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System.Runtime.ConstrainedExecution;

namespace System.Net.Security
{
//
// This is a class holding a Credential handle reference, used for static handles cache
//
internal sealed class SafeCredentialReference : CriticalFinalizerObject, IDisposable
{
//
// Static cache will return the target handle if found the reference in the table.
//
internal SafeFreeCredentials? Target { get; private set; }

internal static SafeCredentialReference? CreateReference(SafeFreeCredentials target)
{
if (target.IsInvalid || target.IsClosed)
{
return null;
}

return new SafeCredentialReference(target);
}
aik-jahoda marked this conversation as resolved.
Show resolved Hide resolved

private SafeCredentialReference(SafeFreeCredentials target)
{
// Bumps up the refcount on Target to signify that target handle is statically cached so
// its dispose should be postponed
bool ignore = false;
target.DangerousAddRef(ref ignore);
Target = target;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
SafeFreeCredentials? target = Target;
target?.DangerousRelease();
Target = null;
}

~SafeCredentialReference()
{
Dispose(false);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

#nullable enable
using System.Diagnostics;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using Microsoft.Win32.SafeHandles;

namespace System.Net.Security
{
Expand All @@ -23,52 +24,4 @@ protected SafeFreeCredentials(IntPtr handle, bool ownsHandle) : base(handle, own
}
}

//
// This is a class holding a Credential handle reference, used for static handles cache
//
#if DEBUG
internal sealed class SafeCredentialReference : DebugCriticalHandleMinusOneIsInvalid
{
#else
internal sealed class SafeCredentialReference : CriticalHandleMinusOneIsInvalid
{
#endif

//
// Static cache will return the target handle if found the reference in the table.
//
internal SafeFreeCredentials Target;

internal static SafeCredentialReference? CreateReference(SafeFreeCredentials target)
{
SafeCredentialReference result = new SafeCredentialReference(target);
if (result.IsInvalid)
{
return null;
}

return result;
}
private SafeCredentialReference(SafeFreeCredentials target) : base()
{
// Bumps up the refcount on Target to signify that target handle is statically cached so
// its dispose should be postponed
bool ignore = false;
target.DangerousAddRef(ref ignore);
Target = target;
SetHandle(new IntPtr(0)); // make this handle valid
}

protected override bool ReleaseHandle()
{
SafeFreeCredentials target = Target;
if (target != null)
{
target.DangerousRelease();
}

Target = null!;
return true;
}
}
}
2 changes: 2 additions & 0 deletions src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@
Link="Common\System\Net\ContextFlagsPal.cs" />
<Compile Include="$(CommonPath)System\Net\SecurityStatusPal.cs"
Link="Common\System\Net\SecurityStatusPal.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SafeCredentialReference.cs"
Link="Common\System\Net\Security\SafeCredentialReference.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SSPIHandleCache.cs"
Link="Common\System\Net\Security\SSPIHandleCache.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SslClientAuthenticationOptionsExtensions.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@
Link="Common\System\Net\Security\SecurityBuffer.Windows.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SecurityBufferType.Windows.cs"
Link="Common\System\Net\Security\SecurityBufferType.Windows.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SafeCredentialReference.cs"
Link="Common\System\Net\Security\SafeCredentialReference.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SSPIHandleCache.cs"
Link="Common\System\Net\Security\SSPIHandleCache.cs" />
<Compile Include="$(CommonPath)System\Net\Security\NetEventSource.Security.cs"
Expand Down
6 changes: 4 additions & 2 deletions src/libraries/System.Net.Mail/src/System.Net.Mail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@
Link="Common\System\Net\ContextAwareResult.cs" />
<Compile Include="$(CommonPath)System\Net\NTAuthentication.Common.cs"
Link="Common\System\Net\NTAuthentication.Common.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SSPIHandleCache.cs"
Link="Common\System\Net\Security\SSPIHandleCache.cs" />
<Compile Include="$(CommonPath)System\Net\DebugCriticalHandleMinusOneIsInvalid.cs"
Link="Common\System\Net\DebugCriticalHandleMinusOneIsInvalid.cs" />
<Compile Include="$(CommonPath)System\Net\DebugCriticalHandleZeroOrMinusOneIsInvalid.cs"
Expand All @@ -126,6 +124,10 @@
Link="Common\System\Net\NegotiationInfoClass.cs" />
<Compile Include="$(CommonPath)System\Net\SecurityStatusPal.cs"
Link="Common\System\Net\SecurityStatusPal.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SafeCredentialReference.cs"
Link="Common\System\Net\Security\SafeCredentialReference.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SSPIHandleCache.cs"
Link="Common\System\Net\Security\SSPIHandleCache.cs" />
<Compile Include="$(CommonPath)System\Net\Security\NetEventSource.Security.cs"
Link="Common\System\Net\Security\NetEventSource.Security.cs" />
<Compile Include="$(CommonPath)System\Net\SecurityProtocol.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@
Link="Common\System\Net\NTAuthentication.Common.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SSPIHandleCache.cs"
Link="Common\System\Net\Security\SSPIHandleCache.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SafeCredentialReference.cs"
Link="Common\System\Net\Security\SafeCredentialReference.cs" />
<Compile Include="$(CommonPath)System\Net\ContextAwareResult.cs"
Link="Common\System\Net\ContextAwareResult.cs" />
<Compile Include="..\..\src\System\Net\Mail\SmtpConnection.Auth.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
Link="Common\System\NotImplemented.cs" />
<Compile Include="$(CommonPath)System\Threading\Tasks\TaskToApm.cs"
Link="Common\System\Threading\Tasks\TaskToApm.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SafeCredentialReference.cs"
Link="Common\System\Net\Security\SafeCredentialReference.cs" />
<Compile Include="$(CommonPath)System\Net\Security\SSPIHandleCache.cs"
Link="Common\System\Net\Security\SSPIHandleCache.cs" />
<Compile Include="$(CommonPath)System\Net\ContextFlagsPal.cs"
Expand Down
Loading