Skip to content

Commit

Permalink
ndp: fix fuzz crashers and add fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlayher committed Jan 10, 2018
1 parent f69c669 commit 4356b10
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
6 changes: 3 additions & 3 deletions fuzz.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// +build gofuzz

package ndp

func Fuzz(data []byte) int {
// fuzz is a shared function for go-fuzz and tests that verify go-fuzz bugs
// are fixed.
func fuzz(data []byte) int {
m, err := ParseMessage(data)
if err != nil {
return 0
Expand Down
26 changes: 26 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ndp

import "testing"

func Test_fuzz(t *testing.T) {
tests := []struct {
name string
s string
}{
{
name: "parse option length",
s: "\x86000000000000000\x01\xc0",
},
{
name: "prefix information length",
s: "\x86000000000000000\x03\x0100" +
"0000",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = fuzz([]byte(tt.s))
})
}
}
7 changes: 7 additions & 0 deletions gofuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//+build gofuzz

package ndp

func Fuzz(data []byte) int {
return fuzz(data)
}
14 changes: 10 additions & 4 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func (pi *PrefixInformation) UnmarshalBinary(b []byte) error {
return err
}

// Guard against incorrect option length.
if raw.Length != piOptLen {
return io.ErrUnexpectedEOF
}

var (
oFlag = (raw.Value[1] & 0x80) != 0
aFlag = (raw.Value[1] & 0x40) != 0
Expand Down Expand Up @@ -333,16 +338,17 @@ func parseOptions(b []byte) ([]Option, error) {
o = new(RawOption)
}

// Verify that we won't advance beyond the end of the byte slice.
if l > len(b[i:]) {
return nil, io.ErrUnexpectedEOF
}

// Unmarshal at the current offset, up to the expected length.
if err := o.UnmarshalBinary(b[i : i+l]); err != nil {
return nil, err
}

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

options = append(options, o)
Expand Down

0 comments on commit 4356b10

Please sign in to comment.