From d2077e2392548e0fdf2432013e6b00426fbad5f7 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Wed, 19 Jul 2023 17:18:25 +0530 Subject: [PATCH] [Fuzzing] remove deprecated go-fuzz and use Native Go fuzz Signed-off-by: Arjun Singh --- dhcpv4/dhcpv4_test.go | 44 ++++++++++++++++++++++++++++++++++++++ dhcpv4/fuzz.go | 41 ----------------------------------- dhcpv6/dhcpv6_test.go | 36 +++++++++++++++++++++++++++++++ dhcpv6/fuzz.go | 33 ---------------------------- rfc1035label/label_test.go | 33 ++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 74 deletions(-) delete mode 100644 dhcpv4/fuzz.go delete mode 100644 dhcpv6/fuzz.go diff --git a/dhcpv4/dhcpv4_test.go b/dhcpv4/dhcpv4_test.go index a961f780..95523aba 100644 --- a/dhcpv4/dhcpv4_test.go +++ b/dhcpv4/dhcpv4_test.go @@ -412,3 +412,47 @@ func Test_withIP(t *testing.T) { b := buff.Buffer require.Equal(t, b.Len(), 4, "Testing no of bytes written by writeIP func") } + +func FuzzDHCPv4(f *testing.F) { + + data_0 := []byte{ + 1, // dhcp request + 1, // ethernet hw type + 6, // hw addr length + 3, // hop count + 0xaa, 0xbb, 0xcc, 0xdd, // transaction ID, big endian (network) + 0, 3, // number of seconds + 0, 1, // broadcast + 0, 0, 0, 0, // client IP address + 0, 0, 0, 0, // your IP address + 0, 0, 0, 0, // server IP address + 0, 0, 0, 0, // gateway IP address + 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding + } + + data_1 := []byte{ + 1, // dhcp request + 1, // ethernet hw type + 6, // hw addr length + 0, // hop count + 0xaa, 0xbb, 0xcc, 0xdd, // transaction ID + 3, 0, // number of seconds + 1, 0, // broadcast + 0, 0, 0, 0, // client IP address + 0, 0, 0, 0, // your IP address + 0, 0, 0, 0, // server IP address + 0, 0, 0, 0, // gateway IP address + 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // client MAC address + padding + } + + f.Add(data_0) + f.Add(data_1) + + f.Fuzz(func(t *testing.T, data []byte) { + msg, err := FromBytes(data) + if err != nil { + return + } + msg.ToBytes() + }) +} diff --git a/dhcpv4/fuzz.go b/dhcpv4/fuzz.go deleted file mode 100644 index cf62ba58..00000000 --- a/dhcpv4/fuzz.go +++ /dev/null @@ -1,41 +0,0 @@ -// +build gofuzz - -package dhcpv4 - -import ( - "fmt" - "reflect" -) - -// Fuzz is the entrypoint for go-fuzz -func Fuzz(data []byte) int { - msg, err := FromBytes(data) - if err != nil { - return 0 - } - - serialized := msg.ToBytes() - - // Compared to dhcpv6, dhcpv4 has padding and fixed-size fields containing - // variable-length data; We can't expect the library to output byte-for-byte - // identical packets after a round-trip. - // Instead, we check that after a round-trip, the packet reserializes to the - // same internal representation - rtMsg, err := FromBytes(serialized) - - if err != nil || !reflect.DeepEqual(msg, rtMsg) { - fmt.Printf("Input: %x\n", data) - fmt.Printf("Round-trip: %x\n", serialized) - fmt.Println("Message: ", msg.Summary()) - fmt.Printf("Go repr: %#v\n", msg) - fmt.Println("Reserialized: ", rtMsg.Summary()) - fmt.Printf("Go repr: %#v\n", rtMsg) - if err != nil { - fmt.Printf("Got error while reserializing: %v\n", err) - panic("round-trip error: " + err.Error()) - } - panic("round-trip different: " + msg.Summary()) - } - - return 1 -} diff --git a/dhcpv6/dhcpv6_test.go b/dhcpv6/dhcpv6_test.go index 210f19d4..9ea61fa0 100644 --- a/dhcpv6/dhcpv6_test.go +++ b/dhcpv6/dhcpv6_test.go @@ -273,3 +273,39 @@ func TestGetTransactionIDRelay(t *testing.T) { // TODO test NewMessageTypeSolicit // test String and Summary + +func FuzzDHCPv6(f *testing.F) { + + var relayForwBytesDuidUUID_data = []byte{ + 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x26, 0x8a, 0x07, 0xff, 0xfe, 0x56, + 0xdc, 0xa4, 0x00, 0x12, 0x00, 0x06, 0x24, 0x8a, + 0x07, 0x56, 0xdc, 0xa4, 0x00, 0x09, 0x00, 0x5a, + 0x06, 0x7d, 0x9b, 0xca, 0x00, 0x01, 0x00, 0x12, + 0x00, 0x04, 0xb7, 0xfd, 0x0a, 0x8c, 0x1b, 0x14, + 0x10, 0xaa, 0xeb, 0x0a, 0x5b, 0x3f, 0xe8, 0x9d, + 0x0f, 0x56, 0x00, 0x06, 0x00, 0x0a, 0x00, 0x17, + 0x00, 0x18, 0x00, 0x17, 0x00, 0x18, 0x00, 0x01, + 0x00, 0x08, 0x00, 0x02, 0xff, 0xff, 0x00, 0x03, + 0x00, 0x28, 0x07, 0x56, 0xdc, 0xa4, 0x00, 0x00, + 0x0e, 0x10, 0x00, 0x00, 0x15, 0x18, 0x00, 0x05, + 0x00, 0x18, 0x26, 0x20, 0x01, 0x0d, 0xc0, 0x82, + 0x90, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xaf, 0xa0, 0x00, 0x00, 0x1c, 0x20, 0x00, 0x00, + 0x1d, 0x4c} + + f.Add(relayForwBytesDuidUUID_data) + f.Add([]byte{01, 0xab, 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00}) + f.Add([]byte{01, 0xa, 0xb, 0xc, 0x00, 0x00, 0x00, 0x00}) + f.Add([]byte("0000\x00\x01\x00\x0e\x00\x01000000000000")) + + f.Fuzz(func(t *testing.T, data []byte) { + msg, err := FromBytes(data) + if err != nil { + return + } + msg.ToBytes() + }) +} diff --git a/dhcpv6/fuzz.go b/dhcpv6/fuzz.go deleted file mode 100644 index 3f5afef9..00000000 --- a/dhcpv6/fuzz.go +++ /dev/null @@ -1,33 +0,0 @@ -// +build gofuzz - -package dhcpv6 - -import ( - "bytes" - "fmt" -) - -// Fuzz is an entrypoint for go-fuzz (github.com/dvyukov/go-fuzz) -func Fuzz(data []byte) int { - msg, err := FromBytes(data) - if err != nil { - return 0 - } - - serialized := msg.ToBytes() - if !bytes.Equal(data, serialized) { - rtMsg, err := FromBytes(serialized) - fmt.Printf("Input: %x\n", data) - fmt.Printf("Round-trip: %x\n", serialized) - fmt.Println("Message: ", msg.Summary()) - fmt.Printf("Go repr: %#v\n", msg) - fmt.Println("round-trip reserialized: ", rtMsg.Summary()) - fmt.Printf("Go repr: %#v\n", rtMsg) - if err != nil { - fmt.Printf("failed to parse after deserialize-serialize: %v\n", err) - } - panic("round-trip different") - } - - return 1 -} diff --git a/rfc1035label/label_test.go b/rfc1035label/label_test.go index 865b41ac..f2cea9a5 100644 --- a/rfc1035label/label_test.go +++ b/rfc1035label/label_test.go @@ -139,3 +139,36 @@ func TestNestedCompressedLabel(t *testing.T) { _, err := FromBytes(data) require.Error(t, err) } + +func FuzzLabel(f *testing.F) { + + f.Add([]byte{0x5, 0xaa, 0xbb}) + f.Add([]byte{0x3, 0xaa, 0xbb}) + + data_0 := []byte{ + // slackware.it + 9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e', + 2, 'i', 't', + 0, + // insomniac.slackware.it + 9, 'i', 'n', 's', 'o', 'm', 'n', 'i', 'a', 'c', + 192, 0, + // mail.systemboot.org + 4, 'm', 'a', 'i', 'l', + 10, 's', 'y', 's', 't', 'e', 'm', 'b', 'o', 'o', 't', + 3, 'o', 'r', 'g', + 0, + // systemboot.org + 192, 31, + } + + f.Add(data_0) + + f.Fuzz(func(t *testing.T, data []byte) { + labels, err := FromBytes(data) + if err != nil { + return + } + labels.ToBytes() + }) +}