Skip to content

Failed to setup SSL connection with .NET8 Linux-Container Image with failure "Received an unexpected EOF or 0 bytes from the transport stream"  #98797

Closed as not planned

Description

@cdlliuy

Description

  • We are using http client to ping external website with below code snippets:
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
client.Timeout = TimeSpan.FromMilliseconds(5000);
try
{
    var url = "https://data.ad-score.com/img?pid=1000943&tt=g&tid=27152&l1=229666&l2=3090&l3=8070911";
    var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
    var statusCode = (int)response.StatusCode;
    var statusCodeString = response.StatusCode.ToString();
    Console.WriteLine($"{DateTime.UtcNow} RunTasks: Status code of Impression Tracking Url is {statusCode} {statusCodeString}");
}
catch ( Exception ex )
{
    Console.WriteLine($"{DateTime.UtcNow} RunTasks: thrown exception {ex}");
}
  • The target framework is net80 in csproj
  • When running this code on Windows, no issue reported
  • When running this code with .NET8 linux container,
FROM mcr.microsoft.com/dotnet/aspnet:8.0
...
CMD ["dotnet", "yinghttpconnectiontest.dll"]

I got exception below:

2/22/2024 8:03:33 AM RunTasks: thrown exception System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
 ---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
   at System.Net.Security.SslStream.ReceiveHandshakeFrameAsync[TIOAdapter](CancellationToken cancellationToken)
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](Boolean receiveFirst, Byte[] reAuthenticationData, CancellationToken cancellationToken)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
...
  • When running this code with .NET7 Linux container as base + copy .NET8 runtime to add .NET8 support
FROM mcr.microsoft.com/dotnet/aspnet:7.0
COPY --from=mcr.microsoft.com/dotnet/aspnet:8.0 /usr/share/dotnet /usr/share/dotnet
...

It works good again with above setting. I can't distinguish whether it is an issue for .NET runtime, or anything wrong in mcr.microsoft.com/dotnet/aspnet8.0 image itself

=============

To support the debug, we collected below information:

  • a tcpdump when accessing the url but failed:
    tcpdump.txt ==> please rename to *.cap to open in wireshark
  • the ssl trace log for "System.Net.Security" when it worked successfully in .NET7 base image
    net7base.good.txt
  • the ssl trace log for "System.Net.Security" when it failed in .NET8 base image
    net8base.bad.txt

I expect to understand why .NET8 linux container can't run this code pieces correctly

Reproduction Steps

see description above

Expected behavior

see description above

Actual behavior

see description above

Regression?

yes, works in .net7

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Feb 22, 2024
  2. cdlliuy commented on Feb 22, 2024

    @cdlliuy
    Author

    @wfurt , can you help to triage ? Thanks a lot!

  3. self-assigned this
    on Feb 22, 2024
  4. rzikm commented on Feb 22, 2024

    @rzikm
    Member

    I am unable to reproduce this with following dockerfile

    FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
    WORKDIR /App
    
    # Copy everything
    COPY . ./
    # Restore as distinct layers
    RUN dotnet restore
    # Build and publish a release
    RUN dotnet publish -c Release -o out
    
    # Build runtime image
    FROM mcr.microsoft.com/dotnet/aspnet:8.0
    WORKDIR /App
    COPY --from=build-env /App/out .
    ENTRYPOINT ["dotnet", "yinghttpconnectiontest.dll"]
    
    ❯ dotnet run -t yinghttpconnectiontest --rm
    2024-02-22 14:22:40 RunTasks: Status code of Impression Tracking Url is 200 OK
    

    Can you share a self-contained runnable repro?

    Looking at the packet captures, it seems to me the ClientHello triggered some weird response in server and it bailed out. (there is no ServerHello in the capture). The ClientHello itself is quite curious, it is rather different from when I tried to reproduce this. The Cipher Suites listed are quite different (e.g. TLS_CHACHA20_POLY1305_SHA256 is specified in the capture from the repro, and also something called TLS_EMPTY_RENEGOTIATION_INFO_SCSV, which aren't present in the working capture.

  5. added
    needs-author-action
    on Feb 22, 2024
  6. cdlliuy commented on Feb 23, 2024

    @cdlliuy
    Author

    @rzikm , you do "dotnet run" instead of "docker run" when trying to reproduce the issue.
    with "dotnet run" , I think you execute the program on Windows , so it can't reproduce at all.

    Here is my code and repro (see the readme file):
    https://github.com/cdlliuy/yinghttpconnectiontest/tree/master

  7. ghost removed
    needs-author-action
    on Feb 23, 2024
  8. rzikm commented on Feb 23, 2024

    @rzikm
    Member

    I see, my bad.

    I think I know where the problem is. Client is offering only ECDHE ciphers

    image

    But the server supports only RSA key exchange (see https://www.ssllabs.com/ssltest/analyze.html?d=data.ad-score.com)

    You can test it explicitly via openssl, both following fail because we try to enforce only ECDHE or DHE exchanges

    openssl s_client -connect data.ad-score.com:443 -cipher DHE
    openssl s_client -connect data.ad-score.com:443 -cipher ECDHE
    

    As noted in SslLabs, the website TLS rating is rather low since it supports only weak or insecure ciphers. And the new aspnet:8.0 image is Debian 12 which as Openssl 3 and it probably has default config which forbids those insecure ciphers.

  9. rzikm commented on Feb 23, 2024

    @rzikm
    Member

    Possible workaround is editting the /etc/ssl/openssl.cnf file

    [openssl_init]
    ssl_conf = ssl_sect
    
    [ssl_sect]
    system_default = system_default_sect
    
    [system_default_sect]
    CipherString = DEFAULT@SECLEVEL=2
    

    You can tweak the SECLEVEL=N if there are any further problems, SECLEVEL=2 is being used by the 7.0 images

  10. rzikm commented on Feb 23, 2024

    @rzikm
    Member

    Interestingly enough, the "default" behavior of openssl s_client is different than .NET's (s_client connects even without the config change above). Although we don't set anything special and rely on the default config settings.

    tagging @wfurt in case he remembers something.

  11. added this to the 9.0.0 milestone on Feb 23, 2024
  12. ghost removed
    untriaged
    on Feb 23, 2024
  13. 2 remaining items

  14. rzikm commented on Feb 26, 2024

    @rzikm
    Member

    I am not sure yet, that's what I want to investigate.

  15. rzikm commented on Feb 26, 2024

    @rzikm
    Member

    Looks like .NET applies a default opinionated config if none is configured system-wide:

    #define DOTNET_DEFAULT_CIPHERSTRING \
    "ECDHE-ECDSA-AES256-GCM-SHA384:" \
    "ECDHE-ECDSA-AES128-GCM-SHA256:" \
    "ECDHE-RSA-AES256-GCM-SHA384:" \
    "ECDHE-RSA-AES128-GCM-SHA256:" \
    "ECDHE-ECDSA-AES256-SHA384:" \
    "ECDHE-ECDSA-AES128-SHA256:" \
    "ECDHE-RSA-AES256-SHA384:" \
    "ECDHE-RSA-AES128-SHA256:" \

    Unfortunately, this config doesn't contain any cipher supported by the site you are connecting to.

    In the aspnet:7.0 image, the config explicitly specifies default ciphersuites:

    [default_conf]
    ssl_conf = ssl_sect
    
    [ssl_sect]
    system_default = system_default_sect
    
    [system_default_sect]
    MinProtocol = TLSv1.2
    CipherString = DEFAULT@SECLEVEL=2
    

    However, in aspnet:8.0 it does not, so the default .NET config applies.

    I checked dotnet/dotnet-docker and it looks like we rely solely on the distro defaults in the docker images.

    I see that DOTNET_DEFAULT_CIPHERSTRING was chosen to achieve better SslLabs grade for Kestrel websites. (SslLabs penalizes RSA key exchange because it does not have Forward Secrecy). @wfurt, @bartonjs Would including a variant with RSA key exchange at the end of the list lower the SslLabs grade? Should we add it, considering that default default OS config (as well as DEFAULT@SECLEVEL=2) allows them as well?

  16. rzikm commented on Feb 26, 2024

    @rzikm
    Member

    Just to complete the picture, this what default openssl s_client uses on aspnet:7.0

    image
  17. wfurt commented on Feb 26, 2024

    @wfurt
    Member

    we can test it e.g. setup public instance with Kestrel and see what adding DEFAULT@SECLEVEL=2 would do to the security rating. I would probably like the level more than list of specific cipherusptes as it can change over time.

    We can probably also chase openssl change that removed the block and study reasons behind it.

  18. cdlliuy commented on Mar 4, 2024

    @cdlliuy
    Author

    OK, I verified that copy the openssl.cnf from aspnet:7.0 can workaround the issue.

    FROM mcr.microsoft.com/dotnet/aspnet:8.0
    COPY --from=mcr.microsoft.com/dotnet/aspnet:7.0 /etc/ssl/openssl.cnf /etc/ssl/openssl.cnf
    

    @rzikm @wfurt , for RSA support, keeping the consistent behavior across Windows, Linux should be one of criteria to evaluate the different cipher setting.
    What is the decision for the cipher setting DEFAULT@SECLEVEL=2 in .NET runtime?
    If .NET runtime behavior won't change, should we ask for dotnet/dotnet-docker to backport the openssl.cnf ?

  19. rzikm commented on Mar 5, 2024

    @rzikm
    Member

    What is the decision for the cipher setting DEFAULT@SECLEVEL=2 in .NET runtime?

    We did not decide yet, first, we want to see what it does to SslLabs score, but that requires the server to be publicly accessible so it takes some time to set up. It is still on our todo list.

  20. bartonjs commented on Mar 5, 2024

    @bartonjs
    Member

    The list that we have as our default was assigned by the Microsoft Crypto Board. We can't add insecure ciphersuites to it. Anyone who needs a less secure ciphersuite has to change system configuration, or use the per-call configuration options.

  21. wfurt commented on Mar 6, 2024

    @wfurt
    Member

    maybe the constant is ok. But we do not respect setting the OS has e.g. openssl s_client works but SslStream does not. Perhaps we need more adjustments for OpenSSL 3 and their default configurations.

    Asking openssl.cnf modifications on client is problematic IMHO as the applications typically don't own the OS (and should not IMHO as I would be pissed personally if each app install changes my crypto)

    I also check Edge browser on Windows and it offers 16 cipherusptes including TLS_RSA_AES_128_GCM_SHA256 so it probably is not that bad. Perhaps we should have different defaults for client & server? I don't really see good reason why HttpClient needs to drop anything different than browser.

    Since this is now also our default docker image I feel it is going to be problematic unless we do something.

  22. bartonjs commented on Mar 6, 2024

    @bartonjs
    Member

    But we do not respect setting the OS has

    The change that we made was that if someone has expressed opinions in openssl.cnf, we respect those. But we don't respect OpenSSL's defaults because it allows too much bad stuff in.

    also check Edge browser on Windows and it offers 16 cipherusptes including TLS_RSA_AES_128_GCM_SHA256

    Windows has complicated history of back-compat concerns. When we changed our defaults on Linux we were told to make them to a higher standard.

  23. rzikm commented on Mar 27, 2024

    @rzikm
    Member

    Considering that:

    We don't plan to make any changes at this time. In case more users hit this issue, either upvote the issue or file a new one. If a lot of users are being affected by this issue, we will reconsider our position on this.

  24. reopened this on Mar 27, 2024
  25. removed their assignment
    on Mar 27, 2024
  26. locked and limited conversation to collaborators on Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions