Skip to content

Commit

Permalink
p2p/discover/v5wire: reject packets smaller than 63 bytes (ethereum#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
dbadoy authored and blakehhuynh committed Oct 3, 2022
1 parent 2ca31a1 commit e0735c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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 @@ -274,7 +274,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

0 comments on commit e0735c9

Please sign in to comment.