From 7b700868b761b2bcd7c242e6bdf2659d19667075 Mon Sep 17 00:00:00 2001 From: Arjun D Date: Mon, 16 Nov 2026 00:36:29 +0530 Subject: [PATCH] check realloc result in deflate and lzma decoders 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. --- lang/c/src/codec.c | 15 +++- lang/c/tests/CMakeLists.txt | 1 + lang/c/tests/test_avro_codec_alloc.c | 121 +++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 lang/c/tests/test_avro_codec_alloc.c diff --git a/lang/c/src/codec.c b/lang/c/src/codec.c index 176fb21d657..41c8b0e718f 100644 --- a/lang/c/src/codec.c +++ b/lang/c/src/codec.c @@ -342,7 +342,13 @@ static int decode_deflate(avro_codec_t c, void * data, int64_t len) // The buffer was not big enough. resize it. if (err == Z_BUF_ERROR) { - c->block_data = avro_realloc(c->block_data, c->block_size, c->block_size * 2); + void *new_data = avro_realloc(c->block_data, c->block_size, c->block_size * 2); + if (!new_data) { + inflateReset(s); + avro_set_error("Cannot allocate memory for deflate"); + return 1; + } + c->block_data = new_data; s->next_out = c->block_data + s->total_out; s->avail_out += c->block_size; c->block_size = c->block_size * 2; @@ -486,7 +492,12 @@ static int decode_lzma(avro_codec_t codec, void * data, int64_t len) // If it ran out of space to decode, give it more!! // It will continue where it left off because of read_pos and write_pos. if (ret == LZMA_BUF_ERROR) { - codec->block_data = avro_realloc(codec->block_data, codec->block_size, codec->block_size * 2); + void *new_data = avro_realloc(codec->block_data, codec->block_size, codec->block_size * 2); + if (!new_data) { + avro_set_error("Cannot allocate memory for lzma decoder"); + return 1; + } + codec->block_data = new_data; codec->block_size = codec->block_size * 2; } diff --git a/lang/c/tests/CMakeLists.txt b/lang/c/tests/CMakeLists.txt index 6b4164fa740..6faf27e24ba 100644 --- a/lang/c/tests/CMakeLists.txt +++ b/lang/c/tests/CMakeLists.txt @@ -88,3 +88,4 @@ add_avro_test_checkmem(test_avro_1691) add_avro_test_checkmem(test_avro_1906) add_avro_test_checkmem(test_avro_1904) add_avro_test_checkmem(test_avro_4246) +add_avro_test_checkmem(test_avro_codec_alloc) diff --git a/lang/c/tests/test_avro_codec_alloc.c b/lang/c/tests/test_avro_codec_alloc.c new file mode 100644 index 00000000000..93f9ed46d78 --- /dev/null +++ b/lang/c/tests/test_avro_codec_alloc.c @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +/* + * A small compressed block that inflates past what the allocator will + * hand out must make avro_codec_decode fail cleanly, not write through + * a NULL block buffer or leak the old one. + */ + +#include +#include +#include + +#include "avro.h" +#include "avro_private.h" +#include "codec.h" + +#define ALLOC_CAP (1024 * 1024) +#define RAW_SIZE (4 * 1024 * 1024) + +static int cap_enabled = 0; + +static void * +capped_allocator(void *ud, void *ptr, size_t osize, size_t nsize) +{ + AVRO_UNUSED(ud); + AVRO_UNUSED(osize); + + if (nsize == 0) { + free(ptr); + return NULL; + } + if (cap_enabled && nsize > ALLOC_CAP) { + return NULL; + } + return realloc(ptr, nsize); +} + +static int +test_codec(const char *name) +{ + struct avro_codec_t_ encoder; + struct avro_codec_t_ decoder; + char *raw; + char *compressed; + int64_t compressed_len; + int rval; + + raw = (char *) calloc(1, RAW_SIZE); + if (!raw) { + fprintf(stderr, "Cannot allocate test buffer\n"); + return 1; + } + + if (avro_codec(&encoder, name) || + avro_codec_encode(&encoder, raw, RAW_SIZE)) { + fprintf(stderr, "Cannot encode %s block: %s\n", + name, avro_strerror()); + free(raw); + return 1; + } + free(raw); + + compressed_len = encoder.used_size; + compressed = (char *) malloc(compressed_len); + memcpy(compressed, encoder.block_data, compressed_len); + avro_codec_reset(&encoder); + + if (avro_codec(&decoder, name)) { + fprintf(stderr, "Cannot create %s codec: %s\n", + name, avro_strerror()); + free(compressed); + return 1; + } + + cap_enabled = 1; + rval = avro_codec_decode(&decoder, compressed, compressed_len); + cap_enabled = 0; + + avro_codec_reset(&decoder); + free(compressed); + + if (rval == 0) { + fprintf(stderr, "%s: decoding past the allocator limit " + "should have failed\n", name); + return 1; + } + + fprintf(stderr, "%s: %s\n", name, avro_strerror()); + return 0; +} + +int main(void) +{ + int failed = 0; + + avro_set_allocator(capped_allocator, NULL); + +#ifdef DEFLATE_CODEC + failed |= test_codec("deflate"); +#endif +#ifdef LZMA_CODEC + failed |= test_codec("lzma"); +#endif + + return failed ? EXIT_FAILURE : EXIT_SUCCESS; +}