From f769df16e8b6ef2236183f8eab1b9ca22a94408c Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 18 Aug 2015 08:38:06 +0200 Subject: [PATCH] Reject unreasonably large messages We allocate a []byte to read the message into, so if the header says the messages is several gigabytes large we may run into trouble. In reality, a message should never be that large so we impose a limit. --- protocol.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/protocol.go b/protocol.go index 8b41c013..420b2085 100644 --- a/protocol.go +++ b/protocol.go @@ -15,7 +15,11 @@ import ( ) const ( - BlockSize = 128 * 1024 + // Data block size (128 KiB) + BlockSize = 128 << 10 + + // We reject messages larger than this when encountered on the wire. (64 MiB) + MaxMessageLen = 64 << 20 ) const ( @@ -383,6 +387,11 @@ func (c *rawConnection) readMessage() (hdr header, msg encodable, err error) { l.Debugf("read header %v (msglen=%d)", hdr, msglen) } + if msglen > MaxMessageLen { + err = fmt.Errorf("message length %d exceeds maximum %d", msglen, MaxMessageLen) + return + } + if hdr.version != 0 { err = fmt.Errorf("unknown protocol version 0x%x", hdr.version) return