Skip to content

[c] check realloc result in deflate and lzma decoders - #4098

Open
ArjunD3456 wants to merge 1 commit into
apache:mainfrom
ArjunD3456:c-codec-realloc-check
Open

ArjunD3456 wants to merge 1 commit into
apache:mainfrom
ArjunD3456:c-codec-realloc-check

Conversation

@ArjunD3456

Copy link
Copy Markdown

What is the purpose of the change

decode_deflate and decode_lzma in lang/c/src/codec.c grow the output buffer by doubling it with avro_realloc whenever the decompressor runs out of room, and assign the result straight back to block_data. If that realloc fails, decode_deflate sets next_out = NULL + total_out while avail_out still advertises the doubled size, so inflate() 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 through avro_set_allocator) ends in a write through a wild pointer. decode_lzma has 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:

  • Added test_avro_codec_alloc (registered with add_avro_test_checkmem). It installs an allocator that refuses requests over 1 MiB, encodes 4 MiB of zeros with each available codec, and checks that avro_codec_decode fails cleanly.
  • Before the fix the deflate case crashes with an access violation writing to address 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.
  • I ran the test against codec.c built 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 test suite on that machine, so CI is the first full run.

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? not applicable

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant