Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packets: discard connection on zero sequence ID #1471

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Maciej Zimnoch <maciej.zimnoch at codilime.com>
Michael Woolnough <michael.woolnough at gmail.com>
Nathanial Murphy <nathanial.murphy at gmail.com>
Nicola Peduzzi <thenikso at gmail.com>
Oliver Bone <owbone at github.com>
Olivier Mengué <dolmen at cpan.org>
oscarzhao <oscarzhaosl at gmail.com>
Paul Bonser <misterpib at gmail.com>
Expand Down
14 changes: 12 additions & 2 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,20 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16)

// check packet sync [8 bit]
if data[3] != mc.sequence {
if data[3] > mc.sequence {
if sequenceID := data[3]; sequenceID != mc.sequence {
if sequenceID > mc.sequence {
return nil, ErrPktSyncMul
}

if sequenceID == 0 {
// If the sequence ID is zero then this is not a response to any
// packet that the client has sent. It's likely an error packet
// that the server sent simultaneously.
errLog.Print("received packet with zero sequence ID")
mc.Close()
return nil, ErrInvalidConn
}

return nil, ErrPktSync
}
mc.sequence++
Expand Down
25 changes: 23 additions & 2 deletions packets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ func TestReadPacketWrongSequenceID(t *testing.T) {
}

// too low sequence id
conn.data = []byte{0x01, 0x00, 0x00, 0x00, 0xff}
conn.data = []byte{0x01, 0x00, 0x00, 0x01, 0xff}
conn.maxReads = 1
mc.sequence = 1
mc.sequence = 2
_, err := mc.readPacket()
if err != ErrPktSync {
t.Errorf("expected ErrPktSync, got %v", err)
Expand All @@ -155,6 +155,27 @@ func TestReadPacketWrongSequenceID(t *testing.T) {
}
}

func TestReadPacketZeroSequenceID(t *testing.T) {
conn := new(mockConn)
mc := &mysqlConn{
buf: newBuffer(conn),
closech: make(chan struct{}),
}

conn.data = []byte{0x01, 0x00, 0x00, 0x00, 0xff}
conn.maxReads = 1
mc.sequence = 1
_, err := mc.readPacket()
if err != ErrInvalidConn {
t.Errorf("expected ErrInvalidConn, got %v", err)
}

// The connection should not be returned to the pool.
if mc.IsValid() {
t.Errorf("expected connection to no longer be valid")
}
}

func TestReadPacketSplit(t *testing.T) {
conn := new(mockConn)
mc := &mysqlConn{
Expand Down