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

wtwire: simply fuzz targets #9271

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
115 changes: 23 additions & 92 deletions watchtower/wtwire/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@ func prefixWithMsgType(data []byte, prefix MessageType) []byte {
return data
}

// harness performs the actual fuzz testing of the appropriate wire message.
// This function will check that the passed-in message passes wire length
// checks, is a valid message once deserialized, and passes a sequence of
// serialization and deserialization checks. Returns an int that determines
// whether the input is unique or not.
func harness(t *testing.T, data []byte, emptyMsg Message) {
// wireMsgHarness performs the actual fuzz testing of the appropriate wire
// message. This function will check that the passed-in message passes wire
// length checks, is a valid message once deserialized, and passes a sequence of
// serialization and deserialization checks. emptyMsg must be an empty Message
// of the type to be fuzzed, as it is used to determine the appropriate prefix
// bytes and max payload length for decoding.
func wireMsgHarness(t *testing.T, data []byte, emptyMsg Message) {
t.Helper()

// Create a reader with the byte array.
r := bytes.NewReader(data)

// Make sure byte array length (excluding 2 bytes for message type) is
// less than max payload size for the wire message.
payloadLen := uint32(len(data)) - 2
// Make sure byte array length is less than max payload size for the
// wire message.
payloadLen := uint32(len(data))
if payloadLen > emptyMsg.MaxPayloadLength(0) {
// Ignore this input - max payload constraint violated.
return
}

data = prefixWithMsgType(data, emptyMsg.MsgType())

// Create a reader with the byte array.
r := bytes.NewReader(data)

msg, err := ReadMessage(r, 0)
if err != nil {
return
Expand All @@ -57,120 +60,48 @@ func harness(t *testing.T, data []byte, emptyMsg Message) {

func FuzzCreateSessionReply(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgCreateSessionReply.
data = prefixWithMsgType(data, MsgCreateSessionReply)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := CreateSessionReply{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &CreateSessionReply{})
})
}

func FuzzCreateSession(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgCreateSession.
data = prefixWithMsgType(data, MsgCreateSession)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := CreateSession{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &CreateSession{})
})
}

func FuzzDeleteSessionReply(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgDeleteSessionReply.
data = prefixWithMsgType(data, MsgDeleteSessionReply)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := DeleteSessionReply{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &DeleteSessionReply{})
})
}

func FuzzDeleteSession(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgDeleteSession.
data = prefixWithMsgType(data, MsgDeleteSession)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := DeleteSession{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &DeleteSession{})
})
}

func FuzzError(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgError.
data = prefixWithMsgType(data, MsgError)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := Error{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &Error{})
})
}

func FuzzInit(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgInit.
data = prefixWithMsgType(data, MsgInit)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := Init{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &Init{})
})
}

func FuzzStateUpdateReply(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgStateUpdateReply.
data = prefixWithMsgType(data, MsgStateUpdateReply)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := StateUpdateReply{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &StateUpdateReply{})
})
}

func FuzzStateUpdate(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
// Prefix with MsgStateUpdate.
data = prefixWithMsgType(data, MsgStateUpdate)

// Create an empty message so that the FuzzHarness func can
// check if the max payload constraint is violated.
emptyMsg := StateUpdate{}

// Pass the message into our general fuzz harness for wire
// messages!
harness(t, data, &emptyMsg)
wireMsgHarness(t, data, &StateUpdate{})
})
}
Loading