Skip to content

Commit

Permalink
sync: go 1.21.4 (refraction-networking#261)
Browse files Browse the repository at this point in the history
[release-branch.go1.21] 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.

For #62266
Fixes #62290

Change-Id: I210b70634e50beb456ab3977eb11272b8724c241
Reviewed-on: https://go-review.googlesource.com/c/go/+/522595
Run-TryBot: Damien Neil <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Marten Seemann <[email protected]>
Reviewed-by: Roland Shoemaker <[email protected]>
(cherry picked from commit e92c0f8)
Reviewed-on: https://go-review.googlesource.com/c/go/+/523039
Auto-Submit: Dmitri Shuralyov <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>

Co-authored-by: Damien Neil <[email protected]>
Co-authored-by: GopherBot <[email protected]>
  • Loading branch information
3 people authored Nov 26, 2023
1 parent b80788b commit 725fc9c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions u_quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tls
import (
"context"
"errors"
"fmt"
)

// A UQUICConn represents a connection which uses a QUIC implementation as the underlying
Expand Down Expand Up @@ -108,16 +109,22 @@ func (q *UQUICConn) 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 {
Expand Down

0 comments on commit 725fc9c

Please sign in to comment.