From f2a3b26319df45b68a6754cd1e394fa4287ad6f2 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Mon, 16 Oct 2023 21:16:36 -0400 Subject: [PATCH] crypto/tls: QUIC: fix panics when processing post-handshake messages The check for fragmentary post-handshake messages in QUICConn.HandleData was reversed, resulting in a potential panic when HandleData receives a partial message. In addition, HandleData wasn't checking the size of buffered post-handshake messages. Produce an error when a post-handshake message is larger than maxHandshake. TestQUICConnectionState was using an onHandleCryptoData hook in runTestQUICConnection that was never being called. (I think it was inadvertently removed at some point while the CL was in review.) Fix this test while making the hook more general. Fixes #62266 Change-Id: I210b70634e50beb456ab3977eb11272b8724c241 Reviewed-on: https://go-review.googlesource.com/c/go/+/522595 Run-TryBot: Damien Neil TryBot-Result: Gopher Robot Reviewed-by: Marten Seemann Reviewed-by: Roland Shoemaker --- quic.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/quic.go b/quic.go index 4f24fd2..baaad01 100644 --- a/quic.go +++ b/quic.go @@ -229,16 +229,22 @@ func (q *QUICConn) HandleData(level QUICEncryptionLevel, data []byte) error { return nil } // The handshake goroutine has exited. + c.handshakeMutex.Lock() + defer c.handshakeMutex.Unlock() c.hand.Write(c.quic.readbuf) c.quic.readbuf = nil for q.conn.hand.Len() >= 4 && q.conn.handshakeErr == nil { b := q.conn.hand.Bytes() n := int(b[1])<<16 | int(b[2])<<8 | int(b[3]) - if 4+n < len(b) { + if n > maxHandshake { + q.conn.handshakeErr = fmt.Errorf("tls: handshake message of length %d bytes exceeds maximum of %d bytes", n, maxHandshake) + break + } + if len(b) < 4+n { return nil } if err := q.conn.handlePostHandshakeMessage(); err != nil { - return quicError(err) + q.conn.handshakeErr = err } } if q.conn.handshakeErr != nil {