Fix race between SendMessage and server-initiated key re-exchange - #1823
Fix race between SendMessage and server-initiated key re-exchange#1823radekvermirovsky wants to merge 4 commits into
Conversation
SendMessage checked the key exchange wait handle before acquiring the socket write lock. When a server-initiated re-exchange started in that window, the client's SSH_MSG_KEXINIT could be sent first, after which the already in-flight data message violated RFC 4253 section 7.1. Strict servers (e.g. ProFTPD mod_sftp) then fail the exchange or drop the connection. SendMessage now re-checks the wait handle while holding the write lock and goes back to waiting when a re-exchange has started in the meantime. The packet is also built entirely under the write lock, so a completing re-exchange can no longer swap the client cipher, MAC or compression state in the middle of building a packet. The race does not reproduce against the OpenSSH test server (which is why the attempt in sshnet#1774 stayed green): OpenSSH queues non key exchange output while a re-exchange is in progress and tolerates the client data that slips in. ProFTPD mod_sftp does not, so this adds an integration test which reproduces the failure with concurrent SFTP uploads against a ProFTPD server configured to re-key every 1 MB. Without the fix the test failed 7 out of 8 runs; with it, it passes consistently. Fixes sshnet#1764.
- Wait until the container's SSH port is actually listening before
connecting. On the Linux CI runner the first connection attempt
arrived before proftpd bound the port, failing the protocol version
exchange with "Connection reset by peer". This is the same behavior
InfrastructureFixture works around with a Task.Delay(300) on Unix;
use an explicit wait strategy instead.
- Skip the test on the Windows CI runners: Docker there is in Windows
containers mode and cannot run the Linux ProFTPD image ("no matching
manifest for windows/amd64"), mirroring the Windows CI condition
used by InfrastructureFixture.
|
A note on CI coverage: the new ProFTPD rekey test currently runs on the Linux job only. On the Windows jobs it reports inconclusive, because Docker on the Windows runners is in Windows containers mode and cannot run the Linux ProFTPD image. If you'd like it to run there too, I'm happy to extend the "Setup SSH Server" step of the Windows Integration Tests .NET job to also build and start the ProFTPD container with Podman in WSL2 on a second fixed port (mirroring how the OpenSSH server is provided), and to switch the test to the same Windows-CI detection |
|
@Rob-Hague @WojciechNagorski @drieseng — gentle ping on this one. The branch is up to date with Could one of you let me know whether this is worth keeping open? Specifically:
If there's no appetite for it, just say so and I'll close it — no hard feelings. Otherwise I'll keep it rebased and ready. |
Fixes #1764
The race
Session.SendMessagechecks_keyExchangeCompletedWaitHandlebefore acquiring_socketWriteLock. When the server initiates a key re-exchange in that window, the message loop thread resets the wait handle and sends the client'sSSH_MSG_KEXINIT(under the same write lock). A data message that already passed the check then acquires the lock and goes out on the wire after our KEXINIT, violating RFC 4253 section 7.1 ("Once a party has sent a SSH_MSG_KEXINIT message ... it MUST NOT send any messages other than: Transport layer generic messages ...; Algorithm negotiation messages ...; Specific key exchange method messages").Strict servers such as ProFTPD mod_sftp then fail the exchange or drop the connection, producing the two failure variants reported in #1764 (
Message type 93 is not valid/ connection drop withKey exchange failed).The fix
SendMessagenow re-checks the wait handle while holding the write lock and goes back to waiting when a re-exchange has started in the meantime. Re-checking under the lock is required: making the reset + KEXINIT send atomic is not sufficient, because a sender can already be blocked on the lock with a stale check result.The packet is now also built entirely under the write lock, so a completing re-exchange can no longer swap
_clientCipher/_clientMac/compression in the middle of building a packet.The diff looks bigger than it is because the packet-building body moved unchanged into
SendMessageWithinWriteLock— reviewing with whitespace changes hidden shows the actual change is the retry loop.Why #1774 could not reproduce it
The repro attempt in #1774 ran against the OpenSSH test server. OpenSSH queues non-key-exchange output while a re-exchange is in progress and tolerates client data that slips in, so the race stays invisible there. ProFTPD mod_sftp does not tolerate it, so this PR adds an integration test running concurrent SFTP uploads against a ProFTPD container (
alpine:3.24, ProFTPD 1.3.9c) configured to re-key every 1 MB (SFTPRekey required 3600 1), giving the race many trials per upload.Evidence
Same environment (Docker Desktop/WSL2, net10.0), 4 concurrent uploads of 128 MB files, 3 attempts per run:
SshConnectionException: Key exchange failed