Skip to content

Commit 2db4d0d

Browse files
committed
Validate buffer length before reading fields in Packet::readFrom
readFrom reads the header byte, transport codes (4 bytes), and path_len from the source buffer before any length validation. With a short input, these reads go past the end of the buffer. Add upfront length checks: minimum 2 bytes overall, transport codes require 4 additional bytes, and path must fit before the remaining payload.
1 parent e6e87fb commit 2db4d0d

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

src/Packet.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ uint8_t Packet::writeTo(uint8_t dest[]) const {
3939
}
4040

4141
bool Packet::readFrom(const uint8_t src[], uint8_t len) {
42+
if (len < 2) return false; // minimum: header + path_len
4243
uint8_t i = 0;
4344
header = src[i++];
4445
if (hasTransportCodes()) {
46+
if (i + 4 >= len) return false; // need 4 bytes for transport codes + path_len after
4547
memcpy(&transport_codes[0], &src[i], 2); i += 2;
4648
memcpy(&transport_codes[1], &src[i], 2); i += 2;
4749
} else {
4850
transport_codes[0] = transport_codes[1] = 0;
4951
}
5052
path_len = src[i++];
5153
if (path_len > sizeof(path)) return false; // bad encoding
54+
if (i + path_len >= len) return false; // path + at least 1 byte payload must fit
5255
memcpy(path, &src[i], path_len); i += path_len;
53-
if (i >= len) return false; // bad encoding
5456
payload_len = len - i;
5557
if (payload_len > sizeof(payload)) return false; // bad encoding
5658
memcpy(payload, &src[i], payload_len); //i += payload_len;

0 commit comments

Comments
 (0)