MySQL Proxy: Outbound result packets >= 16 MB (0x00ffffff bytes) are not split across MySQL wire packets
Description
In packages/mysql-proxy, inbound client messages split across multiple MySQL wire packets (>= 0x00ffffff / 16,777,215 bytes) are already reassembled in MySQL_Session::read_message(), but outbound server responses in MySQL_Protocol / MySQL_Session are always framed with a single MySQL_Protocol::build_packet() call (pack( 'VXCa*', strlen( $payload ), $sequence_id, $payload )).
Per the MySQL basic packet protocol specification, any logical payload whose size reaches or exceeds MAX_PACKET_LENGTH (0x00ffffff = 16,777,215 bytes) must be split into chunks of 0x00ffffff bytes terminated by a final packet shorter than 0x00ffffff bytes (an empty 0-byte packet when the payload size is an exact multiple of 0x00ffffff), incrementing the 8-bit packet sequence ID on every wire packet.
Currently, two protocol framing bugs occur whenever a SELECT row (e.g., a LONGTEXT or LONGBLOB column) produces an encoded Text::ResultsetRow payload >= 0x00ffffff bytes:
- Payload length
=== 0x00ffffff(16,777,215bytes, e.g., a single-column16,777,211-byte string + 4-byte\xfdlength prefix):build_row_packet()emits a single0x00ffffff-byte packet without the required0-byte terminating packet. Standard MySQL clients (PDO_MySQL,mysqli,mysqlCLI) treat a0x00ffffff-length packet as incomplete, concatenate the subsequentOK/EOFpacket onto the row payload, and hang waiting for end-of-result. - Payload length
> 0x00ffffff(>= 16,777,216bytes):pack( 'VXCa*', strlen( $payload ), ... )silently truncatesstrlen( $payload )modulo2^24in the 3-byte packet header while writing the full unframed payload to the socket, corrupting the wire stream and causing clients to fail withPackets out of order.
Steps to Reproduce
Connect to wp-mysql-proxy with PDO (pdo_mysql) or mysqli and query a LONGTEXT row >= 16,777,211 bytes:
$pdo->exec( 'CREATE TABLE t (v LONGTEXT)' );
$pdo->exec( "INSERT INTO t (v) VALUES ('" . str_repeat( 'a', 1048576 ) . "')" );
for ( $i = 0; $i < 4; $i++ ) {
$pdo->exec( 'UPDATE t SET v = CONCAT(v, v)' );
}
// 1. Exact 0x00ffffff row payload (swallows trailing EOF/OK packet and hangs):
$pdo->query( 'SELECT SUBSTRING(v, 1, 16777211) FROM t' )->fetchColumn();
// 2. > 0x00ffffff row payload (wraps 24-bit packet header and corrupts packet stream):
$pdo->query( "SELECT CONCAT(v, 'tail') FROM t" )->fetchColumn();
Expected Behavior
- Outbound payloads
>= MySQL_Protocol::MAX_PACKET_LENGTH(0x00ffffff) should be split into0x00ffffff-byte packets terminated by a< 0x00ffffffpacket (including a0-byte packet on exact multiples of0x00ffffff), advancing$this->packet_idacross every emitted wire packet so trailing rows andOK/EOFpackets carry synchronized sequence IDs. MySQL_Protocol::build_packet()should reject single-frame payloads> MySQL_Protocol::MAX_PACKET_LENGTHwith aMySQL_Proxy_Exceptioninstead of silently truncating the 24-bit length header.