Skip to content

fix(frontend): reject overflowing length-encoded lengths instead of panicking - #29860

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/stmt-execute-lenenc-overflow
Oct 11, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
MatrixAdventurer:fix/stmt-execute-lenenc-overflow

Conversation

@MatrixAdventurer

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #29859

Fixes #29859

What this PR does / why we need it:

readStringLenEnc converted the client-supplied length-encoded uint64 to int before checking it against the packet. A length above MaxInt64 (e.g. fe 30 30 30 30 30 30 30 b3) became negative, passed pos+sLength-1 >= len(data), and the slice panicked. readCountOfBytes had the same hole for negative counts; the handshake auth-response length goes through that path with int(l).

Fix (1 file):

  • readStringLenEnc compares the length as uint64 against the remaining bytes before converting.
  • readCountOfBytes rejects negative counts and out-of-range positions.
  • The caller already turns ok == false into invalid input: mysql protocol error, malformed packet.

Verification

Unit tests:

  • New TestReadLenEncRejectsOverflowingLength panics on main and passes with the fix.
  • go test ./pkg/frontend/ passes, and go vet is clean.

End-to-end setup: built mo-service from main (d7752ff) and from this branch, ran each single-node (-launch etc/launch/launch.toml), and sent raw protocol packets with PyMySQL. The client prepared SELECT ?, then sent COM_STMT_EXECUTE with the issue's body 00 01 00 00 00 00 01 00 00 fe 30 30 30 30 30 30 30 b3, then ran SELECT 1 on the same connection.

main this PR
Execute response ERR 20101 internal error: panic runtime error: slice bounds out of range [:-5534870958513311674] + full Go stack trace returned to the client ERR 20301 invalid input: mysql protocol error, malformed packet
"slice bounds out of range" in server log yes 0
SELECT 1 afterwards on the same connection ok ok
Valid binary execute with string param 'hi' — ok

BVT: mo-tester test/distributed/cases/prepare gave 3336 total / 0 failed on this build.

The handshake auth-response path is covered by the readCountOfBytes guard and the unit test, but I didn't exercise it on the wire.

…anicking

readStringLenEnc converted a client-supplied uint64 length to int before
the bounds check, so a length above MaxInt64 became negative, passed the
check and panicked on the slice (COM_STMT_EXECUTE string params).
readCountOfBytes had the same negative-count hole. Compare in uint64 and
reject negative counts.

Fixes matrixorigin#29859

Signed-off-by: MatrixAdventurer <[email protected]>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@CLAassistant

CLAassistant commented Oct 11, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@XuPeng-SH XuPeng-SH left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

APPROVE on 14fcebc4fe02de4ce796494c1167f3e2bc4a14c3, base/merge-base 4022729da24d71b082c22b52aaed3faba2b77ea5. Direct deep review; no subagents or product edits. No blocking finding in this scoped fix.

The design fixes the existing byte-reader owner rather than adding a second packet validator. readStringLenEnc compares the unsigned length against remaining bytes before int conversion, which proves the later addition/slice safe. readCountOfBytes checks count and position before subtraction; zero-length-at-end and exact remaining-length reads remain valid. Both changes use constant-time bounds checks, with no new allocation, copy, state or execution path on valid prepared/TP traffic.

I traced callers in COM_STMT_EXECUTE, COM_CHANGE_USER, long-data and handshake auth-response parsing. Their failed-read checks return the existing malformed/error path before publishing parameter/session state. A negative offset to readIntLenEnc is not supplied by these network entry points; I did not manufacture an unreachable helper call as a finding. The patch does not claim a complete audit of every handshake field.

Evidence: the new overflow UT and existing length-prefix tests pass. I additionally extended the existing real wire-loop fixture with the exact overflowing VAR_STRING execute packet: the server returns a malformed-packet error without panic text, preserves the previous prepared parameter types/value, releases params, handles PING, and executes subsequent valid binds on the same connection; fixture shutdown passes. Four selected test roots pass. This probe uses the real packet/routine loop with the existing bounded net.Pipe fixture, not a recreated parser. Source overlays use this exact production file and the original wire test on a native-compatible checkout; all production differences to this head are restored, and the probe is not a delivered source modification. Exact-head Linux UT/SCA CI is green; the author's real-service binary execute and prepare-BVT evidence provides additional coverage, not a claim that handshake was tested on the wire.

Accounting: implementation +7/-3; focused tests +56/-0; documentation0. The small new test preserves valid empty/nonempty and exact-end controls and rejects both huge and truncated lengths. No redundant fixture/framework, retired path or prior substantive comment was found.

Post-submission head verification: Mergify merged main into this branch, moving the head to da2384fe872f83889eb0e3dd4e1ac4f7d06e399c and the base to 03c34f837b1b399587e9d7e7e2f64a92010806e3. GitHub reassociated this review with the new head. I fetched and compared both versions: the PR diff against its new base is byte-for-byte identical to the reviewed diff against4022729; both changed frontend file blobs are identical. The imported main changes concern idxcron identifier quoting and DDL test coverage, with no change to the reviewed protocol-reader/caller/wire-test closure. The analysis and selected wire evidence above are therefore reused for this exact current content; they were executed on the original reviewed production closure, not rerun or relabeled as new-head CI. Approval remains applicable.

@mergify mergify Bot added the queued label Oct 11, 2026
@mergify

mergify Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-11 06:51 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-10-11 07:03 UTC · at da2384fe872f83889eb0e3dd4e1ac4f7d06e399c

This pull request spent 12 minutes 9 seconds in the queue, with no time running CI.

Reason

Pull request #29860 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Waiting for any of
  • check-neutral = CI Required
  • check-skipped = CI Required
  • check-success = CI Required
All conditions

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch had an error being deployed

1 failed deployment
ci — da2384fe Deployed Oct 11, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Prepared execute panics when length-encoded parameter length overflows int

4 participants