Fix heap overflow in mpMergePatch via malicious map count - #15
Merged
Conversation
An MP_MAP32 header can declare a pair count up to 0x80000001. This value was multiplied by sizeof(MpPatchEntry)/sizeof(int) and passed to sqlite3_malloc(int), which truncates 64-bit sizes to a signed 32-bit int, causing a tiny under-allocation. The subsequent loop then wrote pCount entries into the undersized buffer, producing a heap-buffer-overflow reachable via msgpack_patch() on untrusted msgpack blobs (found via libFuzzer + ASan). Fixes: - Reject patch map headers whose declared pair count cannot fit in the remaining buffer (every pair needs >=2 bytes), rejecting malformed/truncated input before any allocation. - Switch the pIdx and phash allocations from sqlite3_malloc(int) to sqlite3_malloc64(sqlite3_uint64), removing the signed-int truncation entirely. Adds regression checks in test_spec_p5_mutation.c (6.10-6.12) for bogus MAP32/MAP16 counts and a truncated MAP32 header, plus the minimized crash input as a new fuzz corpus seed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Fixes a heap-buffer-overflow in
mpMergePatch(the RFC-7386msgpack_patch()SQL function), found via a 30-second libFuzzer + ASan run againstfuzz_msgpack.Root cause
A patch blob's
MP_MAP32header can declare a pair count up to0x80000001(attacker-controlled). This count was multiplied bysizeof(MpPatchEntry)/sizeof(int)and passed tosqlite3_malloc(int), which takes a signed 32-bit int — the 64-bit product silently truncates, producing a tiny allocation. The code then writespCountentries into that undersized buffer → heap overflow.Crash trigger:
df 80 00 00 01 31 01 02fed as the patch blob tomsgpack_patch(target, patch).Fix
sqlite3_malloc(int)withsqlite3_malloc64(sqlite3_uint64)for thepIdxandphashallocations, eliminating the signed-int truncation entirely.Testing
tests/test_spec_p5_mutation.cexercising bogus MAP32/MAP16 counts and a truncated MAP32 header — all must returnSQLITE_ERROR, not crash.tests/fuzz_corpus/(per CONTRIBUTING.md guidance).ctestsuite: 16/16 passing.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com