Skip to content

Instantly share code, notes, and snippets.

@cmeister2
Created October 30, 2026 08:31
Show Gist options
  • Select an option

  • Save cmeister2/ef763a8464826adb3c61823b9f5e5710 to your computer and use it in GitHub Desktop.

Select an option

Save cmeister2/ef763a8464826adb3c61823b9f5e5710 to your computer and use it in GitHub Desktop.
curl OpenSSL shutdown contract violation after SSL_ERROR_SYSCALL

curl calls SSL_read() after a fatal SSL_shutdown() result

Summary

ClusterFuzz found a real OpenSSL API contract violation in curl's graceful TLS shutdown path. This is not an AddressSanitizer memory-corruption report: the final ABRT is deliberately raised by curl-fuzzer's OpenSSL contract shim at the first forbidden call.

During IMAP connection-cache cleanup, curl does this:

  1. SSL_shutdown() returns -1.
  2. SSL_get_error() classifies that result as SSL_ERROR_SYSCALL.
  3. curl handles only SSL_ERROR_WANT_WRITE at that point.
  4. Every other classification falls through to SSL_read() on the same SSL *.
  5. The contract shim aborts before that read reaches OpenSSL.

OpenSSL documents both SSL_ERROR_SYSCALL and SSL_ERROR_SSL as fatal: no further I/O may be performed on the connection, and SSL_shutdown() must not be called again.

Relevant stack

OpenSSL contract assertion failed: SSL_read called on SSL ...
after SSL_shutdown returned -1 and SSL_get_error classified it as
SSL_ERROR_SYSCALL; no further I/O or SSL_shutdown is allowed

__wrap_SSL_read
ossl_shutdown                 lib/vtls/openssl.c:1928
ssl_cf_shutdown               lib/vtls/vtls.c:1189
cshutdn_try_once_idx          lib/cshutdn.c:162
cpool_discard_conn            lib/conncache.c:226
Curl_cpool_destroy            lib/conncache.c:279
curl_multi_cleanup            lib/multi.c:3041
PingpongMockServer::HandleDetachedMulti

PingpongMockServer::HandleDetachedMulti() merely exposes the problem while destroying the multi handle and its connection cache. A peer may close incorrectly or an underlying write may fail; curl still has to obey OpenSSL's terminal-error contract.

Why this is not a shim false positive

The shim records a failed operation by SSL *, then poisons that connection only when the matching SSL_get_error() call returns SSL_ERROR_SSL or SSL_ERROR_SYSCALL.

It deliberately treats SSL_shutdown() == 0 as nonfatal. OpenSSL defines zero as an in-progress shutdown, and its documentation warns that passing zero to SSL_get_error() can misleadingly look fatal. The reported operation returned -1, not zero.

The shim's focused tests pass and cover both the zero-result exception and aborts after genuinely fatal classifications.

Fix

ossl_shutdown() should distinguish the three SSL_shutdown() result classes before deciding whether to read:

rc = SSL_shutdown(ssl)

rc == 1:
    shutdown completed

rc == 0:
    shutdown is still in progress; do not call SSL_get_error()
    reading for the peer close_notify remains legal

rc < 0:
    error = SSL_get_error(ssl, rc)
    WANT_WRITE  -> wait for send readiness
    WANT_READ   -> wait for/read the peer close_notify
    SSL or SYSCALL -> stop immediately; do not call any more TLS I/O

For SSL_ERROR_SSL and SSL_ERROR_SYSCALL, the connection is already being discarded. The minimal behavior-preserving fix is therefore to log the shutdown error, set *done = TRUE, and leave ossl_shutdown() with CURLE_OK. That matches the function's existing treatment of a peer that closes without a usable close_notify, while removing the forbidden follow-up call.

In C, the important shape is:

rc = SSL_shutdown(octx->ssl);
if(rc == 1) {
  *done = TRUE;
  goto out;
}
if(rc < 0) {
  err = SSL_get_error(octx->ssl, rc);
  if(err == SSL_ERROR_WANT_WRITE) {
    connssl->io_need = CURL_SSL_IO_NEED_SEND;
    goto out;
  }
  if(err != SSL_ERROR_WANT_READ) {
    /* Trace the terminal shutdown classification here. */
    *done = TRUE;
    goto out;
  }
}
/* rc == 0 or SSL_ERROR_WANT_READ may continue to the existing read path. */

The final patch should retain the existing trace messages and record the fatal classification before returning.

Regression coverage

Add a shutdown test that forces this sequence:

  1. SSL_shutdown() returns -1.
  2. SSL_get_error() returns SSL_ERROR_SYSCALL (and separately SSL_ERROR_SSL).
  3. Verify that curl performs no subsequent SSL_read(), SSL_write(), or SSL_shutdown() on that object.
  4. Verify that SSL_shutdown() == 0, SSL_ERROR_WANT_READ, and SSL_ERROR_WANT_WRITE retain their existing nonblocking behavior.

Once the ClusterFuzz input is available, minimize and keep it as a curl-fuzzer regression corpus entry as an integration check. The upstream curl test should not depend solely on that fuzzer artifact.

Revision notes and validation limits

The reported curl revision, 1ee6ccab10521c8015c86c263a0f08865d575ec2, changes only tests/data/test20; it did not introduce this bug. The faulty shutdown fallthrough predates that revision and is still present on curl master at a5023857679007a9f0abeb5ad279612bff53836c (2026-10-30).

The original ClusterFuzz testcase was not available locally. A matching local libFuzzer build ran 31,566 IMAP mutations with the reported seed without rediscovering it, so the exact protobuf fields and underlying errno remain unknown. Those details are not needed to establish the contract violation: the supplied trace already records the fatal SSL_shutdown() result, its matching classification, and the forbidden next call.

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