Skip to content

Commit 65a1dfb

Browse files
ciregogconnell
authored andcommitted
Fix two panics identified by fuzz testing
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.
1 parent 4e29164 commit 65a1dfb

6 files changed

+23
-0
lines changed

layers/dns.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ func (q *DNSQuestion) decode(data []byte, offset int, df gopacket.DecodeFeedback
639639
return 0, err
640640
}
641641

642+
if len(data) < endq+4 {
643+
return 0, errors.New("DNS question too small")
644+
}
645+
642646
q.Name = name
643647
q.Type = DNSType(binary.BigEndian.Uint16(data[endq : endq+2]))
644648
q.Class = DNSClass(binary.BigEndian.Uint16(data[endq+2 : endq+4]))
@@ -709,6 +713,10 @@ func (rr *DNSResourceRecord) decode(data []byte, offset int, df gopacket.DecodeF
709713
return 0, err
710714
}
711715

716+
if len(data) < endq+10 {
717+
return 0, errors.New("DNS record too small")
718+
}
719+
712720
rr.Name = name
713721
rr.Type = DNSType(binary.BigEndian.Uint16(data[endq : endq+2]))
714722
rr.Class = DNSClass(binary.BigEndian.Uint16(data[endq+2 : endq+4]))

layers/dns_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
"github.com/google/gopacket"
1616
)
1717

18+
func FuzzDecodeFromBytes(f *testing.F) {
19+
f.Fuzz(func(t *testing.T, bytes []byte) {
20+
dns := DNS{}
21+
dns.DecodeFromBytes(bytes, gopacket.NilDecodeFeedback)
22+
})
23+
}
24+
1825
// it have a layer like that:
1926
// name: xxx.com
2027
// type: CNAME
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
[]byte("0000000\x10\x10\x00\x01\x01\x01\x01\x01\x01\x00")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
[]byte("000000000000\x00")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
[]byte("01000\x10\x10\xdfd\x01\x01\x01\x00d\x01\x01\x01\x00")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
go test fuzz v1
2+
[]byte("0000\x00\x00000000\x010\x000")

0 commit comments

Comments
 (0)