Skip to content

Commit 4356b10

Browse files
committed
ndp: fix fuzz crashers and add fuzz test
1 parent f69c669 commit 4356b10

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

fuzz.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// +build gofuzz
2-
31
package ndp
42

5-
func Fuzz(data []byte) int {
3+
// fuzz is a shared function for go-fuzz and tests that verify go-fuzz bugs
4+
// are fixed.
5+
func fuzz(data []byte) int {
66
m, err := ParseMessage(data)
77
if err != nil {
88
return 0

fuzz_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ndp
2+
3+
import "testing"
4+
5+
func Test_fuzz(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
s string
9+
}{
10+
{
11+
name: "parse option length",
12+
s: "\x86000000000000000\x01\xc0",
13+
},
14+
{
15+
name: "prefix information length",
16+
s: "\x86000000000000000\x03\x0100" +
17+
"0000",
18+
},
19+
}
20+
21+
for _, tt := range tests {
22+
t.Run(tt.name, func(t *testing.T) {
23+
_ = fuzz([]byte(tt.s))
24+
})
25+
}
26+
}

gofuzz.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//+build gofuzz
2+
3+
package ndp
4+
5+
func Fuzz(data []byte) int {
6+
return fuzz(data)
7+
}

option.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ func (pi *PrefixInformation) UnmarshalBinary(b []byte) error {
207207
return err
208208
}
209209

210+
// Guard against incorrect option length.
211+
if raw.Length != piOptLen {
212+
return io.ErrUnexpectedEOF
213+
}
214+
210215
var (
211216
oFlag = (raw.Value[1] & 0x80) != 0
212217
aFlag = (raw.Value[1] & 0x40) != 0
@@ -333,16 +338,17 @@ func parseOptions(b []byte) ([]Option, error) {
333338
o = new(RawOption)
334339
}
335340

341+
// Verify that we won't advance beyond the end of the byte slice.
342+
if l > len(b[i:]) {
343+
return nil, io.ErrUnexpectedEOF
344+
}
345+
336346
// Unmarshal at the current offset, up to the expected length.
337347
if err := o.UnmarshalBinary(b[i : i+l]); err != nil {
338348
return nil, err
339349
}
340350

341-
// Verify that we won't advance beyond the end of the byte slice, and
342351
// Advance to the next option's type field.
343-
if l > len(b[i:]) {
344-
return nil, io.ErrUnexpectedEOF
345-
}
346352
i += l
347353

348354
options = append(options, o)

0 commit comments

Comments
 (0)