Skip to content

Improve handling of malformed COM_STMT_EXECUTE packets#5188

Merged
renecannao merged 8 commits intov3.0from
v3.0-stmt_exec_bounds_check
Nov 6, 2025
Merged

Improve handling of malformed COM_STMT_EXECUTE packets#5188
renecannao merged 8 commits intov3.0from
v3.0-stmt_exec_bounds_check

Conversation

@JavierJF
Copy link
Collaborator

This PR introduces two changes to improve the handle of malformed (according to protocol) COM_STMT_EXECUTE packets:

  • Add boundary checks in 'MySQL_Protocol::get_binds_from_pkt': This checks ensure that some flows are safe when decoding params from malformed COM_STMT_EXECUTE packets.
  • Change 'COM_STMT_EXECUTE' params length decoding: Fixes compatibility between ProxySQL and MySQL regarding the handling of COM_STMT_EXECUTE packets which have parameters with length bigger than 2^24. In this scenario, ProxySQL was decoding packets using 8-byte length encoded integers, as protocol specifies. MySQL itself isn't honoring that description, and it's decoding parameter length using only the lower 4-bytes from the 8-bytes integer. This change honors MySQL behavior and improves compatibility with clients that abuse this behavior. As an example of this, NodeJS connector mysql2 is known to have this behavior.

@gemini-code-assist
Copy link

Summary of Changes

Hello @JavierJF, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the robustness and compatibility of ProxySQL's handling of COM_STMT_EXECUTE packets, particularly those that are malformed or deviate from strict protocol specifications. By introducing explicit boundary checks and aligning parameter length decoding with MySQL's observed behavior, the changes prevent potential crashes and ensure smoother interaction with diverse client applications.

Highlights

  • Enhanced Malformed Packet Handling: Implemented boundary checks within MySQL_Protocol::get_binds_from_pkt to safely decode parameters from malformed COM_STMT_EXECUTE packets, preventing potential crashes.
  • MySQL Protocol Compatibility Fix: Adjusted the decoding of COM_STMT_EXECUTE parameter lengths to match MySQL's actual behavior (using only the lower 4-bytes for lengths greater than 2^24), improving compatibility with clients like the mysql2 NodeJS connector.
  • Refactored Length Decoding Functions: Introduced mysql_decode_length_ll for 64-bit length decoding and updated mysql_decode_length for 32-bit lengths, along with corresponding usage updates across the codebase for clarity and correctness.
  • New Regression Test: Added a comprehensive regression test (reg_test_stmt_inv_param_offset-t.cpp) to validate the improved handling of malformed COM_STMT_EXECUTE packets, covering various scenarios of mangled parameter lengths.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces important improvements for handling malformed COM_STMT_EXECUTE packets. The addition of boundary checks in get_binds_from_pkt is a critical fix for security and stability. The change in parameter length decoding to align with MySQL's behavior is a good compatibility improvement. The code is well-structured, and the introduction of mysql_decode_length_ll to separate the 64-bit decoding logic is clean. The new test file is comprehensive and covers the new logic well. I have a few suggestions to improve code readability and maintainability.

@renecannao
Copy link
Contributor

Can one of the admins verify this patch?

@renecannao
Copy link
Contributor

Add to whitelist

@mirostauder
Copy link
Contributor

retest this please

JavierJF and others added 8 commits November 5, 2025 17:39
These checks should prevent crashes when packets are received holding
invalid lengths in the processed params.
According to MySQL protocol, variable length strings are encoded using
length encoded integers. For reference, see:

- https://dev.mysql.com/doc/dev/mysql-server/9.4.0/page_protocol_com_stmt_execute.html
- https://dev.mysql.com/doc/dev/mysql-server/9.4.0/page_protocol_basic_dt_integers.html#a_protocol_type_int2

The protocol specifies that values greater than 2^24 (16777216) should
be encoded using '0xFE + 8-byte integer'. Yet, in reality MySQL ignores
the upper section of these 8-byte integers, treating them effectively
like '4-bytes'. For the sake of compatibility this commit changes the
decoding behavior for 'COM_STMT_EXECUTE' to match MySQL one. This
different is subtle but important, since in practice MySQL itself
doesn't use the '8 bytes' from the field. This means that connectors
that are compatible with MySQL could find issues when sending these
packets through ProxySQL (like NodeJS 'mysql2' connector which writes
the 8-bytes as a 4-bytes duplication, motivating these changes),
situation that could result in rejection due to malformed packet
detection (or crashes/invalid handling in the worse case scenario).

The previous decoding function is now renamed into
'mysql_decode_length_ll' to honor MySQL naming 'net_field_length_ll'.
For now, this protocol change is limited to 'COM_STMT_EXECUTE'.
The test exercises the boundary checks for 'COM_STMT_EXECUTE' params
decoding and ensures the correct processing of malformed packets which
only mangle the upper '4-bytes' of the param length-encoded integer.
Signed-off-by: Miro Stauder <miro@proxysql.com>
Connection requires root credentials for modifying 'max_allowed_packet'.
Since the test requires to modify 'max_allowed_packet' to work, it
cannot reuse previous connections from the conn_pool. New connections
must be created, to ensure that the new defaults are used.
Replace 'MYSQL_QUERY' macro with verbose alternative 'MYSQL_QUERY_T'.
@JavierJF JavierJF force-pushed the v3.0-stmt_exec_bounds_check branch from 08a8e05 to 1a874de Compare November 5, 2025 16:59
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 5, 2025

@renecannao renecannao merged commit cce161b into v3.0 Nov 6, 2025
141 of 154 checks passed
@renecannao renecannao deleted the v3.0-stmt_exec_bounds_check branch March 7, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants