Skip to content

Commit

Permalink
Merge pull request #40 from jtt/fix-out-of-bounds-label
Browse files Browse the repository at this point in the history
Add missing length check when reading label pointer
  • Loading branch information
dmachard authored Feb 9, 2022
2 parents 5e0e439 + 792350b commit 661fd12
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dnsutils/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ func _ParseLabels(offset int, payload []byte, pointers map[uint16]int) (string,
}
// label pointer support ?
if length>>6 == 3 {
if offset+1 >= len(payload) {
return "", 0, ErrDecodeDnsLabelTooShort
}
ptr := binary.BigEndian.Uint16(payload[offset:offset+2]) & 16383
_, exist := pointers[ptr]
if exist {
Expand Down
8 changes: 8 additions & 0 deletions dnsutils/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,14 @@ func TestDecodeDnsQuestion_QtypeMissing(t *testing.T) {
}
}

func TestDecodeQuestion_InvalidPointer(t *testing.T) {
decoded := []byte{88, 27, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 202}
_, _, _, err := DecodeQuestion(decoded)
if !errors.Is(err, ErrDecodeDnsLabelTooShort) {
t.Errorf("bad error returned: %v", err)
}
}

func TestDecodeDnsAnswer_PacketTooShort(t *testing.T) {
payload := []byte{46, 172, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 15, 100, 110, 115, 116, 97, 112, 99, 111, 108, 108, 101, 99, 116,
111, 114, 4, 116, 101, 115, 116, 0, 0, 1, 0, 1, 15, 100, 110, 115, 116, 97, 112, 99, 111, 108, 108, 101, 99, 116,
Expand Down

0 comments on commit 661fd12

Please sign in to comment.