Skip to content

fix: add connection limiting and idle timeout to prevent TCP DoS (issue #3103) - #3116

Open
omerbek wants to merge 2 commits into
pymodbus-dev:devfrom
omerbek:fix/issue-3103-tcp-dos
Open

omerbek wants to merge 2 commits into
pymodbus-dev:devfrom
omerbek:fix/issue-3103-tcp-dos

Conversation

@omerbek

@omerbek omerbek commented Dec 5, 2026

Copy link
Copy Markdown

Summary

Fixed DoS vulnerability by implementing connection limiting and idle timeout. Added max_silence parameter for idle connection timeout (default 300s), enforced max_connections limit at TCP level, and added connection tracking with proper cleanup.

Root Cause

ModbusTcpServer accepted an unlimited number of concurrent TCP connections with no maximum connection count and no idle connection timeout (timeout_connect=0.0). An attacker on the same network can exhaust server file descriptors and memory by opening hundreds of idle TCP connections, causing complete denial of service for legitimate clients.

Solution

Implemented three-layer protection:

1. Connection Limit Enforcement (pymodbus/transport/transport.py)

  • Changed default max_connections from 0 to 100 in CommParams
  • Added is_full() method to check connection count
  • Added handle_new_connection() that refuses connections when limit reached (returns RefusedConnection)
  • Connection counting happens at TCP accept level via connection_made() / connection_lost()

2. Idle Connection Timeout (pymodbus/transport/transport.py)

  • Added max_silence parameter (default 300s / 5 minutes) to CommParams
  • Added last_activity timestamp tracking on data send/receive
  • Added _silence_monitor() background task that closes idle connections
  • Automatic cleanup of monitoring task on connection close

3. Server Integration (pymodbus/server/server.py, pymodbus/server/requesthandler.py)

  • All server types (TCP, TLS, UDP, Serial) now accept max_silence parameter
  • Default max_silence=300.0 (5 minutes) for all servers
  • ServerRequestHandler properly passes max_silence to transport layer

Changes

  • pymodbus/transport/transport.py: Core connection limiting and idle timeout logic
  • pymodbus/server/server.py: All server classes accept max_silence parameter
  • pymodbus/server/requesthandler.py: Passes max_silence to transport

Testing

  • All existing tests pass (1010+ tests in pdu, framer, server modules)
  • Manual verification:
    • Connection limit enforcement: 5 concurrent connections with max=2 only 2 accepted
    • Idle timeout: Connection closed after 5s of inactivity with max_silence=5
    • Concurrent connection handling works correctly

Backward Compatibility

  • max_connections=0 still means no limit (explicit opt-out)
  • max_silence=0 disables idle timeout (explicit opt-out)
  • Default values are conservative but reasonable

Security Impact

This fix resolves a DoS vulnerability where an attacker could:

  1. Open unlimited connections to exhaust file descriptors
  2. Keep connections idle indefinitely to exhaust memory
    Both attack vectors are now mitigated.

Fixes #3103

…ipulation (issue pymodbus-dev#3104)

- Change ModbusControlBlock to store delimiters per device_id using a dictionary
- Update ChangeAsciiInputDelimiterRequest to use device-specific delimiter
- Modify FramerAscii to use device-specific delimiters for encode/decode
- Add backward-compatible Delimiter property for device_id=0
- Fixes security vulnerability where a single client could disrupt all Modbus ASCII connections
pymodbus-dev#3103)

- Add max_silence parameter for idle connection timeout (default 300s)
- Change default max_connections from 0 to 100 in CommParams
- Add is_full() and handle_new_connection() for connection limiting
- Add last_activity tracking and _silence_monitor() for idle timeout
- Update all server types (TCP, TLS, UDP, Serial) to accept max_silence
- Fixes DoS vulnerability where attackers could exhaust file descriptors and memory

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unlimited TCP Connection Acceptance

1 participant