Skip to content

Commit

Permalink
Fix panic in MSSQL when Login7 package is invalid (#10452)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakule committed Mar 1, 2022
1 parent 726ed4a commit f590f50
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
54 changes: 54 additions & 0 deletions lib/srv/db/sqlserver/protocol/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build go1.18

/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package protocol

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"
)

func FuzzMSSQLLogin(f *testing.F) {
testcases := [][]byte{
{},
[]byte("\x100\x00x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
[]byte("\x100\x00x00000000000000000000000000000000000000000000\xff\xff0\x800000000000000000000000000\x00 \x000000000000000000000000000000000000000000"),
[]byte("\x100\x00x00000000000000000000000000000000000000000000 \x00 \x800000000000000000000000000\x00\xff\xff0000000000000000000000000000000000000000"),
[]byte("\x100\x00x000000000000000000000000000000000000000000000\x00 \x8000000000000000000000000000000000000000000000000000000000000000000000"),
[]byte("\x100\x00x000000000000000000000000000000000000000000000\x00 \x800000000000000000000000000\x00\xff\xff0000000000000000000000000000000000000000"),
[]byte("\x100\x00x000000000000000000000000000000000000000000000\x00\xff\xff0000000000000000000000000\x00 \x000000000000000000000000000000000000000000"),
[]byte("\x100\x00x000000000000000000000000000000000000000000000\x00\x00\x800000000000000000000000000\x00\xff\xff0000000000000000000000000000000000000000"),
}

for _, tc := range testcases {
f.Add(tc)
}

f.Fuzz(func(t *testing.T, packet []byte) {
reader := bytes.NewReader(packet)

require.NotPanics(t, func() {
_, _ = ReadLogin7Packet(reader)
})
})
}
42 changes: 36 additions & 6 deletions lib/srv/db/sqlserver/protocol/login7.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,12 @@ func ReadLogin7Packet(r io.Reader) (*Login7Packet, error) {
return nil, trace.Wrap(err)
}

// Decode username and database from the packet. Offset/length are counted
// from from the beginning of entire packet data (excluding header).
username, err := mssql.ParseUCS2String(
pkt.Data[header.IbUserName : header.IbUserName+header.CchUserName*2])
username, err := readUsername(pkt, header)
if err != nil {
return nil, trace.Wrap(err)
}
database, err := mssql.ParseUCS2String(
pkt.Data[header.IbDatabase : header.IbDatabase+header.CchDatabase*2])

database, err := readDatabase(pkt, header)
if err != nil {
return nil, trace.Wrap(err)
}
Expand All @@ -143,3 +140,36 @@ func ReadLogin7Packet(r io.Reader) (*Login7Packet, error) {
database: database,
}, nil
}

// errInvalidPacket is returned when Login7 package contains invalid data.
var errInvalidPacket = trace.Errorf("invalid login7 packet")

// readUsername reads username from login7 package.
func readUsername(pkt *Packet, header Login7Header) (string, error) {
if len(pkt.Data) < int(header.IbUserName)+int(header.CchUserName)*2 {
return "", errInvalidPacket
}

// Decode username and database from the packet. Offset/length are counted
// from the beginning of entire packet data (excluding header).
username, err := mssql.ParseUCS2String(
pkt.Data[header.IbUserName : header.IbUserName+header.CchUserName*2])
if err != nil {
return "", trace.Wrap(err)
}
return username, nil
}

// readDatabase reads database name from login7 package.
func readDatabase(pkt *Packet, header Login7Header) (string, error) {
if len(pkt.Data) < int(header.IbDatabase)+int(header.CchDatabase)*2 {
return "", errInvalidPacket
}

database, err := mssql.ParseUCS2String(
pkt.Data[header.IbDatabase : header.IbDatabase+header.CchDatabase*2])
if err != nil {
return "", trace.Wrap(err)
}
return database, nil
}

0 comments on commit f590f50

Please sign in to comment.