File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -312,6 +312,19 @@ def _read_others(self) -> None:
312312 self .data ,
313313 exc_info = True ,
314314 )
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
315328 if rec is not None :
316329 self ._answers .append (rec )
317330
Original file line number Diff line number Diff line change @@ -938,6 +938,37 @@ def test_a_record_rdlength_overruns_packet_rejected():
938938 assert not any (isinstance (a , r .DNSAddress ) for a in parsed .answers ())
939939
940940
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"\x04 test\x05 local\x00 "
954+ b"\x00 \x0d \x80 \x01 "
955+ b"\x00 \x00 \x11 \x94 "
956+ b"\x00 \x02 "
957+ b"\x01 x"
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+
941972def test_records_same_packet_share_fate ():
942973 """Test records in the same packet all have the same created time."""
943974 out = r .DNSOutgoing (const ._FLAGS_QR_QUERY | const ._FLAGS_AA )
You can’t perform that action at this time.
0 commit comments