Repository navigation
[c] check realloc result in deflate and lzma decoders - #4098
Open
ArjunD3456 wants to merge 1 commit into
Open
ArjunD3456 wants to merge 1 commit into
ArjunD3456 wants to merge 1 commit into
Conversation
decode_deflate and decode_lzma grow the codec output buffer by doubling it with avro_realloc and store the result without checking it. When the realloc fails, decode_deflate points next_out at NULL + total_out and inflate() keeps writing decompressed data to that absolute address. decode_lzma leaks the previous buffer. The output size comes from the compressed block in the container file, so a small block that inflates past what the allocator can provide reaches this. Keep the old buffer on failure and return an error, and add a test that decodes a large block through a capped allocator.
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.
What is the purpose of the change
decode_deflateanddecode_lzmainlang/c/src/codec.cgrow the output buffer by doubling it withavro_reallocwhenever the decompressor runs out of room, and assign the result straight back toblock_data. If that realloc fails,decode_deflatesetsnext_out = NULL + total_outwhileavail_outstill advertises the doubled size, soinflate()carries on writing decompressed bytes to an absolute address equal to the output produced so far. How far a block inflates is decided by the compressed data in the container file, so a few KiB of deflate that expands past what the allocator will hand out (RLIMIT_AS, a 32-bit address space, or a bounded allocator installed throughavro_set_allocator) ends in a write through a wild pointer.decode_lzmahas the same unchecked realloc; liblzma rejects the NULL output buffer there, but the previous buffer is leaked. On failure the old buffer is now kept and the decoder returns an error, and the inflate stream is reset so the codec can still decode the next block.Verifying this change
This change added tests and can be verified as follows:
test_avro_codec_alloc(registered withadd_avro_test_checkmem). It installs an allocator that refuses requests over 1 MiB, encodes 4 MiB of zeros with each available codec, and checks thatavro_codec_decodefails cleanly.0x100000(NULL + the 1 MiB already produced), and the lzma case leaves one allocation outstanding. After the fix both decodes return an error and nothing is leaked.codec.cbuilt with zlib 1.3.1 and liblzma 5.4.6 on clang with-W -Wall; no new warnings. I could not run the full./build.sh testsuite on that machine, so CI is the first full run.Documentation