diff --git a/cpp/src/common/allocator/byte_stream.h b/cpp/src/common/allocator/byte_stream.h index 2957761da..b403d3b5c 100644 --- a/cpp/src/common/allocator/byte_stream.h +++ b/cpp/src/common/allocator/byte_stream.h @@ -1283,6 +1283,21 @@ class SerializationUtil { int32_t len = 0; int32_t read_len = 0; if (RET_FAIL(read_var_int(len, in))) { + } else if (len < 0) { + // A var-int length is zigzag-encoded and can decode to any + // negative value. Reject it before it is used as an allocation + // size and array index. write_var_str never emits a negative + // length; write_var_char_ptr's NO_STR_TO_READ sentinel belongs to + // read_var_char_ptr. + ret = E_TSFILE_CORRUPTED; + } else if (static_cast(len) > in.remaining_size()) { + // Reject a length the stream cannot satisfy before allocating it, + // so a corrupt field cannot request a multi-GiB buffer that is + // discarded as soon as the read fails. E_PARTIAL_READ is what the + // read below used to return here; chunk/page header parsing + // relies on it to retry with a larger file window (see + // deserialize_buf_not_enough()). + ret = common::E_PARTIAL_READ; } else { char* tmp_buf = static_cast(mem_alloc(len + 1, in.get_mid())); @@ -1310,6 +1325,12 @@ class SerializationUtil { int ret = common::E_OK; int32_t len = 0; if (RET_FAIL(read_i32(len, in))) { + } else if (len < 0) { + // A fixed-width length field can hold a negative value; reject it + // before using it as an allocation size and array index. + ret = E_TSFILE_CORRUPTED; + } else if (static_cast(len) > in.remaining_size()) { + ret = common::E_PARTIAL_READ; } else { int32_t read_len = 0; char* tmp_buf = @@ -1340,6 +1361,12 @@ class SerializationUtil { int32_t len = 0; int32_t read_len = 0; if (RET_FAIL(read_i32(len, in))) { + } else if (len < 0 || + static_cast(len) > in.remaining_size()) { + // A truncated length is what read_buf() used to report here; keep + // E_PARTIAL_READ so callers that grow their read window on + // deserialize_buf_not_enough() still retry. + ret = len < 0 ? E_TSFILE_CORRUPTED : common::E_PARTIAL_READ; } else { char* buf = (char*)pa->alloc(len); if (IS_NULL(buf)) { @@ -1370,6 +1397,12 @@ class SerializationUtil { int32_t len = 0; int32_t read_len = 0; if (RET_FAIL(read_var_int(len, in))) { + } else if (len < 0 || + static_cast(len) > in.remaining_size()) { + // A truncated length is what read_buf() used to report here; keep + // E_PARTIAL_READ so callers that grow their read window on + // deserialize_buf_not_enough() still retry. + ret = len < 0 ? E_TSFILE_CORRUPTED : common::E_PARTIAL_READ; } else { char* buf = (char*)pa->alloc(len); if (IS_NULL(buf)) { diff --git a/cpp/test/common/allocator/byte_stream_test.cc b/cpp/test/common/allocator/byte_stream_test.cc index 0ef87dab1..ab0a08b7f 100644 --- a/cpp/test/common/allocator/byte_stream_test.cc +++ b/cpp/test/common/allocator/byte_stream_test.cc @@ -83,6 +83,41 @@ TEST_F(ByteStreamTest, VarStringPreservesEmbeddedNulls) { ASSERT_EQ(byte_stream_->remaining_size(), 0); } +TEST_F(ByteStreamTest, VarStringRejectsNegativeLength) { + // Var-int lengths are zigzag-encoded, so a crafted stream can decode to + // any negative value. -1 (zigzag 1) used to write tmp_buf[-1]; every + // negative length must be rejected before it reaches mem_alloc / indexing. + for (uint8_t encoded : {1, 3, 5}) { + byte_stream_->reset(); + write_to_stream(&encoded, 1); + std::string str = "unchanged"; + EXPECT_EQ(SerializationUtil::read_var_str(str, *byte_stream_), + E_TSFILE_CORRUPTED); + EXPECT_EQ(str, "unchanged"); + } +} + +TEST_F(ByteStreamTest, VarStringRejectsLengthBeyondStream) { + ASSERT_EQ(SerializationUtil::write_var_int(100, *byte_stream_), E_OK); + write_to_stream(reinterpret_cast("ab"), 2); + std::string str; + // E_PARTIAL_READ keeps the chunk/page-header retry path working. + EXPECT_EQ(SerializationUtil::read_var_str(str, *byte_stream_), + E_PARTIAL_READ); +} + +TEST_F(ByteStreamTest, FixedStringRejectsNegativeLength) { + ASSERT_EQ(SerializationUtil::write_i32(-1, *byte_stream_), E_OK); + std::string str = "unchanged"; + EXPECT_EQ(SerializationUtil::read_str(str, *byte_stream_), + E_TSFILE_CORRUPTED); + EXPECT_EQ(str, "unchanged"); + + byte_stream_->reset(); + ASSERT_EQ(SerializationUtil::write_i32(100, *byte_stream_), E_OK); + EXPECT_EQ(SerializationUtil::read_str(str, *byte_stream_), E_PARTIAL_READ); +} + TEST_F(ByteStreamTest, WriteReadTest) { const uint8_t data[] = {0x01, 0x02, 0x03}; const uint32_t data_size = sizeof(data);