Skip to content

rustls: reject retries shorter than buffered plaintext - #23494

Closed
cpu wants to merge 2 commits into
curl:masterfrom
cpu:rustls-retry-narrow-repair
Closed

cpu wants to merge 2 commits into
curl:masterfrom
cpu:rustls-retry-narrow-repair

Conversation

@cpu

@cpu cpu commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

See the commit messages for full detail. This is an alternative to #23260 that also considers the accounting across internal flush-only calls, adds test coverage, and tries to make the API docs a little bit more explicit.

See #23260 for a discussion of an alternative I considered and ultimately discarded as being both a much more invasive patch and also incompatible with connect-only applications and the existing API semantics.

For full disclosure, I used Codex & GPT-6-Astra to help implement the test coverage (but have reviewed the output myself). The non-test components were implemented by hand. The test coverage fails as expected when the fix is backed out, and passes otherwise. Because of the new DEBUGASSERT(0); matching the OpenSSL/mbedtls backends the test has some DEBUGBUILD gates to test the non-debug error return path. If that's too much complexity we could drop that part. LMK!

@cpu

cpu commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

@ctz If you had a few minutes to spare for a review of this I'd appreciate it.

Comment thread lib/vtls/rustls.c Resolved
@bagder

bagder commented Oct 23, 2026

Copy link
Copy Markdown
Member

In the mean time, someone sneakily stole test number 2607 so you need to pick a new one... (./scripts/testnum can offer you some)

@cpu

cpu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

someone sneakily stole test number 2607 so you need to pick a new one

Thanks for noting. I'll tackle the conflicts that have accrued + Joe's feedback shortly (likely this weekend).

cpu added 2 commits October 25, 2026 11:06
The rustls vTLS backend may accept plaintext but be blocked from writing
the TLS output. In this case we remember how much of the plaintext data
buffer we consumed, and then return CURLE_AGAIN. The caller must then
provide the same data buffer on a subsequent retry. The same API
contract is also required by the OpenSSL backend.

However, if through happenstance (e.g. bugs) the caller provides
a buffer smaller than the already consumed count for the retry, return
an error instead of mangling our accounting. This too matches the
OpenSSL vTLS backend's handling of the situation, including a debug
assertion for the invalid retry since it indicates programmer error.

We do the new check before flushing because while Curl_conn_send already
catches an excessive write count, it only does it after the backend has
flushed and cleared the saved count. Direct Curl_conn_cf_send callers
have no such check. By rejecting the retry early we leave the accounting
intact and return an explicit CURLE_BAD_FUNCTION_ARGUMENT in non-debug
builds, so a correct retry can still succeed.

For calls providing a null/empty buffer we continue to drain the TLS
output without touching the plaintext or clearing the count already
consumed when handling a subsequent retry. Handshake write readiness now
depends only on Rustls' queued TLS output.

Test coverage is added using a real Rustls connection and a lower filter
that blocks output. It covers short retries, moving and extending an
unchanged buffer, partial TLS writes, flush-only calls and fatal output
errors through both origin and proxy TLS filters. The pre-handshake
buffer lets the test exercise this accounting without a server or
certificates. Short-retry error checks run in non-debug builds, since
debug builds assert on them.
Document the same-data retry requirement imposed by WebSocket send
bookkeeping.

Also fix the instruction to wait for readability when a send blocks.
Wait for writability, as curl_easy_send does.
@cpu
cpu force-pushed the rustls-retry-narrow-repair branch from 9c4330b to a32219a Compare October 25, 2026 15:11
@cpu

cpu commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

I'll tackle the conflicts that have accrued + Joe's feedback shortly

Done. The dragonfly BSD CI failures appear unrelated.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Security-sensitive TLS accounting needs human confirmation of Rustls-enabled debug and release test results, which were not independently verified here.

No open issues 🎉

What changed in this PR

This PR hardens Rustls blocked-send handling by rejecting retries shorter than already-buffered plaintext while preserving accounting across internal flush-only calls.

Changes:

  • Validates retry lengths and keeps flush-only calls from acknowledging application plaintext.
  • Adds regression coverage for blocked sends, partial writes, retry lengths, flushes, and fatal errors.
  • Clarifies WebSocket send retry documentation to wait for writability and reuse the same data and length.
File Description
tests/​unit/​unit2640.c Tests Rustls retry accounting through direct and proxy TLS filters.
tests/​unit/​Makefile.inc Registers the new unit-test source.
tests/​data/​test2640 Defines the Rustls regression test and required features.
tests/​data/​Makefile.am Registers the new test case.
lib/​vtls/​rustls.c Adds retry validation, preserves flush-only accounting, and supplies test initialization.
docs/​libcurl/​curl_ws_send.md Corrects socket readiness guidance and clarifies retry requirements.

Review effort: Balanced

@bagder bagder closed this in 35bb30a Oct 25, 2026
@cpu
cpu deleted the rustls-retry-narrow-repair branch October 25, 2026 17:07
@cpu

cpu commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks!

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

Development

Successfully merging this pull request may close these issues.

4 participants