sqlite-database-integration icon indicating copy to clipboard operation
sqlite-database-integration copied to clipboard

MySQL Proxy: Outbound result packets >= 16 MB (0x00ffffff bytes) are not split across MySQL wire packets

Open 0xABCD01 opened this issue 1 month ago • 0 comments

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:

  1. Payload length === 0x00ffffff (16,777,215 bytes, e.g., a single-column 16,777,211-byte string + 4-byte \xfd length prefix): build_row_packet() emits a single 0x00ffffff-byte packet without the required 0-byte terminating packet. Standard MySQL clients (PDO_MySQL, mysqli, mysql CLI) treat a 0x00ffffff-length packet as incomplete, concatenate the subsequent OK/EOF packet onto the row payload, and hang waiting for end-of-result.
  2. Payload length > 0x00ffffff (>= 16,777,216 bytes): pack( 'VXCa*', strlen( $payload ), ... ) silently truncates strlen( $payload ) modulo 2^24 in the 3-byte packet header while writing the full unframed payload to the socket, corrupting the wire stream and causing clients to fail with Packets 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 into 0x00ffffff-byte packets terminated by a < 0x00ffffff packet (including a 0-byte packet on exact multiples of 0x00ffffff), advancing $this->packet_id across every emitted wire packet so trailing rows and OK/EOF packets carry synchronized sequence IDs.
  • MySQL_Protocol::build_packet() should reject single-frame payloads > MySQL_Protocol::MAX_PACKET_LENGTH with a MySQL_Proxy_Exception instead of silently truncating the 24-bit length header.

0xABCD01 avatar Nov 01 '26 11:11 0xABCD01