Skip to content

Fix DoS vulnerability: validate quantity field in read request handlers - #359

Open
jamesgol wants to merge 2 commits into
Cloud-Automation:v4.0-devfrom
jamesgol:fix/validate-read-quantity
Open

Fix DoS vulnerability: validate quantity field in read request handlers#359
jamesgol wants to merge 2 commits into
Cloud-Automation:v4.0-devfrom
jamesgol:fix/validate-read-quantity

Conversation

@jamesgol

Copy link
Copy Markdown

Summary

An unauthenticated remote attacker can crash a jsmodbus TCP server with a single crafted Modbus packet. The server
does not validate the quantity field in read requests (FC01-FC04) against the Modbus specification limits. For
FC03/FC04 (Read Holding/Input Registers), a quantity greater than 127 produces a byte count exceeding 255, causing
Buffer.writeUInt8() to throw an uncaught RangeError that terminates the Node.js process.

I originally discovered and reported this vulnerability to the maintainers in October 2022, discovered during the Cyberforce competition. I've followed up several times since then but have not received a response.

Changes

  • Quantity range validation per the Modbus Application Protocol Specification:
    • FC01/FC02 (Read Coils/Discrete Inputs): 1–2000 (0x0001–0x07D0)
    • FC03/FC04 (Read Holding/Input Registers): 1–125 (0x0001–0x007D)
  • Address bounds validation for FC03/FC04: returns exception code 0x02 (Illegal Data Address) when start + count
    exceeds the register buffer
  • Out-of-range requests now return a proper Modbus exception response (error code 0x03, Illegal Data Value), matching
    the pattern already used by the write handlers
  • 9 regression tests covering all validated function codes, zero quantity, out-of-bounds addressing, and the exact
    crash payload

jamesgol added 2 commits July 25, 2026 22:56
…nt DoS

The server response handler did not validate the quantity field in read
requests (FC01-FC04) against the Modbus specification limits. For FC03/FC04
(Read Holding/Input Registers), a quantity greater than 127 produces a byte
count exceeding 255, which causes Buffer.writeUInt8() to throw an uncaught
RangeError that crashes the Node.js process. A single crafted packet from
an unauthenticated client is sufficient to terminate the server.

Add quantity range validation per the Modbus spec:
- FC01/FC02 (Read Coils/Discrete Inputs): 1-2000 (0x0001-0x07D0)
- FC03/FC04 (Read Holding/Input Registers): 1-125 (0x0001-0x007D)

Also add address bounds checks for FC03/FC04 to return a Modbus exception
(error code 0x02) when start + count exceeds the register buffer, instead
of reading out of bounds.

Out-of-range requests now receive a proper Modbus exception response
(error code 0x03, Illegal Data Value) matching the pattern used by the
existing write handlers.
Tests cover:
- FC01/FC02: reject quantity of 0 (below minimum)
- FC03/FC04: reject quantity > 125 (Modbus spec limit)
- FC03: reject quantity of 0
- FC03/FC04: reject start + count exceeding register buffer (error 0x02)
- FC03: DoS regression test using the exact payload from the vulnerability
  disclosure (quantity 617, byte count 1234 overflows UInt8)
- FC03: valid request at max buffer capacity still succeeds

Also adds input and discrete buffers to the test server setup to
enable FC02 and FC04 testing.
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.

1 participant