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

p2p/discover/v5wire: reject packets if smaller than 63 bytes #25740

Merged
merged 2 commits into from
Sep 12, 2022
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
8 changes: 6 additions & 2 deletions p2p/discover/v5wire/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ const (
minVersion = 1
sizeofMaskingIV = 16

// The minimum size of any Discovery v5 packet is 63 bytes.
// Should reject packets smaller than minPacketSize.
minPacketSize = 63

minMessageSize = 48 // this refers to data after static headers
randomPacketMsgSize = 20
)
Expand Down Expand Up @@ -415,10 +419,10 @@ func (c *Codec) encryptMessage(s *session, p Packet, head *Header, headerData []

// Decode decodes a discovery packet.
func (c *Codec) Decode(input []byte, addr string) (src enode.ID, n *enode.Node, p Packet, err error) {
// Unmask the static header.
if len(input) < sizeofStaticPacketData {
if len(input) < minPacketSize {
return enode.ID{}, nil, nil, errTooShort
}
// Unmask the static header.
var head Header
copy(head.IV[:], input[:sizeofMaskingIV])
mask := head.mask(c.localnode.ID())
Expand Down
10 changes: 9 additions & 1 deletion p2p/discover/v5wire/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,15 @@ func TestDecodeErrorsV5(t *testing.T) {
net := newHandshakeTest()
defer net.close()

net.nodeA.expectDecodeErr(t, errTooShort, []byte{})
b := make([]byte, 0)
net.nodeA.expectDecodeErr(t, errTooShort, b)

b = make([]byte, 62)
net.nodeA.expectDecodeErr(t, errTooShort, b)

b = make([]byte, 63)
net.nodeA.expectDecodeErr(t, errInvalidHeader, b)

// TODO some more tests would be nice :)
// - check invalid authdata sizes
// - check invalid handshake data sizes
Expand Down