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

Dispose secret instances #51253

Merged
merged 8 commits into from
Nov 9, 2023
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 @@ -55,8 +55,9 @@ public CngCbcAuthenticatedEncryptorFactory(ILoggerFactory loggerFactory)
return null;
}

using var key = new Secret(secret);
return new CbcAuthenticatedEncryptor(
keyDerivationKey: new Secret(secret),
keyDerivationKey: key,
Copy link
Member

Choose a reason for hiding this comment

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

I think this is wrong because the key passed in with the returned object will already be disposed when the caller will try to use it.

@halter73 would you agree?

Copy link
Member

@halter73 halter73 Oct 19, 2023

Choose a reason for hiding this comment

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

Looking at the implementation of CbcAuthenticatedEncryptor, it copies the buffer and then releases all references to the Secret inside the constructor. So, it's probably safe, but it feels like it's relying on an implementation detail.

@GrabYourPitchforks is right that there is no memory leak, but it does look like it has a SafeHandle field that needs to be finalized if you don't dispose it. It looks like this is generally used only once per key by the singleton KeyRingProvider, so maybe the finalization is okay in order to not rely on the implementation details of the Encryptor constructors.

What's your recommendation @GrabYourPitchforks?

Copy link
Member

Choose a reason for hiding this comment

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

The general pattern of passing Secret instances around is that the receiver should make a standalone copy (via .ctor(ISecret)) if they need a copy with a standalone lifetime, which allows the caller to maintain control over their own instance's lifetime. So I don't see any issues with this PR from a "this might break in the future" perspective.

The PR seems to be fine from a correctness / regression perspective.

This PR would prevent the finalization of a few objects: let's ballpark it at single digits count per day. The determination of whether this is valuable I'll leave up to the aspnet team. :)

symmetricAlgorithmHandle: GetSymmetricBlockCipherAlgorithmHandle(configuration),
symmetricAlgorithmKeySizeInBytes: (uint)(configuration.EncryptionAlgorithmKeySize / 8),
hmacAlgorithmHandle: GetHmacAlgorithmHandle(configuration));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public CngGcmAuthenticatedEncryptorFactory(ILoggerFactory loggerFactory)
return null;
}

using var key = new Secret(secret);
return new CngGcmAuthenticatedEncryptor(
keyDerivationKey: new Secret(secret),
keyDerivationKey: key,
symmetricAlgorithmHandle: GetSymmetricBlockCipherAlgorithmHandle(configuration),
symmetricAlgorithmKeySizeInBytes: (uint)(configuration.EncryptionAlgorithmKeySize / 8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void IInternalAlgorithmConfiguration.Validate()
{
var factory = new AuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this);
using var secret = Secret.Random(512 / 8);
var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
try
{
encryptor.PerformSelfTest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ void IInternalAlgorithmConfiguration.Validate()
{
var factory = new CngCbcAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
{
encryptor.PerformSelfTest();
}
using var secret = Secret.Random(512 / 8);
using var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
encryptor.PerformSelfTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ void IInternalAlgorithmConfiguration.Validate()
{
var factory = new CngGcmAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
{
encryptor.PerformSelfTest();
}
using var secret = Secret.Random(512 / 8);
using var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
encryptor.PerformSelfTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ void IInternalAlgorithmConfiguration.Validate()
{
var factory = new ManagedAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
{
encryptor.PerformSelfTest();
}
using var secret = Secret.Random(512 / 8);
using var encryptor = factory.CreateAuthenticatedEncryptorInstance(secret, this);
encryptor.PerformSelfTest();
}

// Any changes to this method should also be be reflected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public static bool CanProtectToCurrentUserAccount()
try
{
Guid dummy;
ProtectWithDpapi(new Secret((byte*)&dummy, sizeof(Guid)), protectToLocalMachine: false);
using var secret = new Secret((byte*)&dummy, sizeof(Guid));
ProtectWithDpapi(secret, protectToLocalMachine: false);
return true;
}
catch
Expand Down
Loading