Skip to content

Proper error handling in handleAuthResult #1191

Closed
@MrBTTF

Description

@MrBTTF

Issue description

If block is nil, in line 375 ParsePKIXPublicKey will panic. It happened to me when I was trying to connect GCP sql database and it responded with Access denied for user 'user'@'cloudsqlproxy~xx.xx.xx.x' (using password: YES)

mysql/auth.go

Lines 374 to 375 in fe2230a

block, _ := pem.Decode(data[1:])
pkix, err := x509.ParsePKIXPublicKey(block.Bytes)

Error log

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x6a44f2]

goroutine 1 [running]:
github.com/go-sql-driver/mysql.(*mysqlConn).handleAuthResult(0xc0000a79e0, 0xc0004c8be8, 0x14, 0x14, 0xc0004c8bd0, 0x15, 0x0, 0x0)
	/root/go/pkg/mod/github.com/go-sql-driver/[email protected]/auth.go:380 +0x732
github.com/go-sql-driver/mysql.(*connector).Connect(0xc0000103e8, 0xb69bf0, 0xc0000240d8, 0x0, 0x0, 0x0, 0x0)
	/root/go/pkg/mod/github.com/go-sql-driver/[email protected]/connector.go:109 +0x596
database/sql.(*DB).conn(0xc000110ea0, 0xb69bf0, 0xc0000240d8, 0xc00025fb01, 0x4e34d7, 0xc0000811e0, 0xc00008e500)
	/usr/local/go/src/database/sql/sql.go:1301 +0x184
database/sql.(*DB).PingContext(0xc000110ea0, 0xb69bf0, 0xc0000240d8, 0x1, 0x1)
	/usr/local/go/src/database/sql/sql.go:799 +0x90
database/sql.(*DB).Ping(...)
	/usr/local/go/src/database/sql/sql.go:817
github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql.DialCfg(0xc00025fd68, 0xc00025fd68, 0x52632e, 0xc0002acbd0)
	/root/go/pkg/mod/github.com/!google!cloud!platform/[email protected]/proxy/dialers/mysql/hook.go:93 +0x1c5
github.com/qreuz/common-go/pkg/utils.InitDB(0xc0000f78c0, 0xaaf751, 0x9, 0xc0000d8600, 0x0, 0xc00025feb0, 0x52a3a5)
...

Suggestion

I suggest to handle it in this way

block, rest := pem.Decode(data[1:])
if block == nil {
	return fmt.Errorf("No Pem data found, data: %s", rest)
}

Configuration

Driver version (or git SHA): v1.5.0

Go version: go version go1.16 linux/amd64

Server version: MySQL 8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions