Skip to content

Commit 5444495

Browse files
authored
fix: bound record payload reads against rdlength overrun (#1756)
1 parent 28bb01f commit 5444495

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

‎src/zeroconf/_protocol/incoming.py‎

Lines changed: 28 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
@@ -297,6 +312,19 @@ def _read_others(self) -> None:
297312
self.data,
298313
exc_info=True,
299314
)
315+
if rec is not None and self.offset != end:
316+
# The decoded record consumed a different number of bytes than
317+
# rdlength advertised. The record is built from a slice that
318+
# straddles its rdata boundary, so drop it and resync to the
319+
# declared end so the next record header lands aligned.
320+
log.debug(
321+
"Record for %s with type %s did not consume exactly rdlength=%d; dropping",
322+
domain,
323+
_TYPES.get(type_, type_),
324+
length,
325+
)
326+
self.offset = end
327+
rec = None
300328
if rec is not None:
301329
self._answers.append(rec)
302330

‎tests/test_protocol.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,89 @@ 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+
941+
def test_record_consuming_more_than_rdlength_dropped_and_resyncs():
942+
"""A record whose decoded fields overrun its rdlength must drop and resync.
943+
944+
The first answer is a HINFO with ``rdlength=2`` and rdata ``\\x01x`` (one
945+
char string ``"x"``). The second character string's length byte then comes
946+
from the next record's name (``\\x00``, root domain), so the HINFO would
947+
silently parse as ``cpu="x", os=""`` but leave the offset one byte past
948+
the declared end, smearing the second record's framing. With the per-record
949+
boundary check the bogus HINFO is dropped and the second record decodes.
950+
"""
951+
packet = (
952+
b"\x00\x00\x84\x00\x00\x00\x00\x02\x00\x00\x00\x00"
953+
b"\x04test\x05local\x00"
954+
b"\x00\x0d\x80\x01"
955+
b"\x00\x00\x11\x94"
956+
b"\x00\x02"
957+
b"\x01x"
958+
b"\x00"
959+
b"\x00\x0c\x00\x01"
960+
b"\x00\x00\x11\x94"
961+
b"\x00\x02"
962+
b"\xc0\x0c"
963+
)
964+
parsed = r.DNSIncoming(packet)
965+
answers = parsed.answers()
966+
assert not any(isinstance(a, r.DNSHinfo) for a in answers)
967+
ptrs = [a for a in answers if isinstance(a, r.DNSPointer)]
968+
assert len(ptrs) == 1
969+
assert ptrs[0].alias == "test.local."
970+
971+
889972
def test_records_same_packet_share_fate():
890973
"""Test records in the same packet all have the same created time."""
891974
out = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA)

0 commit comments

Comments
 (0)