Malformed server packets cause panics instead of ErrMalformPkt (readLengthEncodedInteger, readStatus, prepare result, readColumns)
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,0xfeprefixes indexb[1:]with no length check. A 1-byte0xfcgivesindex out of range.readStatus(packets.go):b[0]andb[1]with no check. Callers:handleOkPacket(data[1+n+m : 1+n+m+2], a 3-byte OK packet panics), the classic EOF branch ofreadRowPacket(readStatus(data[3:]), a 1-byte0xferow panics), and theclientDeprecateEOFbranch.readPrepareResultPacket:data[1:5],data[5:7],data[7:9]with no check (a COM_STMT_PREPARE OK with only the0x00byte 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 withmakeslice: len out of range. Also the per-field reads inside it, with a 1-byte column definition.handleErrorPacketwith a bare0xffpacket (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.