AcceptEx and the pitfalls of the Windows socket API
Summary
While debugging in 2008, Felix von Leitner came across a problem with the Linux socket option TCP_DEFER_ACCEPT that resembled a previously described weakness of AcceptEx on Windows. Both mechanisms only report a new connection once the client has also sent data. On Linux, however, such silent connections count against the listen() queue, and when it is full, the server no longer accepts new connections.
What it is about technically
A server calls listen() with a maximum queue length, the backlog, and picks up new connections with accept(). TCP_DEFER_ACCEPT is an optimisation: the kernel only wakes the server when data has actually arrived, which saves work for web servers. On Windows, AcceptEx with overlapped I/O serves a similar purpose, and there a single silent client was enough to block the server.
Von Leitner saw 16 half-open connections in netstat, exactly as many as the backlog of 16. The silent connections occupied the queue without the server being able to accept them. The pattern resembles a SYN flood: an attacker only has to open enough connections and send nothing.
What you can learn from it
Optimisations in the kernel change behaviour under attack conditions. What saves computing time in normal operation can enable a simple denial of service. Anyone using such options should choose generous backlogs and timeouts and test the behaviour with silent clients.
Ideas
- TCP_DEFER_ACCEPT only wakes the server once a client has sent data.
- Silent connections can fill the listen queue.
- Similar optimisations on Windows and Linux show similar weaknesses.
Insights
- Performance optimisations must also be checked under attack conditions.
- A small backlog makes a server vulnerable to simple denial-of-service attacks.
Facts
- The backlog in the test was 16 connections.
- TCP_DEFER_ACCEPT is a Linux-specific socket option.
- Von Leitner uses the option via libowfat in the gatling web server.
References
Remarks
- The post describes an observation made while debugging, not a complete analysis of the kernel’s behaviour.
Recommendations
- Set a generous listen() backlog for publicly reachable servers.
- Also test servers with clients that open connections and send nothing.
Found a mistake or have something to add? Get in touch.
Links to the original source and the Web Archive open in a new tab.