Fix DoS vulnerability: validate quantity field in read request handlers - #359
Open
jamesgol wants to merge 2 commits into
Open
Fix DoS vulnerability: validate quantity field in read request handlers#359jamesgol wants to merge 2 commits into
jamesgol wants to merge 2 commits into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 uncaughtRangeErrorthat 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
exceeds the register buffer
the pattern already used by the write handlers
crash payload