Repository navigation
fix(mysql): return error instead of panic on truncated OK packet - #4176
Conversation
d0ca316 to
0ffef15
Compare
| let affected_rows = buf.get_uint_lenenc(); | ||
| let last_insert_id = buf.get_uint_lenenc(); |
There was a problem hiding this comment.
I don't want to move the goalposts too much, but it seems like these fields should at least be covered by the length check too.
In the long run we should probably ban use of bytes::Buf methods and instead write our own that return Result.
There was a problem hiding this comment.
Checking get_uint_lenenc calls is a bit trickier since the method advances a variable number of bytes depending on the prefix. I've changed its signature to return Result and added remaining-bytes checks inside the method, propagating with ? at all call sites.
If this approach is good, I could follow up with a separate PR that wraps the other panicking bytes::Buf methods in the same way.
There was a problem hiding this comment.
I was mostly thinking that we should just check that the packet is nonempty, but this is a great improvement, thanks!
If this approach is good, I could follow up with a separate PR that wraps the other panicking
bytes::Bufmethods in the same way.
I think that would be a good idea, there's a lot of shortcuts that we took early on that are coming back to bite us and using things like this that panic instead of returning errors is a big one.
53b3be5 to
33b6165
Compare
Change get_uint_lenenc to return Result and validate buffer has sufficient bytes before each read
33b6165 to
af6fb80
Compare
…ostile servers Found during a security review of server-controlled parsing paths (threat model: rogue or MITM MySQL/MariaDB server; handshake parsing is reachable pre-authentication). ### Does your PR solve an issue? No tracking issue — reported here directly. This extends the fix pattern of transact-rs#4176 (truncated `OK` packet) to the rest of the protocol; transact-rs#4451 documents the same review on the PostgreSQL side. ### The problem - `recv_packet`'s continuation loop accumulated an unbounded number of `0xFFFFFF`-sized parts: a hostile server streams endless 16 MiB frames and the client OOMs at near-zero bandwidth cost. - The result-set column count (a length-encoded u64) fed `HashMap::with_capacity` / `Vec::reserve` directly; a ~10-byte packet claiming 2^40 columns aborts the process. - Dozens of decode sites call panicking `bytes::Buf` getters / slice indexing / `advance` on untrusted buffers: truncated ERR packets, every field of `HandshakeV10` (including the attacker-controlled auth-plugin-data length), length-encoded strings claiming more than the remaining bytes, `TextRow` field-count mismatch, `BinaryRow` (null bitmap, temporal length byte, fixed-width and lenenc value bounds — plus an `unreachable!()` actually reachable via a NULL-typed column), short EOF packets, and a 1-byte `caching_sha2_password` continue packet. The OkPacket decoder was hardened in transact-rs#4176; the rest of the protocol was not. ### The change - Cumulative 1 GiB cap on packet reassembly (`MySql`'s own `max_allowed_packet` hard ceiling); protocol error beyond it. - Result sets claiming more than 4096 columns (the MySQL server hard limit) are protocol errors before any allocation. - All listed sites check remaining length up front and return `Error::Protocol` describing expectation vs. reality; the `unreachable!()` is now an error. ### Is this a breaking change? No: every changed path converts a panic or abort into a returned `Err`, which callers already handle (the pool discards the connection). Newly-observable edges: aggregate packets over 1 GiB and result sets over 4096 columns are now errors rather than allocation attempts. ### Regression tests 15 new unit tests, including a sweep asserting that every strict prefix of captured MySQL 8.0.18 and MariaDB 10.4.7 handshake packets now errors instead of panicking. Each test fails (panics) without the fix. `cargo test -p sqlx-mysql --lib` passes.
…ostile servers (#4458) Found during a security review of server-controlled parsing paths (threat model: rogue or MITM MySQL/MariaDB server; handshake parsing is reachable pre-authentication). ### Does your PR solve an issue? No tracking issue — reported here directly. This extends the fix pattern of #4176 (truncated `OK` packet) to the rest of the protocol; #4451 documents the same review on the PostgreSQL side. ### The problem - `recv_packet`'s continuation loop accumulated an unbounded number of `0xFFFFFF`-sized parts: a hostile server streams endless 16 MiB frames and the client OOMs at near-zero bandwidth cost. - The result-set column count (a length-encoded u64) fed `HashMap::with_capacity` / `Vec::reserve` directly; a ~10-byte packet claiming 2^40 columns aborts the process. - Dozens of decode sites call panicking `bytes::Buf` getters / slice indexing / `advance` on untrusted buffers: truncated ERR packets, every field of `HandshakeV10` (including the attacker-controlled auth-plugin-data length), length-encoded strings claiming more than the remaining bytes, `TextRow` field-count mismatch, `BinaryRow` (null bitmap, temporal length byte, fixed-width and lenenc value bounds — plus an `unreachable!()` actually reachable via a NULL-typed column), short EOF packets, and a 1-byte `caching_sha2_password` continue packet. The OkPacket decoder was hardened in #4176; the rest of the protocol was not. ### The change - Cumulative 1 GiB cap on packet reassembly (`MySql`'s own `max_allowed_packet` hard ceiling); protocol error beyond it. - Result sets claiming more than 4096 columns (the MySQL server hard limit) are protocol errors before any allocation. - All listed sites check remaining length up front and return `Error::Protocol` describing expectation vs. reality; the `unreachable!()` is now an error. ### Is this a breaking change? No: every changed path converts a panic or abort into a returned `Err`, which callers already handle (the pool discards the connection). Newly-observable edges: aggregate packets over 1 GiB and result sets over 4096 columns are now errors rather than allocation attempts. ### Regression tests 15 new unit tests, including a sweep asserting that every strict prefix of captured MySQL 8.0.18 and MariaDB 10.4.7 handshake packets now errors instead of panicking. Each test fails (panics) without the fix. `cargo test -p sqlx-mysql --lib` passes.
Does your PR solve an issue?
fixes #4139
Is this a breaking change?
No. This changes a panic into a returned
Err(Protocol(...)), which callers already handle. The connection pool will gracefully discard the bad connection instead of crashing the tokio worker thread.