mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Short initial handshake packet panics in readHandshakePacket instead of returning an error

Open aibeshkov opened this issue 3 weeks ago • 0 comments

Version: v1.10.1 and master (e6232456, 2026-11-10).

I know the library assumes a trusted server (see the discussion in #1751), so I am filing this as a robustness issue, not a security report. A malformed packet from the server should come back as an error such as ErrMalformPkt, not as a panic in the caller's goroutine.

readHandshakePacket slices the initial handshake packet without checking its length. On master:

  • https://github.com/go-sql-driver/mysql/blob/e6232456bb6eb92a117563823ac6956937410b96/packets.go#L210 authData := data[pos : pos+8]
  • https://github.com/go-sql-driver/mysql/blob/e6232456bb6eb92a117563823ac6956937410b96/packets.go#L216 binary.LittleEndian.Uint16(data[pos : pos+2])
  • https://github.com/go-sql-driver/mysql/blob/e6232456bb6eb92a117563823ac6956937410b96/packets.go#L250 data[pos:pos+12] (the one from #1751)

A two-byte handshake (protocol version 10, then NUL) gives slice bounds out of range [:14] with capacity 2 from Connector.Connect. It was found by fuzzing the client against a fake server. The test below uses only the public API:

package zzrepro

import (
	"context"
	"net"
	"testing"
	"time"

	"github.com/go-sql-driver/mysql"
)

// serve accepts one connection, sends payload as a single MySQL packet
// (seq 0) and keeps the socket open briefly.
func serve(t *testing.T, packets ...[]byte) string {
	t.Helper()
	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { ln.Close() })
	go func() {
		c, err := ln.Accept()
		if err != nil {
			return
		}
		defer c.Close()
		c.SetDeadline(time.Now().Add(3 * time.Second))
		for i, p := range packets {
			h := []byte{byte(len(p)), byte(len(p) >> 8), byte(len(p) >> 16), byte(i * 2)}
			c.Write(append(h, p...))
			if i+1 < len(packets) {
				buf := make([]byte, 4096)
				c.Read(buf) // client's handshake response
			}
		}
		time.Sleep(500 * time.Millisecond)
	}()
	return ln.Addr().String()
}

func connect(addr string) (panicVal any) {
	defer func() { panicVal = recover() }()
	cfg := mysql.NewConfig()
	cfg.Net, cfg.Addr, cfg.User, cfg.Passwd = "tcp", addr, "u", "p"
	cfg.Timeout = 2 * time.Second
	conn, err := mysql.NewConnector(cfg)
	if err != nil {
		return nil
	}
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()
	c, err := conn.Connect(ctx)
	if err == nil {
		c.Close()
	}
	return nil
}

// Finding 1: initial handshake of 2 bytes (protocol 10 + NUL) -> slice bounds out of range.
func TestShortInitialHandshakePanics(t *testing.T) {
	addr := serve(t, []byte{0x0a, 0x00})
	if p := connect(addr); p != nil {
		t.Fatalf("PANIC (expected an error return): %v", p)
	}
}

Suggested fix: check len(data) >= pos+n before each of these slices and return ErrMalformPkt. I can send a PR with a test if that is welcome.

Side note: a native-password AuthSwitchRequest with fewer than 20 bytes of challenge panicked at authData[:20] in v1.10.1; I see that master no longer has that code path, so nothing to do there.

aibeshkov avatar Nov 11 '26 10:11 aibeshkov