Skip to content

Commit

Permalink
Fix panic in MSSQL when Login7 package is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
jakule committed Feb 24, 2022
1 parent ad8f0da commit c2258ca
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
50 changes: 50 additions & 0 deletions lib/srv/db/sqlserver/protocol/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//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"
)

func FuzzMSSQLLogin(f *testing.F) {
corpuses := [][]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 _, corpus := range corpuses {
f.Add(corpus)
}

f.Fuzz(func(t *testing.T, packet []byte) {
reader := bytes.NewReader(packet)
// no assertion, check for panic
ReadLogin7Packet(reader)
})
}
50 changes: 44 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,44 @@ func ReadLogin7Packet(r io.Reader) (*Login7Packet, error) {
database: database,
}, nil
}

// errInvalidPackage is returned when Login7 package contains invalid data.
var errInvalidPackage = 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 "", errInvalidPackage
}

if len(pkt.Data) <= int(header.IbUserName) {
return "", errInvalidPackage
}

// 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 "", errInvalidPackage
}

if len(pkt.Data) < int(header.IbDatabase) {
return "", errInvalidPackage
}

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 c2258ca

Please sign in to comment.