Repository navigation
Attach the passed socket before awaiting connection_made - #786
Open
bozhko-egor wants to merge 2 commits into
Open
bozhko-egor wants to merge 2 commits into
bozhko-egor wants to merge 2 commits into
Conversation
If the peer resets the connection before the TLS ClientHello is written, connection_lost() closes the transport before create_connection(sock=) resumes. The socket was attached only after `await waiter`, so it was never detached: it kept the closed fd number and closed it again when garbage-collected, possibly closing an unrelated connection that reused the fd. Attach the socket right after `tr._open()`, so every close path detaches it. The same change is applied to create_unix_connection(). Fixes MagicStack#785
The same bug happens without TLS when the protocol aborts the transport in connection_made(): create_connection(sock=) returns normally, but the socket keeps the closed fd number.
|
@1st1 @elprans @fantix @graingert we need your help with this PR. We've spot the vulnerable behaviour in our application and we trust it affects other applications, so I'm calling for immediate attention. Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #785
create_connection(sock=)andcreate_unix_connection(sock=)attached the passed socket to the transport only afterawait waiter. If the peer resets the connection before the TLS ClientHello is written,connection_lost()closes the transport before the coroutine resumes. The socket was then never detached: it kept the closed fd number and closed it again when garbage-collected, possibly closing an unrelated connection that reused the fd.This PR attaches the socket right after
tr._open(), so every close path detaches it, as on the success path. The existingsock.detach()in theexceptbranch is unchanged; if the socket is already detached, it returns -1.The same bug happens without TLS when the protocol aborts the transport in
connection_made():create_connection(sock=)then returns normally, but the socket keeps the closed fd number.Tests:
test_create_connection_sock_abort_detaches(tests/test_tcp.py) andtest_create_unix_connection_sock_abort_detaches(tests/test_unix.py): the protocol callstransport.abort()inconnection_made().test_create_connection_ssl_sock_reset_detaches(tests/test_tcp.py): the server resets the connection withSO_LINGER0, and the client callscreate_connection(sock=, ssl=)after the RST has arrived.test_create_unix_connection_ssl_sock_closed_detaches(tests/test_unix.py): the same for a Unix socket, where the ClientHello write fails withEPIPE.All tests check
sock.fileno() == -1and run for uvloop and asyncio. On master the uvloop variants fail with13 != -1on Linux. With this change they pass (also withPYTHONASYNCIODEBUG=1), and the full test suite passes (python:3.12-slim, 535 tests).AI disclosure: the fix and tests were made with AI assistance (Claude). I ran them as described above.