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

Use a unified cache for SSL_CTX objects #112567

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rzikm
Copy link
Member

@rzikm rzikm commented Feb 14, 2025

Fixes #110803.

Regression from #102656, This also simplifies the internal code a bit and removes the requirement to always use SslStreamCertificateContext on server side to benefit from TLS Resumption.

Copy link
Member Author

@rzikm rzikm left a comment

Choose a reason for hiding this comment

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

/azp run runtime-libraries-coreclr outerloop

@rzikm
Copy link
Member Author

rzikm commented Feb 17, 2025

An alternative solution seems to be simply to remove

While the SslStreamCertificateContext holding the cache is gc rooted, the rent count never goes below 0 (we free when it drops below 0), and once the cert context goes out, the finalizer on SafeHandle will provide the last Dispose() which will close the handle.

Edit: the above change actually breaks a different scenario, and the code should've worked already, will try to dig more as to why it does not work now.

Edit 2: Got it, since we are using "Dispose" as "decrease reference by 1", GC finalization get's suppressed (inside SafeHandle.Dispose()) and finalizer never runs for the safe handles abandoned in the SslStreamCertificateContext. A better fix would be not using Dispose for that operation (which sounds like a good improvement in the source anyway, but after this change we won't be relying on finalizers to cleanup anymore as all reused instances are owned by the static caches)

@wfurt any opinions there? I lean towards keeping the current change (this PR), as it simplifies the code and removes the IMO not-well documented requirement on reusing cert context for TLS Resumption.

@rzikm
Copy link
Member Author

rzikm commented Feb 17, 2025

/azp run runtime-libraries-coreclr outerloop

@rzikm rzikm marked this pull request as ready for review February 17, 2025 15:53
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@wfurt
Copy link
Member

wfurt commented Feb 18, 2025

@wfurt any opinions there? I lean towards keeping the current change (this PR), as it simplifies the code and removes the IMO not-well documented requirement on reusing cert context for TLS Resumption.

I can live with ether option. Originally, I did not want global objects as the management becomes more complicated IMHO and using the context allowed easy and natural partitioning. We already use weak Certificate.Thumbprint and this change makes it more vulnerable IMHO. I do see benefit of making the resume scenario available in other cases.

While the resume behavior is not well documented (and keeps changing) I also feel the using the context is the best way and we should promote it regardless.

@rzikm
Copy link
Member Author

rzikm commented Feb 18, 2025

We already use weak Certificate.Thumbprint

As of #112193, we use SHA512 which should be good for the time being.

While the resume behavior is not well documented (and keeps changing) I also feel the using the context is the best way and we should promote it regardless.

Agreed

In that case, please review this PR when you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Memory leak using SslStream in dotnet9 running on Linux
3 participants