lib: graceful connection shutdown

When libcurl discards a connection there are two phases this may go
through: "shutdown" and "closing". If a connection is aborted, the
shutdown phase is skipped and it is closed right away.

The connection filters attached to the connection implement the phases
in their `do_shutdown()` and `do_close()` callbacks. Filters carry now a
`shutdown` flags next to `connected` to keep track of the shutdown
operation.

Filters are shut down from top to bottom. If a filter is not connected,
its shutdown is skipped. Notable filters that *do* something during
shutdown are HTTP/2 and TLS. HTTP/2 sends the GOAWAY frame. TLS sends
its close notify and expects to receive a close notify from the server.

As sends and receives may EAGAIN on the network, a shutdown is often not
successful right away and needs to poll the connection's socket(s). To
facilitate this, such connections are placed on a new shutdown list
inside the connection cache.

Since managing this list requires the cooperation of a multi handle,
only the connection cache belonging to a multi handle is used. If a
connection was in another cache when being discarded, it is removed
there and added to the multi's cache. If no multi handle is available at
that time, the connection is shutdown and closed in a one-time,
best-effort attempt.

When a multi handle is destroyed, all connection still on the shutdown
list are discarded with a final shutdown attempt and close. In curl
debug builds, the environment variable `CURL_GRACEFUL_SHUTDOWN` can be
set to make this graceful with a timeout in milliseconds given by the
variable.

The shutdown list is limited to the max number of connections configured
for a multi cache. Set via CURLMOPT_MAX_TOTAL_CONNECTIONS. When the
limit is reached, the oldest connection on the shutdown list is
discarded.

- In multi_wait() and multi_waitfds(), collect all connection caches
  involved (each transfer might carry its own) into a temporary list.
  Let each connection cache on the list contribute sockets and
  POLLIN/OUT events it's connections are waiting for.

- in multi_perform() collect the connection caches the same way and let
  them peform their maintenance. This will make another non-blocking
  attempt to shutdown all connections on its shutdown list.

- for event based multis (multi->socket_cb set), add the sockets and
  their poll events via the callback. When `multi_socket()` is invoked
  for a socket not known by an active transfer, forward this to the
  multi's cache for processing. On closing a connection, remove its
  socket(s) via the callback.

TLS connection filters MUST NOT send close nofity messages in their
`do_close()` implementation. The reason is that a TLS close notify
signals a success. When a connection is aborted and skips its shutdown
phase, the server needs to see a missing close notify to detect
something has gone wrong.

A graceful shutdown of FTP's data connection is performed implicitly
before regarding the upload/download as complete and continuing on the
control connection. For FTP without TLS, there is just the socket close
happening. But with TLS, the sent/received close notify signals that the
transfer is complete and healthy. Servers like `vsftpd` verify that and
reject uploads without a TLS close notify.

- added test_19_* for shutdown related tests
- test_19_01 and test_19_02 test for TCP RST packets
  which happen without a graceful shutdown and should
  no longer appear otherwise.
- add test_19_03 for handling shutdowns by the server
- add test_19_04 for handling shutdowns by curl
- add test_19_05 for event based shutdowny by server
- add test_30_06/07 and test_31_06/07 for shutdown checks
  on FTP up- and downloads.

Closes #13976
This commit is contained in:
Stefan Eissing 2024-06-19 12:40:06 +02:00 committed by Daniel Stenberg
parent c1845dc0e2
commit c9b95c0bb3
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
33 changed files with 1313 additions and 276 deletions

View file

@ -1080,7 +1080,7 @@ static CURLcode bearssl_shutdown(struct Curl_cfilter *cf,
CURLcode result;
DEBUGASSERT(backend);
if(!backend->active || connssl->shutdown) {
if(!backend->active || cf->shutdown) {
*done = TRUE;
return CURLE_OK;
}
@ -1101,7 +1101,7 @@ static CURLcode bearssl_shutdown(struct Curl_cfilter *cf,
else
CURL_TRC_CF(data, cf, "shutdown error: %d", result);
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -1112,15 +1112,10 @@ static void bearssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
(struct bearssl_ssl_backend_data *)connssl->backend;
size_t i;
(void)data;
DEBUGASSERT(backend);
if(backend->active) {
if(!connssl->shutdown) {
bool done;
bearssl_shutdown(cf, data, TRUE, &done);
}
backend->active = FALSE;
}
backend->active = FALSE;
if(backend->anchors) {
for(i = 0; i < backend->anchors_len; ++i)
free(backend->anchors[i].dn.data);

View file

@ -1822,7 +1822,7 @@ static CURLcode gtls_shutdown(struct Curl_cfilter *cf,
size_t i;
DEBUGASSERT(backend);
if(!backend->gtls.session || connssl->shutdown) {
if(!backend->gtls.session || cf->shutdown) {
*done = TRUE;
goto out;
}
@ -1876,7 +1876,7 @@ static CURLcode gtls_shutdown(struct Curl_cfilter *cf,
}
out:
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -1891,10 +1891,6 @@ static void gtls_close(struct Curl_cfilter *cf,
DEBUGASSERT(backend);
CURL_TRC_CF(data, cf, "close");
if(backend->gtls.session) {
if(!connssl->shutdown) {
bool done;
gtls_shutdown(cf, data, TRUE, &done);
}
gnutls_deinit(backend->gtls.session);
backend->gtls.session = NULL;
}

View file

@ -1274,7 +1274,7 @@ static CURLcode mbedtls_shutdown(struct Curl_cfilter *cf,
DEBUGASSERT(backend);
if(!backend->initialized || connssl->shutdown) {
if(!backend->initialized || cf->shutdown) {
*done = TRUE;
return CURLE_OK;
}
@ -1346,7 +1346,7 @@ static CURLcode mbedtls_shutdown(struct Curl_cfilter *cf,
}
out:
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -1356,13 +1356,9 @@ static void mbedtls_close(struct Curl_cfilter *cf, struct Curl_easy *data)
struct mbed_ssl_backend_data *backend =
(struct mbed_ssl_backend_data *)connssl->backend;
(void)data;
DEBUGASSERT(backend);
if(backend->initialized) {
if(!connssl->shutdown) {
bool done;
mbedtls_shutdown(cf, data, TRUE, &done);
}
mbedtls_pk_free(&backend->pk);
mbedtls_x509_crt_free(&backend->clicert);
mbedtls_x509_crt_free(&backend->cacert);

View file

@ -1880,9 +1880,10 @@ static CURLcode ossl_shutdown(struct Curl_cfilter *cf,
char buf[1024];
int nread, err;
unsigned long sslerr;
size_t i;
DEBUGASSERT(octx);
if(!octx->ssl || connssl->shutdown) {
if(!octx->ssl || cf->shutdown) {
*done = TRUE;
goto out;
}
@ -1893,14 +1894,19 @@ static CURLcode ossl_shutdown(struct Curl_cfilter *cf,
/* We have not started the shutdown from our side yet. Check
* if the server already sent us one. */
ERR_clear_error();
nread = SSL_read(octx->ssl, buf, (int)sizeof(buf));
for(i = 0; i < 10; ++i) {
nread = SSL_read(octx->ssl, buf, (int)sizeof(buf));
CURL_TRC_CF(data, cf, "SSL shutdown not sent, read -> %d", nread);
if(nread <= 0)
break;
}
err = SSL_get_error(octx->ssl, nread);
if(!nread && err == SSL_ERROR_ZERO_RETURN) {
bool input_pending;
/* Yes, it did. */
if(!send_shutdown) {
connssl->shutdown = TRUE;
CURL_TRC_CF(data, cf, "SSL shutdown received, not sending");
*done = TRUE;
goto out;
}
else if(!cf->next->cft->is_alive(cf->next, data, &input_pending)) {
@ -1908,59 +1914,65 @@ static CURLcode ossl_shutdown(struct Curl_cfilter *cf,
* seems not interested to see our close notify, so do not
* send it. We are done. */
connssl->peer_closed = TRUE;
connssl->shutdown = TRUE;
CURL_TRC_CF(data, cf, "peer closed connection");
*done = TRUE;
goto out;
}
}
if(send_shutdown && SSL_shutdown(octx->ssl) == 1) {
CURL_TRC_CF(data, cf, "SSL shutdown finished");
*done = TRUE;
goto out;
}
}
if(send_shutdown && SSL_shutdown(octx->ssl) == 1) {
CURL_TRC_CF(data, cf, "SSL shutdown finished");
/* SSL should now have started the shutdown from our side. Since it
* was not complete, we are lacking the close notify from the server. */
for(i = 0; i < 10; ++i) {
ERR_clear_error();
nread = SSL_read(octx->ssl, buf, (int)sizeof(buf));
CURL_TRC_CF(data, cf, "SSL shutdown read -> %d", nread);
if(nread <= 0)
break;
}
if(SSL_get_shutdown(octx->ssl) & SSL_RECEIVED_SHUTDOWN) {
CURL_TRC_CF(data, cf, "SSL shutdown received, finished");
*done = TRUE;
goto out;
}
else {
size_t i;
/* SSL should now have started the shutdown from our side. Since it
* was not complete, we are lacking the close notify from the server. */
for(i = 0; i < 10; ++i) {
ERR_clear_error();
nread = SSL_read(octx->ssl, buf, (int)sizeof(buf));
if(nread <= 0)
break;
}
err = SSL_get_error(octx->ssl, nread);
switch(err) {
case SSL_ERROR_ZERO_RETURN: /* no more data */
CURL_TRC_CF(data, cf, "SSL shutdown received");
*done = TRUE;
break;
case SSL_ERROR_NONE: /* just did not get anything */
case SSL_ERROR_WANT_READ:
/* SSL has send its notify and now wants to read the reply
* from the server. We are not really interested in that. */
CURL_TRC_CF(data, cf, "SSL shutdown sent, want receive");
connssl->io_need = CURL_SSL_IO_NEED_RECV;
break;
case SSL_ERROR_WANT_WRITE:
CURL_TRC_CF(data, cf, "SSL shutdown send blocked");
connssl->io_need = CURL_SSL_IO_NEED_SEND;
break;
default:
sslerr = ERR_get_error();
CURL_TRC_CF(data, cf, "SSL shutdown, error: '%s', errno %d",
(sslerr ?
ossl_strerror(sslerr, buf, sizeof(buf)) :
SSL_ERROR_to_str(err)),
SOCKERRNO);
result = CURLE_RECV_ERROR;
break;
}
err = SSL_get_error(octx->ssl, nread);
switch(err) {
case SSL_ERROR_ZERO_RETURN: /* no more data */
CURL_TRC_CF(data, cf, "SSL shutdown not received, but closed");
*done = TRUE;
break;
case SSL_ERROR_NONE: /* just did not get anything */
case SSL_ERROR_WANT_READ:
/* SSL has send its notify and now wants to read the reply
* from the server. We are not really interested in that. */
CURL_TRC_CF(data, cf, "SSL shutdown sent, want receive");
connssl->io_need = CURL_SSL_IO_NEED_RECV;
break;
case SSL_ERROR_WANT_WRITE:
CURL_TRC_CF(data, cf, "SSL shutdown send blocked");
connssl->io_need = CURL_SSL_IO_NEED_SEND;
break;
default:
/* Server seems to have closed the connection without sending us
* a close notify. */
sslerr = ERR_get_error();
CURL_TRC_CF(data, cf, "SSL shutdown, ignore recv error: '%s', errno %d",
(sslerr ?
ossl_strerror(sslerr, buf, sizeof(buf)) :
SSL_ERROR_to_str(err)),
SOCKERRNO);
*done = TRUE;
result = CURLE_OK;
break;
}
out:
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -1973,14 +1985,6 @@ static void ossl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
DEBUGASSERT(octx);
if(octx->ssl) {
/* Send the TLS shutdown if have not done so already and are still
* connected *and* if the peer did not already close the connection. */
if(cf->connected && !connssl->shutdown &&
cf->next && cf->next->connected && !connssl->peer_closed) {
bool done;
(void)ossl_shutdown(cf, data, TRUE, &done);
}
SSL_free(octx->ssl);
octx->ssl = NULL;
}

View file

@ -742,7 +742,7 @@ cr_shutdown(struct Curl_cfilter *cf,
size_t i;
DEBUGASSERT(backend);
if(!backend->conn || connssl->shutdown) {
if(!backend->conn || cf->shutdown) {
*done = TRUE;
goto out;
}
@ -793,7 +793,7 @@ cr_shutdown(struct Curl_cfilter *cf,
}
out:
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -804,16 +804,9 @@ cr_close(struct Curl_cfilter *cf, struct Curl_easy *data)
struct rustls_ssl_backend_data *backend =
(struct rustls_ssl_backend_data *)connssl->backend;
(void)data;
DEBUGASSERT(backend);
if(backend->conn) {
/* Send the TLS shutdown if have not done so already and are still
* connected *and* if the peer did not already close the connection. */
if(cf->connected && !connssl->shutdown &&
cf->next && cf->next->connected && !connssl->peer_closed) {
bool done;
(void)cr_shutdown(cf, data, TRUE, &done);
}
rustls_connection_free(backend->conn);
backend->conn = NULL;
}

View file

@ -2482,7 +2482,7 @@ static CURLcode schannel_shutdown(struct Curl_cfilter *cf,
(struct schannel_ssl_backend_data *)connssl->backend;
CURLcode result = CURLE_OK;
if(connssl->shutdown) {
if(cf->shutdown) {
*done = TRUE;
return CURLE_OK;
}
@ -2499,7 +2499,7 @@ static CURLcode schannel_shutdown(struct Curl_cfilter *cf,
connssl->peer.hostname, connssl->peer.port);
}
if(!backend->ctxt || connssl->shutdown) {
if(!backend->ctxt || cf->shutdown) {
*done = TRUE;
goto out;
}
@ -2606,7 +2606,7 @@ static CURLcode schannel_shutdown(struct Curl_cfilter *cf,
}
out:
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -2619,13 +2619,6 @@ static void schannel_close(struct Curl_cfilter *cf, struct Curl_easy *data)
DEBUGASSERT(data);
DEBUGASSERT(backend);
if(backend->cred && backend->ctxt &&
cf->connected && !connssl->shutdown &&
cf->next && cf->next->connected && !connssl->peer_closed) {
bool done;
(void)schannel_shutdown(cf, data, TRUE, &done);
}
/* free SSPI Schannel API security context handle */
if(backend->ctxt) {
DEBUGF(infof(data, "schannel: clear security context handle"));

View file

@ -2575,7 +2575,7 @@ static CURLcode sectransp_shutdown(struct Curl_cfilter *cf,
size_t i;
DEBUGASSERT(backend);
if(!backend->ssl_ctx || connssl->shutdown) {
if(!backend->ssl_ctx || cf->shutdown) {
*done = TRUE;
goto out;
}
@ -2638,7 +2638,7 @@ static CURLcode sectransp_shutdown(struct Curl_cfilter *cf,
}
out:
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -2654,12 +2654,6 @@ static void sectransp_close(struct Curl_cfilter *cf, struct Curl_easy *data)
if(backend->ssl_ctx) {
CURL_TRC_CF(data, cf, "close");
if(cf->connected && !connssl->shutdown &&
cf->next && cf->next->connected && !connssl->peer_closed) {
bool done;
(void)sectransp_shutdown(cf, data, TRUE, &done);
}
#if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
if(SSLCreateContext)
CFRelease(backend->ssl_ctx);

View file

@ -1757,17 +1757,17 @@ static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf,
struct Curl_easy *data,
bool *done)
{
struct ssl_connect_data *connssl = cf->ctx;
struct cf_call_data save;
CURLcode result = CURLE_OK;
*done = TRUE;
if(!connssl->shutdown) {
if(!cf->shutdown) {
struct cf_call_data save;
CF_DATA_SAVE(save, cf, data);
result = Curl_ssl->shut_down(cf, data, TRUE, done);
CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", result, *done);
CF_DATA_RESTORE(cf, save);
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
}
return result;
}
@ -2052,7 +2052,7 @@ static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf,
timediff_t timeout_ms;
int what, loop = 10;
if(connssl->shutdown) {
if(cf->shutdown) {
*done = TRUE;
return CURLE_OK;
}
@ -2091,7 +2091,7 @@ static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf,
}
out:
CF_DATA_RESTORE(cf, save);
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}

View file

@ -94,7 +94,6 @@ struct ssl_connect_data {
int io_need; /* TLS signals special SEND/RECV needs */
BIT(use_alpn); /* if ALPN shall be used in handshake */
BIT(peer_closed); /* peer has closed connection */
BIT(shutdown); /* graceful close notify finished */
};

View file

@ -1357,7 +1357,7 @@ static CURLcode wolfssl_shutdown(struct Curl_cfilter *cf,
int nread, err;
DEBUGASSERT(wctx);
if(!wctx->handle || connssl->shutdown) {
if(!wctx->handle || cf->shutdown) {
*done = TRUE;
goto out;
}
@ -1374,17 +1374,17 @@ static CURLcode wolfssl_shutdown(struct Curl_cfilter *cf,
bool input_pending;
/* Yes, it did. */
if(!send_shutdown) {
connssl->shutdown = TRUE;
CURL_TRC_CF(data, cf, "SSL shutdown received, not sending");
*done = TRUE;
goto out;
}
else if(!cf->next->cft->is_alive(cf->next, data, &input_pending)) {
/* Server closed the connection after its closy notify. It
* seems not interested to see our close notify, so do not
* send it. We are done. */
connssl->peer_closed = TRUE;
connssl->shutdown = TRUE;
CURL_TRC_CF(data, cf, "peer closed connection");
connssl->peer_closed = TRUE;
*done = TRUE;
goto out;
}
}
@ -1435,7 +1435,7 @@ static CURLcode wolfssl_shutdown(struct Curl_cfilter *cf,
}
out:
connssl->shutdown = (result || *done);
cf->shutdown = (result || *done);
return result;
}
@ -1450,11 +1450,6 @@ static void wolfssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
DEBUGASSERT(backend);
if(backend->handle) {
if(cf->connected && !connssl->shutdown &&
cf->next && cf->next->connected && !connssl->peer_closed) {
bool done;
(void)wolfssl_shutdown(cf, data, TRUE, &done);
}
wolfSSL_free(backend->handle);
backend->handle = NULL;
}