Fix compound dictionary integer overflows#1473
Closed
ZX41R wants to merge 1 commit into
Closed
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
Closing in favor of a cleaner replacement PR with corrected commit authorship. |
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.
Fix two signed integer overflows in compound dictionary decoding that survived the partial fix in 4792c8e (#1438 / #1450).
What overflows
EnsureCompoundDictionaryInitialized(c/dec/decode.c:1556) iteratescursorin steps of1 << block_bits, whereblock_bitsis derived fromaddon->total_size. The post-4792c8eBROTLI_SAFE_ADDonly clampstotal_size <= INT_MAX = 2,147,483,647. For anytotal_size > 255 * (1 << 23) = 2,139,095,040,block_bitssettles at 23 and the cursor reaches255 * 8388608 = 2139095040; the nextcursor += 1 << block_bitsis2139095040 + 8388608 = 2147483648, which exceedsINT_MAX. Under-fwrapv,cursorwraps toINT_MIN, the loop guardcursor < total_sizestays true, and the next body executesaddon->block_map[cursor >> block_bits] = (uint8_t)indexat a negative index — a 1-byte heap write 36 bytes before the 480-byteBrotliDecoderCompoundDictionaryallocation. The clamp commit4792c8edoes not protect this site because the loop's overflow threshold is lower thanINT_MAXby8,388,607.The same
address + lengthpattern appears inInitializeCompoundDictionaryCopy(c/dec/decode.c:1573) —address + lengthis signedintaddition with no overflow check. In normal flow this is shadowed by the cursor-loop overflow above, but a fix that addresses only the cursor loop would expose this site.What this patch changes
EnsureCompoundDictionaryInitialized: changecursortosize_t, castaddon->total_sizeandchunk_offsets[index + 1]tosize_tfor comparison, cast1 << block_bitsto(size_t)1 << block_bitsfor the increment. Cursor arithmetic now lives entirely in the non-negativesize_tdomain.InitializeCompoundDictionaryCopy: replaceaddon->total_size < address + lengthwithBROTLI_SAFE_ADD(int, address, length, &end)followed byaddon->total_size < end. Reuses the helper introduced in4792c8e.Diff is +7 −5 in
c/dec/decode.c. No public API change.Verification
decode.c:1568:12: runtime error: signed integer overflow: 2139095040 + 8388608 cannot be represented in type 'int'-fwrapv, unpatched):heap-buffer-overflow on address 0x...02dc, WRITE of size 1, located 36 bytes before 480-byte regionallocated byAttachCompoundDictionaryatc/dec/decode.c:1533.ctest --output-on-failureon patched ASan+UBSan build:100% tests passed, 0 tests failed out of 28.Reachability
This is a hardening fix. The trigger requires an embedder that calls
BrotliDecoderAttachDictionarywithtotal_size > 2,139,095,040. Chromium's Compression Dictionary Transport caps each shared dictionary at 100 MiB, so that path is unaffected. The Java JNI wrapper caps individual attach calls below 1 GiB. The brotli CLI (c/tools/brotli.c) and the C API itself accept any size up toINT_MAXand are the structural exposure for this loop.I am happy to add a libFuzzer harness (e.g., extending the harness in #1443 to cover this size range) if useful.
Reported under Google's OSS VRP for hardening of the unaddressed half of #1438.