TLS\Listener runs TLS handshakes one at a time and without a deadline, so one silent client blocks the listener indefinitely
Description
Impact
Psl\TLS\Listener::accept() accepts a connection from the listener it wraps and runs that connection's TLS handshake before returning, with nothing but the caller's cancellation token to stop it. Accept loops usually call accept() without a token, or with one that only signals shutdown, and Psl\Network\CompositeListener calls the accept() of its inner listeners without one. A client that opens a TCP connection and then sends nothing, or stops partway through its ClientHello, therefore holds accept() for as long as it keeps the connection open, and every later connection waits behind it. A failed handshake also left the plain connection open until it was garbage collected.
Any server that accepts connections from an untrusted network through TLS\Listener is affected. A single TCP connection, with no TLS and no credentials, stops the server from completing any new TLS connection; connections already established keep working. Servers that accept plain streams themselves and run Psl\TLS\Acceptor::accept() per connection, with their own timeout, are not affected.
Patches
Fixed in 6.4.0.
TLS\Listenerruns handshakes concurrently. While anaccept()call waits, the listener takes connections from the inner listener and runs the handshake of each in a fiber of its own, andaccept()returns connections in the order their handshakes complete. A peer that connects and stays silent holds only its own handshake, not the connections behind it.- Each handshake has its own deadline, 10 seconds by default and configurable with the new optional
$handshakeTimeoutconstructor argument (aPsl\DateTime\Duration). The deadline starts once the inner listener has accepted the connection, independently of the caller's token. - At most 256 handshakes are pending at a time, configurable with the new optional
$maxPendingHandshakesconstructor argument (Listener::DEFAULT_MAX_PENDING_HANDSHAKES). Each pending handshake holds a connection, a fiber and a timer, so the limit bounds what silent peers cost. While it is reached, the listener takes no further connection from the inner listener, and new connections wait in the inner listener's backlog until a handshake ends. - A handshake that fails or exceeds its deadline closes its connection and is reported to the next
accept()call asHandshakeFailedException, with theCancelledExceptionthat ended an exceeded deadline as the previous exception, so accept loops that already skip failed handshakes keep serving. - Cancelling an
accept()call now ends that call only; pending handshakes run to completion or to their deadline.close()cancels every pending handshake and closes its connection.
The limit bounds cost, not every flood: a client that opens silent connections faster than the limit divided by the deadline (about 26 a second with the defaults) keeps every slot busy, and later connections wait until a slot frees, as with any server that limits concurrent connections. Servers exposed to untrusted clients can shorten $handshakeTimeout, raise $maxPendingHandshakes and limit connections per client address.
Workarounds
Before 6.4.0, accept plain streams from the inner listener and run each handshake in its own fiber with its own timeout, instead of using TLS\Listener (6.4.0 does this inside TLS\Listener):
$acceptor = new TLS\Acceptor($configuration);
while (true) {
$stream = $listener->accept();
Async\run(static function () use ($acceptor, $stream): void {
try {
$tls = $acceptor->accept($stream, new Async\TimeoutCancellationToken(DateTime\Duration::seconds(10)));
} catch (Throwable) {
$stream->close();
return;
}
// ... handle $tls
})->ignore();
}References
- RFC 8446 (TLS 1.3), which leaves how long a server waits for a client's handshake messages to the implementation
- github.com/
php-standard-library/ php-standard-library/ releases/ tag/ 6.4.0