Skip to content

mssql: prevent uint16 overflow in PRELOGIN option bounds check - #758

Open
ChrisJr404 wants to merge 1 commit into
zmap:masterfrom
ChrisJr404:fix/mssql-prelogin-offset-overflow
Open

mssql: prevent uint16 overflow in PRELOGIN option bounds check#758
ChrisJr404 wants to merge 1 commit into
zmap:masterfrom
ChrisJr404:fix/mssql-prelogin-offset-overflow

Conversation

@ChrisJr404

Copy link
Copy Markdown

A malformed MSSQL PRELOGIN response can crash a scan because the offset+length bounds check in decodePreloginOptions is computed in uint16 arithmetic and can overflow, so this widens both values to int before the check.

offset and length are read straight from the server's PRELOGIN response as uint16. The guard if len(body) < int(offset+length) evaluates offset+length as a uint16, so a large offset wraps around instead of failing the check. For example offset=0xFFFF, length=0x0002 wraps to 1, the check passes, and the next line body[offset : offset+length] slices body[65535:1], panicking with slice bounds out of range [65535:1]. Since a scanned server fully controls these bytes, any host can trigger the panic. The fix converts offset and length to int at read time so the addition can't wrap, and the existing check then correctly rejects the packet with ErrInvalidData.

How to Test

go test ./modules/mssql/

Two new tests in modules/mssql/connection_test.go cover it:

  • TestDecodePreloginOptionsOverflow feeds the overflowing option above and asserts ErrInvalidData is returned (before the fix this test panics).
  • TestDecodePreloginOptionsValid confirms a well-formed PRELOGIN body still decodes to the right option value.

Notes & Caveats

Behavior is unchanged for valid packets; only inputs that previously panicked now return ErrInvalidData, matching how other malformed PRELOGIN bodies are already handled in this function.

Issue Tracking

No existing issue; found while reviewing the PRELOGIN parser.

Signed-off-by: Chris (ChrisJr404) <11917633+ChrisJr404@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant