[mysqlnd] Fix OK packet message length buffer over-read - #287
Open
iliaal wants to merge 1 commit into
Open
Conversation
The OK packet message-length varint is read after the last bounds check, so a length-encoded integer at the end of a packet can advance p past header.size and even past the end of the 4096-byte command buffer. The old MIN(net_len, buf_len - (p - begin)) clamp then underflows and passes an unclamped attacker-controlled length to mnd_pestrndup(), reading heap memory beyond both the packet and its allocation; even in the non- overflowing case the message was filled with stale command-buffer bytes. Bail out via BAIL_IF_NO_MORE_DATA once the varint advances p past the payload and clamp against header.size instead of buf_len, matching the hardening already applied to php_mysqlnd_auth_response_read() and the other packet readers by GHSA-h35g-vwh6-m678; an audit found no further readers using the vulnerable buf_len clamp.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
php_mysqlnd_ok_read() reads the length-encoded message length after its last bounds check, so a malicious server can place the varint at the very end of an OK packet that fills the 4096-byte command buffer. The varint read then advances p past header.size and past the end of the command buffer, the old MIN(net_len, buf_len - (p - begin)) clamp underflows to a huge value, and mnd_pestrndup() copies an unclamped attacker-controlled number of bytes from beyond the allocation; with smaller packets the message is filled with stale command-buffer content, which the new fake-server test demonstrates as mysqli_info() returning leftover greeting bytes. The fix bails out via BAIL_IF_NO_MORE_DATA once p moves past the payload and clamps against header.size instead of buf_len, mirroring the hardening GHSA-h35g-vwh6-m678 already applied to the auth response and result-set readers; an audit found no other readers still using the vulnerable buf_len clamp.