mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Malformed server packets cause panics instead of ErrMalformPkt (readLengthEncodedInteger, readStatus, prepare result, readColumns)

Open aibeshkov opened this issue 2 weeks ago • 0 comments

Following the guidance in SECURITY.md (a malicious or broken server is not in the security model), this is filed as a regular robustness bug, in the same family as #1848 and #878.

Several places read fixed offsets from server packets without checking len(data), so a short or malformed packet from a misbehaving server (or a proxy or middlebox in the way) causes a runtime panic in the client instead of ErrMalformPkt. Checked against master 4b7cc9c:

  • readLengthEncodedInteger (utils.go): 0xfc, 0xfd, 0xfe prefixes index b[1:] with no length check. A 1-byte 0xfc gives index out of range.
  • readStatus (packets.go): b[0] and b[1] with no check. Callers: handleOkPacket (data[1+n+m : 1+n+m+2], a 3-byte OK packet panics), the classic EOF branch of readRowPacket (readStatus(data[3:]), a 1-byte 0xfe row panics), and the clientDeprecateEOF branch.
  • readPrepareResultPacket: data[1:5], data[5:7], data[7:9] with no check (a COM_STMT_PREPARE OK with only the 0x00 byte panics).
  • readColumns: make([]mysqlField, count) with the column count taken from the server and not bounded by the packet; a 9-byte header declaring 5,000,000 columns allocates about 229 MiB, larger counts panic with makeslice: len out of range. Also the per-field reads inside it, with a 1-byte column definition.
  • handleErrorPacket with a bare 0xff packet (data[1:3]).

Suggested fix: check len(data) before each fixed-offset read (return ErrMalformPkt), bound the length-encoded integers, and reject column counts that the packet cannot carry before allocating. I am happy to send a PR with tests if that is welcome.

Found with a fake-server harness driving the client through database/sql. Tested on v1.10.1; the sites above were re-read on current master.

aibeshkov avatar Nov 12 '26 20:11 aibeshkov