Skip to content

_modbus_receive_msg: use a deadline for the receive timeout - #882

Open
StalderT wants to merge 1 commit into
stephane:masterfrom
StalderT:fix/receive-deadline
Open

StalderT wants to merge 1 commit into
stephane:masterfrom
StalderT:fix/receive-deadline

Conversation

@StalderT

Copy link
Copy Markdown
Contributor

_modbus_receive_msg() passes the same timeval to every select() call and relies on select() to decrement it. Only Linux does that. On Windows, macOS and the BSDs the full timeout starts again after each received byte, so when the byte timeout is disabled a peer sending one byte just before each expiry keeps the receive going for as long as it wants (CVE-2026-51539).

The response or indication timeout now sets an absolute deadline on a monotonic clock, and each select() gets the time left. When the byte timeout is enabled nothing changes: it takes over after the first byte as before.

Fixes #843

_modbus_receive_msg() passes the same timeval to every select() call and relies on select() to decrement it. Only Linux does that. On Windows, macOS and the BSDs the full timeout starts again after each received byte, so when the byte timeout is disabled a peer sending one byte just before each expiry keeps the receive going for as long as it wants (CVE-2026-51539).

The response or indication timeout now sets an absolute deadline on a monotonic clock, and each select() gets the time left. When the byte timeout is enabled nothing changes: it takes over after the first byte as before.

Fixes stephane#843
@cla-bot

cla-bot Bot commented Sep 29, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement. In order for us to review and merge your code, please fill https://forms.gle/5635zjphDo5JEJQSA to get added. Your document will be manually checked by the maintainer. Be patient...

@niun

niun commented Nov 5, 2026

Copy link
Copy Markdown

This affects clients too, not only servers. When the byte timeout is disabled, the modbus_set_response_timeout() documentation says "the full confirmation response must be received before expiration of the response timeout". On Windows that doesn't hold.

Test with v3.2.0 (on Windows 11 x64, MSVC 2022, with the FD_SETSIZE checks from #863 disabled so TCP can connect): the response timeout is 500 ms, the byte timeout is disabled, and the server sends the response in 3 parts, 400 ms apart. The read succeeds after 1229 ms:

Waiting for a confirmation...
<00><01><00><00><00><05><FF><03><02><12><34>
RESULT modbus_read_registers rc=1 ok after 1229 ms (response timeout 500 ms, byte timeout 0)

The Win32 RTU backend has the same problem. win32_ser_select() takes a const struct timeval * and sets the COM port timeouts from it, so every wait gets the full timeout again. This PR recomputes tv in _modbus_receive_msg() before each backend select() call, so it should fix RTU as well.

We build 3.2.0 with an equivalent change for confirmations: a deadline from QueryPerformanceCounter(), and the remaining time before each wait. With it, the same test fails as documented, with ETIMEDOUT after 510 ms.

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.

Slowloris-Style DoS in Modbus TCP Receive Path on Windows

2 participants