Validate DGUS read variable lengths - #28502
Conversation
Reject malformed READVAR frames before reading address, length, or payload data. Compare declared and received payload lengths and use widened length types in the E3S1Pro and Reloaded parsers.
|
@quietcode-org, please note Note: Marlin code is for microcontrollers. It does not need such validation for every single thing. That could increase the codebase to exceed the limited resources of the microcontrollers. As long as the code handles errors ok, that is all that is required. |
|
quietcode-org ist my coding account, cyberman is for research and management. Thank you for taking the time to point this out, especially given how little maintainer capacity Marlin currently has. I do understand that every additional review request consumes time that is already in very short supply. I also agree with your broader point: Marlin runs on constrained microcontrollers, including AVR targets, so indiscriminate defensive validation would be the wrong direction. I am not trying to add checks for every theoretically possible condition. The reason I considered this particular validation important is that it sits directly at the serial protocol boundary. The Your concern about code size prompted me to measure the actual binaries rather than rely on intuition. The results were:
So the maximum added cost in any one configuration is 48 bytes, not the sum of all three variants. Part of my background is AmigaOS, where treating memory and CPU time as scarce resources—and making every added byte justify itself—is deeply ingrained. That is also why I take the AVR concern seriously. At the same time, that background has taught me to be cautious at external data boundaries, because a very small check there can prevent failures that are much harder to diagnose later. I hope the measurements help put the trade-off into perspective. I am certainly open to a smaller implementation if the same boundary can be protected with fewer bytes, but I believe leaving the demonstrated out-of-bounds reads unhandled would be the less robust choice. |
Problem
The DGUS
READVARhandlers could access the VP address, word count, or payload before confirming that those bytes were present. Truncated or over-declared frames could therefore read beyond the received datagram in the Classic, E3S1Pro, and Reloaded implementations.Fix
Require
READVARframes to contain the VP address and word count, and require the declared payload length to match the received payload before accessing it.The E3S1Pro and Reloaded string paths now keep the payload length wide enough for the declared word count. Valid three-byte
0x82/OKacknowledgements are unchanged.Validation
Malformed and over-declared
READVARframes reproduced out-of-bounds reads before the fix and are rejected after it.mega2560ext, test 2 (DGUS MKS): passedmega2560ext, test 3 (DGUS Reloaded): passedSTM32F401RC_creality, test 99 (DGUS E3S1Pro): passedNo device was accessed.