Skip to content

Commit d23b029

Browse files
committed
fix: bound record payload reads against rdlength overrun
1 parent b22c8ff commit d23b029

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎src/zeroconf/_protocol/incoming.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,27 @@ def _read_character_string(self) -> str:
254254
"""Reads a character string from the packet"""
255255
length = self.view[self.offset]
256256
self.offset += 1
257+
# Python slicing silently truncates when indices exceed the buffer,
258+
# but self.offset still advances by the declared length below; without
259+
# this check a record with an inflated character-string length would
260+
# land in the cache carrying a payload shorter than the wire claimed
261+
# and leave the parser pointed past _data_len for the next record.
262+
if self.offset + length > self._data_len:
263+
raise IncomingDecodeError(
264+
f"Character string length {length} at offset {self.offset} overruns "
265+
f"packet of {self._data_len} bytes from {self.source}"
266+
)
257267
info = self.data[self.offset : self.offset + length].decode("utf-8", "replace")
258268
self.offset += length
259269
return info
260270

261271
def _read_string(self, length: _int) -> bytes:
262272
"""Reads a string of a given length from the packet"""
273+
if self.offset + length > self._data_len:
274+
raise IncomingDecodeError(
275+
f"String length {length} at offset {self.offset} overruns "
276+
f"packet of {self._data_len} bytes from {self.source}"
277+
)
263278
info = self.data[self.offset : self.offset + length]
264279
self.offset += length
265280
return info

‎tests/test_protocol.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,58 @@ def test_nsec_bitmap_truncated_window_header_rejected():
886886
assert not any(isinstance(a, r.DNSNsec) for a in answers)
887887

888888

889+
def test_txt_rdlength_overruns_packet_rejected():
890+
"""A TXT record with rdlength past the buffer must not enter the cache.
891+
892+
Python slicing silently truncates when the slice end exceeds the buffer,
893+
so without a bounds check in ``_read_string`` a malformed wire frame
894+
would land in the cache carrying a payload shorter than the rdlength
895+
declared, leaving the parser desynchronized for downstream records.
896+
"""
897+
packet = (
898+
b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00"
899+
b"\x04test\x05local\x00"
900+
b"\x00\x10\x80\x01"
901+
b"\x00\x00\x11\x94"
902+
b"\xff\xff"
903+
b"\x05hello"
904+
)
905+
parsed = r.DNSIncoming(packet)
906+
assert parsed.valid
907+
assert parsed.answers() == []
908+
909+
910+
def test_hinfo_character_string_length_overruns_record_rejected():
911+
"""A HINFO character string declaring more bytes than remain must be rejected."""
912+
packet = (
913+
b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00"
914+
b"\x04test\x05local\x00"
915+
b"\x00\x0d\x80\x01"
916+
b"\x00\x00\x11\x94"
917+
b"\x00\x07"
918+
b"\x03cpu"
919+
b"\xff\xff\xff"
920+
)
921+
parsed = r.DNSIncoming(packet)
922+
assert parsed.valid
923+
assert not any(isinstance(a, r.DNSHinfo) for a in parsed.answers())
924+
925+
926+
def test_a_record_rdlength_overruns_packet_rejected():
927+
"""An A record whose 4-byte address would walk past the buffer must be rejected."""
928+
packet = (
929+
b"\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00"
930+
b"\x04test\x05local\x00"
931+
b"\x00\x01\x80\x01"
932+
b"\x00\x00\x11\x94"
933+
b"\x00\x04"
934+
b"\xc0\xa8"
935+
)
936+
parsed = r.DNSIncoming(packet)
937+
assert parsed.valid
938+
assert not any(isinstance(a, r.DNSAddress) for a in parsed.answers())
939+
940+
889941
def test_records_same_packet_share_fate():
890942
"""Test records in the same packet all have the same created time."""
891943
out = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA)

0 commit comments

Comments
 (0)