Skip to content

schannel: retain close_notify records across send retries - #23368

Closed
PiDiBi wants to merge 1 commit into
curl:masterfrom
PiDiBi:fix/schannel-shutdown-retry
Closed

PiDiBi wants to merge 1 commit into
curl:masterfrom
PiDiBi:fix/schannel-shutdown-retry

Conversation

@PiDiBi

@PiDiBi PiDiBi commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Motivation
While testing FTPS interoperability with Windows curl 8.21.0 using Schannel, we observed intermittent upload failures during TLS shutdown of the data connection. The server could receive the 64 KiB test payload but still return FTP 451, with SSPI error 0x80090330, instead of completing the transfer successfully. This occurred with both TLS 1.2 and TLS 1.3.

Receiving the payload was not sufficient: the encrypted data connection still needed to close correctly before the upload could be considered successful.

We isolated the failure using an independent, memory-only FTPS fixture. This demonstrated a curl client-side retry defect rather than a dependency on the original server’s authentication or storage implementation.

Root cause and correction
When the transport returns CURLE_AGAIN while sending Schannel’s encrypted close_notify, curl frees the unsent SSPI output token. The next shutdown call generates a new encrypted record instead of retrying the original ciphertext. This advances the TLS record sequence and can cause the peer to reject the next record.

This PR:

Retains the generated shutdown token and its sent offset until the complete record has been written.
Retries the same remaining ciphertext after would-block or short writes.
Releases pending tokens during connection cleanup.
The goal is reliable FTPS transfer completion without server-side workarounds or relaxed TLS checks. Certificate verification and TLS session reuse are unchanged.

Validation

  • Added native unit test 4782 covering repeated would-block, partial writes, pending-token cleanup, send failure, an already closed peer, and SSPI generation failure.
  • The identical native regression fails against unmodified upstream and passes with this patch.
  • Independent FTPS before/after tests reproduced the original failure under TLS 1.2 and TLS 1.3; the corrected client passed twelve upload/download hash roundtrips per version.
  • MinGW GCC warning-as-error build, unit test 4782, and checksrc pass.

AI assistance was used for the investigation and patch. The failure and correction were verified with executable before/after tests.

@github-actions github-actions Bot added the tests label Oct 11, 2026
@testclutch

Copy link
Copy Markdown

Analysis of PR #23368 at 390aad2d:

Test ../../tests/http/test_20_websockets.py::TestWebsockets::test_20_11_crazy_pings failed, but it has been 0.3% flaky lately, so it's probably NOT a fault of the PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them).

Generated by Testclutch

@PiDiBi
PiDiBi marked this pull request as ready for review October 12, 2026 02:44
@PiDiBi

PiDiBi commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

updated description with motivation for the fix

@jay

jay commented Oct 13, 2026

Copy link
Copy Markdown
Member

I've modified this slightly to address two things. First the shutdown offset should not be advanced if the send call has failed (result != 0) so now we if(!result). Second it was unclear what happened when remaining shutdown message bytes to send was 0 and sent_shutdown == FALSE, like how could this happen unless the message generated by schannel was empty, so I addressed this specifically by doing it like if(remaining) result = send bytes; else result = CURLE_OK;

@PiDiBi

PiDiBi commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, Jay. The follow-up looks reasonable and preserves the original shutdown-retry fix.

Advancing the offset only on CURLE_OK is sensible defensive accounting. Blocked or failed sends leave the pending ciphertext at the same offset, while successful partial writes still advance correctly.
Explicitly handling an empty shutdown message with CURLE_OK makes that path clearer. The previous code already initialized the result to success, so this makes the existing behavior explicit.
Importantly, pending ciphertext is still retained and retried rather than regenerated, preserving the correction for the FTPS failure.

Two minor observations:

The previous written > remaining check was removed. I haven’t identified a concrete regression from that under normal send behavior, but retaining the check on successful sends would preserve defensive validation.
Unit test 4782 doesn’t directly exercise an empty generated token or an error accompanied by a nonzero reported byte count. Targeted cases would make those edge conditions explicit.

@jay

jay commented Oct 14, 2026

Copy link
Copy Markdown
Member

The previous written > remaining check was removed.

That's ok. send will never return a written count greater than the length passed to send.

Unit test 4782 doesn’t directly exercise an empty generated token or an error accompanied by a nonzero reported byte count.

That's also ok. We don't need to test every single improbable outlier.

@jay
jay force-pushed the fix/schannel-shutdown-retry branch from 32f6113 to 5dd041d Compare October 14, 2026 02:56
jay pushed a commit to PiDiBi/curl that referenced this pull request Oct 14, 2026
Keep the SSPI shutdown token and its sent offset until the complete
record has been written. Regenerating it after CURLE_AGAIN loses an
encrypted record and can make the peer reject the next record.

Handle short writes without advancing the TLS context again and free
pending tokens when the connection closes. Add a native unit test for
would-block, partial writes, pending-token cleanup and send failures.

Prior to this change libcurl didn't properly handle the case where
the close_notify TLS record could not be sent in full on the first try.
Microsoft observed FTPS upload failures due to this problem. Refer to
the pull request for more detail.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Closes curl#23368
Keep the SSPI shutdown token and its sent offset until the complete
record has been written. Regenerating it after CURLE_AGAIN loses an
encrypted record and can make the peer reject the next record.

Handle short writes without advancing the TLS context again and free
pending tokens when the connection closes. Add a native unit test for
would-block, partial writes, pending-token cleanup and send failures.

Prior to this change libcurl didn't properly handle the case where
the close_notify TLS record could not be sent in full on the first try.
Microsoft observed FTPS upload failures due to this problem. Refer to
the pull request for more detail.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Closes curl#23368
@jay
jay force-pushed the fix/schannel-shutdown-retry branch from 5dd041d to 978bfe9 Compare October 14, 2026 03:10
@jay jay closed this in 978bfe9 Oct 14, 2026
@jay

jay commented Oct 14, 2026

Copy link
Copy Markdown
Member

Thanks

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

Labels

Development

Successfully merging this pull request may close these issues.

4 participants