Repository navigation
Cherry pick #108474 to 26.3: Fix SQL injection in MySQL wire protocol replacement queries - #120206
Merged
Merged
Conversation
The MySQL handler rewrites a few client commands into ClickHouse SQL by concatenating the raw client-supplied suffix into the replacement query without parsing or escaping it. An authenticated MySQL-wire client could inject SQL: - showTableStatusReplacementQuery concatenated the SHOW TABLE STATUS LIKE argument after "WHERE name LIKE ", so "" UNION SELECT ... FROM system.users read arbitrary tables instead of the intended system.tables-only lookup. - setSettingReplacementQuery concatenated the SET value tail, so "SET SQL_SELECT_LIMIT=1, max_threads=42" set unrelated settings. Both now parse the client argument (a single string literal for the LIKE pattern; "= <literal|DEFAULT>" for the setting value) and re-serialize it with quoteString()/FieldVisitorToString instead of concatenating raw input. The injection runs under the caller's own grants, so there is no privilege escalation, but it bypasses the intended read scope. checkShouldReplaceQuery is only a byte-prefix check, so the setting name in setSettingReplacementQuery is now checked for a word boundary before the sanitized replacement is produced. Otherwise a longer variable that merely starts with a mapped name (e.g. "SET SQL_SELECT_LIMITED=1" vs the mapped "SET SQL_SELECT_LIMIT") would be translated and silently reset the unrelated "limit" setting; it now passes through untranslated and errors as an unknown setting. The sanitized parsers accept one optional trailing ';' followed by end-of-stream. executeQuery treats ';' as end-of-query, so a benign programmatic client may send "SHOW TABLE STATUS LIKE 'one';" or "SET SQL_SELECT_LIMIT=2;" (the mysql CLI strips the terminator, but other clients pass it through). Requiring a strict end-of-stream broke those benign inputs. A second statement after the ';' still leaves the parser before end-of-stream and is rejected, so the injection protection is intact. setSettingReplacementQuery no longer has a state-changing fallback on a malformed value. The previous "reset the mapped setting to DEFAULT" fallback silently changed the caller's session setting (and reported success) when the tail was not exactly "= <literal|DEFAULT>" with an optional single ';' (for example "SET SQL_SELECT_LIMIT=1, max_threads=42" parses the "1", stops at the comma, and reset "limit"). It now throws a syntax error in that case, so the session setting is never changed as a side effect of a rejected input and the raw tail is never executed. killConnectionIdReplacementQuery used the regex "^[0-9]+$", which rejected a benign single-statement terminator: "KILL QUERY 12;" left the suffix "12;", failed the match, and fell through to the raw "KILL QUERY 12;" that ClickHouse does not support. The connection id is now captured with "^\s*([0-9]+)\s*;?\s*$" (one optional trailing ';' and surrounding whitespace) and only the captured digits are substituted, so multi-digit ids and a trailing terminator work while any non-numeric tail or a second statement still fails the match. The dispatcher matches the registered keys "KILL QUERY" and "SHOW TABLE STATUS LIKE" without their trailing separator, but the helpers slice the suffix at prefix.length() which includes the space. A malformed command would skip the byte at the separator position and be coerced into a valid command: "KILL QUERY;12" and "KILL QUERYx12" reached the digit regex as "12" (cancelling query id 12), and "SHOW TABLE STATUS LIKEx'a'" became a lookup of 'a'. Both helpers now require the byte at the separator position to be whitespace before slicing, so a missing or non-space separator is rejected (KILL QUERY passes through and errors; SHOW TABLE STATUS matches nothing). Added a regression test (test_mysql_replacement_query_injection) that fails on the previous behavior and passes with this fix, covering the SQL_SELECT_LIMITED boundary case, the trailing-semicolon terminator for SHOW TABLE STATUS / SET / KILL QUERY, the rejection of an injected SET tail (without changing "limit" or "max_threads"), the rejection of a second statement after ';', and the missing-separator cases for KILL QUERY and SHOW TABLE STATUS LIKE. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ery-injection Fix SQL injection in MySQL wire protocol replacement queries
The `ErrorCodes` block in `MySQLHandler.cpp` conflicted: the fix appends `SYNTAX_ERROR`, while the release branch declares both `SYNTAX_ERROR` and `UNKNOWN_PACKET_FROM_CLIENT`, the latter dropped on master. Keep the branch block unchanged, because it already provides what the fix needs.
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.
Original pull-request #108474
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job