Add bound checks to the CoAP option parser
The option-parsing loop in createFromFrame lacks bound checks.
- The loop condition used "i != reply.size()". Because the index can
advance by more than one per iteration (extended delta/length bytes
and the option value length), it could step over reply.size() without
ever equalling it, leaving the condition true and reading pduData[i]
out of bounds.
- The extended delta/length branches did "++i" and then read pduData[i]
without checking that the extra byte was actually present.
- The option value length was not validated against the remaining
buffer, so an option claiming more bytes than were sent was accepted
with a silently truncated value. QByteArray::mid() clamps rather than
reading out of bounds, so this is an integrity problem rather than a
crash, but the resulting reply is delivered as if it were complete.
Change the loop condition to "i < reply.size()", reject the frame
(return nullptr) when an extended length byte or an option value would
run past the end of the buffer, and make internalReply a std::unique_ptr
so these mid-loop rejections do not leak the partially built reply.
Task-number: QTBUG-149345
Pick-to: 6.12 6.11 6.8 6.5 5.15
Change-Id: I609145015127ac9fcf87880fcfbeee74b4c22556
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>