Skip to content

Commit

Permalink
Fix two panics identified by fuzz testing
Browse files Browse the repository at this point in the history
This commit adds additional bounds checking, one for DNSResourceRecord
and another for DNSQuestion. The DNSResourceRecord panic was observed in
production and the second panic was caught by fuzzing.

Add a fuzz test with seeds to reproduce these two panics.
  • Loading branch information
cirego authored and gconnell committed Aug 10, 2022
1 parent 4e29164 commit 65a1dfb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions layers/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ func (q *DNSQuestion) decode(data []byte, offset int, df gopacket.DecodeFeedback
return 0, err
}

if len(data) < endq+4 {
return 0, errors.New("DNS question too small")
}

q.Name = name
q.Type = DNSType(binary.BigEndian.Uint16(data[endq : endq+2]))
q.Class = DNSClass(binary.BigEndian.Uint16(data[endq+2 : endq+4]))
Expand Down Expand Up @@ -709,6 +713,10 @@ func (rr *DNSResourceRecord) decode(data []byte, offset int, df gopacket.DecodeF
return 0, err
}

if len(data) < endq+10 {
return 0, errors.New("DNS record too small")
}

rr.Name = name
rr.Type = DNSType(binary.BigEndian.Uint16(data[endq : endq+2]))
rr.Class = DNSClass(binary.BigEndian.Uint16(data[endq+2 : endq+4]))
Expand Down
7 changes: 7 additions & 0 deletions layers/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import (
"github.com/google/gopacket"
)

func FuzzDecodeFromBytes(f *testing.F) {
f.Fuzz(func(t *testing.T, bytes []byte) {
dns := DNS{}
dns.DecodeFromBytes(bytes, gopacket.NilDecodeFeedback)
})
}

// it have a layer like that:
// name: xxx.com
// type: CNAME
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000000\x10\x10\x00\x01\x01\x01\x01\x01\x01\x00")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("000000000000\x00")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("01000\x10\x10\xdfd\x01\x01\x01\x00d\x01\x01\x01\x00")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("0000\x00\x00000000\x010\x000")

0 comments on commit 65a1dfb

Please sign in to comment.