Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fuzzing] fuzzing support for oss-fuzz #2707

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ This will produce a version number of
## Layout

The GoBGP project adopts [Standard Go Project Layout](https://github.com/golang-standards/project-layout).

## Fuzzing

Run [Go Fuzzing](https://go.dev/security/fuzz)

```bash
go test -fuzz=FuzzParseRTR $PWD/pkg/packet/rtr
go test -fuzz=FuzzParseBMPMessage $PWD/pkg/packet/bmp
go test -fuzz=FuzzParseBGPMessage $PWD/pkg/packet/bgp
go test -fuzz=FuzzParseLargeCommunity $PWD/pkg/packet/bgp
go test -fuzz=FuzzParseFlowSpecComponents $PWD/pkg/packet/bgp
go test -fuzz=FuzzMRT $PWD/pkg/packet/mrt
go test -fuzz=FuzzZapi $PWD/internal/pkg/zebra
```
25 changes: 25 additions & 0 deletions internal/pkg/zebra/zapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,3 +1120,28 @@ func Test_vrfLabelBody(t *testing.T) {
assert.Equal(bufIn, bufOut)
}
}

func FuzzZapi(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {

if len(data) < 16 {
return
}

for v := MinZapiVer; v <= MaxZapiVer; v++ {

ZAPIHeaderSize := int(HeaderSize(v))

hd := &Header{}
err := hd.decodeFromBytes(data[:ZAPIHeaderSize])

if err != nil {
return
}

software := NewSoftware(v, "")
parseMessage(hd, data[:ZAPIHeaderSize], software)
}
})
}
14 changes: 14 additions & 0 deletions pkg/packet/bgp/bgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3715,3 +3715,17 @@ func Test_BGPOpenDecodeCapabilities(t *testing.T) {
assert.Equal(t, tuples[0].RouteFamily, RF_IPv4_UC)
assert.Equal(t, tuples[0].Mode, BGP_ADD_PATH_SEND)
}

func FuzzParseBGPMessage(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {
ParseBGPMessage(data)
})
}

func FuzzParseFlowSpecComponents(f *testing.F) {

f.Fuzz(func(t *testing.T, data string) {
ParseFlowSpecComponents(RF_FS_IPv4_UC, data)
})
}
7 changes: 7 additions & 0 deletions pkg/packet/bgp/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,10 @@ func TestValidateLargeCommunities(t *testing.T) {
assert.Nil(err)
assert.True(len(a.Values) == 2)
}

func FuzzParseLargeCommunity(f *testing.F) {

f.Fuzz(func(t *testing.T, data string) {
ParseLargeCommunity(data)
})
}
7 changes: 7 additions & 0 deletions pkg/packet/bmp/bmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ func Test_RouteMonitoringUnknownType(t *testing.T) {
_, err := ParseBMPMessage(data)
require.NoError(t, err)
}

func FuzzParseBMPMessage(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {
ParseBMPMessage(data)
})
}
18 changes: 18 additions & 0 deletions pkg/packet/mrt/mrt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,21 @@ func TestMrtSplit(t *testing.T) {
t.Logf("scanner scanned %d serialized keepalives from the buffer", numread)
assert.Equal(t, numwrite, numread)
}

func FuzzMRT(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {
if len(data) < 16 {
return
}

hdr := &MRTHeader{}
err := hdr.DecodeFromBytes(data[:MRT_COMMON_HEADER_LEN])

if err != nil {
return
}

ParseMRTBody(hdr, data[MRT_COMMON_HEADER_LEN:])
})
}
7 changes: 7 additions & 0 deletions pkg/packet/rtr/rtr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,10 @@ func Test_RTRErrorReport(t *testing.T) {
// when it has both "erroneous PDU" and "Arbitrary Text"
verifyRTRMessage(t, NewRTRErrorReport(CORRUPT_DATA, errPDU, errText2))
}

func FuzzParseRTR(f *testing.F) {

f.Fuzz(func(t *testing.T, data []byte) {
ParseRTR(data)
})
}
Loading