Skip to content

Commit 1069c21

Browse files
committed
fix and bound buffer length, add documentation from PR 11843 by maxl99
1 parent 6d8a420 commit 1069c21

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pkg/tcpproxy/tcp.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,21 @@ func (p *TCPProxy) Get(host string) *TCPServer {
5959
// and open a connection to the passthrough server.
6060
func (p *TCPProxy) Handle(conn net.Conn) {
6161
defer conn.Close()
62-
// See: https://www.ibm.com/docs/en/ztpf/1.1.0.15?topic=sessions-ssl-record-format
63-
data := make([]byte, 16384)
62+
// [Documentation by @maxl99](https://github.com/kubernetes/ingress-nginx/pull/11843/files#diff-aef3e187fd37c68706ad582d7b89a2d9ad11691bd929a2158b86f93362244105R67-R79)
63+
// It appears that the ClientHello must fit into *one* TLSPlaintext message:
64+
// When a client first connects to a server, it is REQUIRED to send the ClientHello as its first TLS message.
65+
// Source: https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.2
66+
//
67+
// length: The length (in bytes) of the following TLSPlaintext.fragment. The length MUST NOT exceed 2^14 bytes.
68+
// An endpoint that receives a record that exceeds this length MUST terminate the connection with a "record_overflow" alert.
69+
// Source: https://datatracker.ietf.org/doc/html/rfc8446#section-5.1
70+
// bytes 0 : content type
71+
// bytes 1-2: legacy version
72+
// bytes 3-4: length
73+
// bytes 5+ : message
74+
// https://en.wikipedia.org/wiki/Transport_Layer_Security#TLS_record
75+
// Thus, we need to allocate 5 + 16384 bytes
76+
data := make([]byte, parser.TLSHeaderLength+16384)
6477

6578
// read the tls header first
6679
_, err := io.ReadFull(conn, data[:parser.TLSHeaderLength])
@@ -69,7 +82,7 @@ func (p *TCPProxy) Handle(conn net.Conn) {
6982
return
7083
}
7184
// get the total data length then read the rest
72-
length := int(data[3])<<8 + int(data[4]) + parser.TLSHeaderLength
85+
length := min(int(data[3])<<8+int(data[4])+parser.TLSHeaderLength, len(data))
7386
_, err = io.ReadFull(conn, data[parser.TLSHeaderLength:length])
7487
if err != nil {
7588
klog.V(4).ErrorS(err, "Error reading data from the connection")

0 commit comments

Comments
 (0)