2019-05-19 03:23:50 +10:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestPackDataOpt tests generated using fuzz.go and with a message pack
|
|
|
|
// containing the following bytes:
|
|
|
|
// "0000\x00\x00000000\x00\x00/00000" +
|
|
|
|
// "0\x00\v\x00#\b00000000\x00\x00)000" +
|
|
|
|
// "000\x00\x1c00\x00\x0000\x00\x01000\x00\x00\x00\b" +
|
|
|
|
// "\x00\v\x00\x02\x0000000000"
|
|
|
|
// That bytes sequence created the overflow error.
|
|
|
|
func TestPackDataOpt(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
option []EDNS0
|
|
|
|
msg []byte
|
|
|
|
off int
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want int
|
|
|
|
wantErr bool
|
|
|
|
wantErrMsg string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "overflow",
|
|
|
|
args: args{
|
|
|
|
option: []EDNS0{
|
|
|
|
&EDNS0_LOCAL{Code: 0x3030, Data: []uint8{}},
|
|
|
|
&EDNS0_LOCAL{Code: 0x3030, Data: []uint8{0x30}},
|
|
|
|
&EDNS0_LOCAL{Code: 0x3030, Data: []uint8{}},
|
|
|
|
&EDNS0_SUBNET{
|
|
|
|
Code: 0x0, Family: 0x2,
|
|
|
|
SourceNetmask: 0x0, SourceScope: 0x30,
|
|
|
|
Address: net.IP{0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}},
|
|
|
|
},
|
|
|
|
msg: []byte{
|
|
|
|
0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x2,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x30,
|
|
|
|
0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x0b, 0x00,
|
|
|
|
0x23, 0x08, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
|
|
|
|
0x30, 0x30, 0x00, 0x00, 0x29, 0x30, 0x30, 0x30,
|
|
|
|
0x30, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x00,
|
|
|
|
0x00, 0x30, 0x30, 0x00, 0x01, 0x30, 0x00, 0x00,
|
|
|
|
0x00,
|
|
|
|
},
|
|
|
|
off: 54,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
wantErrMsg: "dns: overflow packing opt",
|
|
|
|
want: 57,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := packDataOpt(tt.args.option, tt.args.msg, tt.args.off)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("packDataOpt() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil && tt.wantErrMsg != err.Error() {
|
|
|
|
t.Errorf("packDataOpt() error msg = %v, wantErrMsg %v", err.Error(), tt.wantErrMsg)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("packDataOpt() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
[nsec] compute NSEC.len() the same way that we would do in packDataNsec (#967)
The byte sequence, when Unpack()-ed and subsequential Pack()-ed created a
panic: runtime error: slice bounds out of range
github.com/miekg/dns.(*Msg).packBufferWithCompressionMap(0xc0000d4000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x100, 0x14, 0x14e80b, 0xbf2d4654d501a3c8, ...)
/Users/chantra/go/src/github.com/miekg/dns/msg.go:868 +0x13a8
Confirmed that Unpacking/Repacking payload described in TestCrashNSEC
did not raise a slice bound out of range panic, added unittests which
failed prior to this change.
```
go test -run TestCrashNSEC
--- FAIL: TestCrashNSEC (0.00s)
types_test.go:135: expected length of 19, got 12
FAIL
exit status 1
FAIL github.com/miekg/dns 0.067s
```
2019-05-19 17:40:22 +10:00
|
|
|
|
|
|
|
// TestCrashNSEC tests generated using fuzz.go and with a message pack
|
|
|
|
// containing the following bytes:
|
|
|
|
// "0000\x00\x00000000\x00\x00/00000" +
|
|
|
|
// "0\x00\v\x00#\b00000\x00\x00\x00\x00\x00\x1a000" +
|
|
|
|
// "000\x00\x00\x00\x00\x1a000000\x00\x00\x00\x00\x1a0" +
|
|
|
|
// "00000\x00\v00\a0000000\x00"
|
|
|
|
// That byte sequence, when Unpack() and subsequential Pack() created a
|
|
|
|
// panic: runtime error: slice bounds out of range
|
|
|
|
// which was attributed to the fact that NSEC RR length computation was different (and smaller)
|
|
|
|
// then when within packDataNsec.
|
|
|
|
func TestCrashNSEC(t *testing.T) {
|
|
|
|
compression := make(map[string]struct{})
|
|
|
|
nsec := &NSEC{
|
2019-05-20 16:44:53 +10:00
|
|
|
Hdr: RR_Header{
|
|
|
|
Name: ".",
|
|
|
|
Rrtype: 0x2f,
|
|
|
|
Class: 0x3030,
|
|
|
|
Ttl: 0x30303030,
|
|
|
|
Rdlength: 0xb,
|
|
|
|
},
|
|
|
|
NextDomain: ".",
|
|
|
|
TypeBitMap: []uint16{
|
|
|
|
0x2302, 0x2303, 0x230a, 0x230b,
|
|
|
|
0x2312, 0x2313, 0x231a, 0x231b,
|
|
|
|
0x2322, 0x2323,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectedLength := 19
|
|
|
|
l := nsec.len(0, compression)
|
|
|
|
if l != expectedLength {
|
|
|
|
t.Fatalf("expected length of %d, got %d", expectedLength, l)
|
|
|
|
}
|
[nsec] compute NSEC.len() the same way that we would do in packDataNsec (#967)
The byte sequence, when Unpack()-ed and subsequential Pack()-ed created a
panic: runtime error: slice bounds out of range
github.com/miekg/dns.(*Msg).packBufferWithCompressionMap(0xc0000d4000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x100, 0x14, 0x14e80b, 0xbf2d4654d501a3c8, ...)
/Users/chantra/go/src/github.com/miekg/dns/msg.go:868 +0x13a8
Confirmed that Unpacking/Repacking payload described in TestCrashNSEC
did not raise a slice bound out of range panic, added unittests which
failed prior to this change.
```
go test -run TestCrashNSEC
--- FAIL: TestCrashNSEC (0.00s)
types_test.go:135: expected length of 19, got 12
FAIL
exit status 1
FAIL github.com/miekg/dns 0.067s
```
2019-05-19 17:40:22 +10:00
|
|
|
}
|