-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Private key file not got deleted for X509Certificate2 after process exit correctly for .net core program #69549
Comments
Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones Issue DetailsWe detected there is different behavior for X509Certificate2 with private key in .net core program and .net framework program.
|
.NET Framework more eagerly runs garbage collection and finalization on process exit than .NET Core/.NET5+ does. If the process exits without having already run the finalizer for the object (which would clean up the private key file) then it will be left on disk, the same as if it had been loaded with PersistKeySet or the process terminated abnormally. For best results, use EphemeralKeySet when you can (e.g. when not using SslStream/HttpClient, or macOS); and always Dispose the X509Certificate(2) objects when they are no longer needed. |
Thanks for take a look at this issue. |
You could add GC.Collect();
GC.WaitForPendingFinalizers(); after everything has run, though I have no idea if the DI system will have released references to them, or not. Alternatively, when you create them you could register them into a static field/collection and register for the AppDomain.CurrentDomain.ProcessExit event, and manually call Dispose on your certificate in that callback. |
Thanks, @bartonjs Also can you provider more information about this statement |
EphemeralKeySet means the key never gets written to disk. SslStream/HttpClient on Windows can't work with certs of that form (because of an OS limitation). macOS doesn't support certificate+key pairs that aren't associated with a Keychain, which is on-disk, so it can't load them at all. |
We detected there is different behavior for X509Certificate2 with private key in .net core program and .net framework program.

We use same api in both platform
X509Certificate2 temp = new X509Certificate2(certBytes, "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
The certBytes contains private key, so after X509Certificate2 object is created, there will be a private key file created in the system.
For both platfrom, we dont call dispose method of X509Certificate2.
Then we found, in .net framework, if the process exit correctly, even we dont call dispose, the private key file will also be deleted after process exit.
While in .net core platform, the private key file will stay in file system and got leaked.
Not sure if this behavior difference is by design or not, it already caused some prod outage in our side, so want to check with the platform team to see which one is expected.
The text was updated successfully, but these errors were encountered: