runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Backport Fix for 110803 to dotnet9

Open nathanmascitelli opened this issue 1 year ago • 8 comments

Description

Dotnet9 introduced a memory leak when using TLS resume on Linux (see https://github.com/dotnet/runtime/issues/110803). The issue was fixed in https://github.com/dotnet/runtime/pull/112567, however it seems like it's only be done for dotnet 10.

Can this fix be ported to dotnet 9? As it stands we are unable to use dotnet 9 due to this memory leak. While we can disable the use of TLS resume there are performance costs to that we would prefer to avoid.

Reproduction Steps

See https://github.com/dotnet/runtime/issues/110803.

Expected behavior

See https://github.com/dotnet/runtime/issues/110803.

Actual behavior

See https://github.com/dotnet/runtime/issues/110803.

Regression?

Yes, see https://github.com/dotnet/runtime/issues/110803.

Known Workarounds

Using DOTNET_SYSTEM_NET_SECURITY_DISABLETLSRESUME=1 to disable TLS resume.

Configuration

No response

Other information

No response

nathanmascitelli avatar Jun 16 '25 18:06 nathanmascitelli

Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones See info in area-owners.md if you want to be subscribed.

The leak in question happens only in case the server certificate is specified via SslServerAuthenticationOptions.ServerCertificate property (or using one of the AuthenticateAsServer(Async) overloads which accepts the server cert.)

https://github.com/dotnet/runtime/issues/110803#issuecomment-2553658071 mentions another workaround of creating a global SslStreamCertificateContext and reusing it over multiple SslStream instances. This will also improve your perf as SslStream would otherwise create a fresh SslStreamCertificateContext (which includes building the X509Chain which is expensive).

The "fix" was refactoring how we cache TLS sessions and would be too big for us to consider backporting. Please consider using the workaround using SslStreamCertificateContext.

rzikm avatar Jun 17 '25 06:06 rzikm

@rzikm thank you for the quick response! So we are using asp.net core and Kestral. While we are setting the HttpsConnectionAdapterOptions.ServerCertificateSelector property when setting up the endpoint we want Kestral to listen on we are not setting the SslServerAuthenticationOptions.ServerCertificate or calling AuthenticateAsServer(Async) anywhere explicitly. I'm assuming this is being done by the framework for us and given that its not possible to use the workaround you suggest. I'm very happy to be wrong here but so far the only solution we have found is to disable resume with the envar.

nathanmascitelli avatar Jun 17 '25 12:06 nathanmascitelli

Do you need to dynamically select the certificate via callback? Can you check if you can use one of the following instead?

builder.WebHost.UseKestrel(options =>
{
    options.ListenAnyIP(443, options =>
    {
        options.UseHttps("myCert.pfx", "pass");
    });
});

if you need only 1 cert (Kestrel caches the SslStreamCertificateContext here automatically).

or if you really need a callback, then something along the lines of

ConcurrentDictionary<string, SslStreamCertificateContext> certContextCache = new();

builder.WebHost.UseKestrel(options =>
{
    options.ListenAnyIP(443, options =>
    {
        options.UseHttps(new TlsHandshakeCallbackOptions
        {
            OnConnection = context =>
            {
                // Replace this with your current cert selection callback logic.
                string certPath = context.ClientHelloInfo.ServerName == "foo" ? "fooCert.pfx" : "barCert.pfx";

                // Use a cached context for the cert!
                SslStreamCertificateContext certContext = certContextCache.GetOrAdd(certPath, path =>
                    SslStreamCertificateContext.Create(X509CertificateLoader.LoadPkcs12FromFile(path, "pass"), null));

                return ValueTask.FromResult(new SslServerAuthenticationOptions
                {
                    ServerCertificateContext = certContext,
                    // Any other options you care about
                });
            }
        });
    });
});

rzikm avatar Jun 18 '25 09:06 rzikm

@rzikm unfortunately we do need the callback as that allows us to rotate the server certs without taking out application down.

Thank you for the suggested code, I can give that a try and confirm that it fixes the leak. Its a hard no on the backport of the fix then?

nathanmascitelli avatar Jun 18 '25 13:06 nathanmascitelli

The approach outlined in the second snippet should unblock you, then.

Its a hard no on the backport of the fix then?

We most likely would not backport the exact fix we did for .NET 10, that change is too big and brings other behavioral changes (i.e. it is risky)

We can consider developing a different fix, but if there is a workaround that works for your use case, then we'd prefer to focus on completing work planned for the next release. The workaround would also be available for you immediately, as opposed to any servicing fixes that would be available in the August release at the earliest.

If the workaround does not work for you, then we need more info how much is the issue costing you. @karelz would be the best person to continue that conversation.

rzikm avatar Jun 18 '25 14:06 rzikm

are we waiting for confirmation here or can it be closed as answered?

wfurt avatar Jul 15 '25 19:07 wfurt

Since it has been a month without hearing back about backport justification, I think we can close this.

Feel free to open a new issue if you change your mind.

rzikm avatar Jul 16 '25 06:07 rzikm