TCP server: connections that never finish TLS bypass max-clients since 66f8cdb9fd, ASSERT in multi_assign_peer_id exits the server
Describe the bug
Since commit 66f8cdb9fd, a TCP connection is no longer added to m->hash when it is accepted. The max-clients check in multi_create_instance() counts m->hash, so it no longer sees connections that have not finished TLS. multi_create_instance_tcp() still gives every new instance a peer slot through multi_assign_peer_id(). Once max-clients instances are open, the next accepted connection finds no free slot, rx_peer_id stays at MAX_PEER_ID, and ASSERT(rx_peer_id < max_clients) at multi.c:4216 exits the server. No TLS handshake or authentication is needed to trigger it.
To Reproduce
- Create a throwaway CA and server certificate. Any pair works, because the handshake never completes, so the keys are not secret.
- Start the server with this config (TCP server, dev null, no tun):
mode server
tls-server
proto tcp-server
port 11940
local 127.0.0.1
dev null
ca ca.crt
cert srv.crt
key srv.key
dh none
max-clients 100
- Run this script, which opens 101 TCP connections and sends nothing:
import socket, time
socks = [socket.create_connection(("127.0.0.1", 11940)) for _ in range(101)]
time.sleep(3)
- HEAD (dda75df) server log:
Assertion failed at multi.c:4216 (mi->context.c2.tls_multi->rx_peer_id < m->max_clients)
Exiting due to fatal error
The process exits with status 1, so every tunnel on the server drops.
- The same config and script on v2.7.8 logs the refusal and keeps running:
MULTI: new incoming connection would exceed maximum number of clients (100)
The 101st socket is closed and the server keeps running.
Expected behavior
The 101st connection should be refused at the max-clients check, as it is in v2.7.8, and the server should keep running.
Version information (please complete the following information):
- OS: Fedora 44 (x86_64)
- OpenVPN version: HEAD dda75df (2.8_git), built with --disable-lzo --disable-lz4 --disable-systemd --disable-plugins --disable-management --disable-pkcs11 --disable-dco --disable-selinux --disable-plugin-auth-pam. Regression from 66f8cdb9fd. v2.7.8 is not affected.
- Repeat for peer if relevant: not relevant. No client needs to connect or authenticate; the crash happens at accept time.
Additional context
In v2.7.8, multi_create_instance_tcp() calls hash_add_fast() on &mi->real right after creating the instance, so pending TCP connections count toward the m->hash check in multi_create_instance(). Commit 66f8cdb9fd removes hashing of TCP connections by source address. Its commit message says this is intended, but the admission check depended on that hash. TCP instances are now added to m->hash only after auth (ASSERT(hash_add(m->hash, ...)) at multi.c:2504), so the check no longer counts them. multi_assign_peer_id() still asserts when no slot is free, and its caller still runs for every new TCP instance.
Possible fixes, for maintainers to choose (neither has been tested):
- Count pending TCP instances in the admission check, for example by checking the occupied m->instances[] slots, so the refusal happens before an instance is created.
- Or make a failed peer-id assignment in multi_create_instance_tcp() close the new instance instead of asserting. The comment above the ASSERT already says it should not be reached.
Impact: any TCP-mode server with max-clients N can be made to exit with N+1 idle connections from one host. The packaged openvpn-server@ unit has Restart=on-failure, so repeating the connections could keep the service down. That restart loop was not tested. The released 2.7.8 is not affected.
The referenced commit is yours, @schwabe, Could you please take a look?
I pushed a fix for this issue to Gerrit: https://gerrit.openvpn.net/c/openvpn/+/2064 Can you take a look if that fixes the problem for you?
As a side note, there is a related issue that clients will not show up in management status or --status file that is fixed up by the follow up patch https://gerrit.openvpn.net/c/openvpn/+/2065