Skip to content

parquet: read_vlq accepts varints longer than 10 bytes and wraps the value #12099

Done

Description

@nlebovits

Describe the bug

ThriftCompactInputProtocol::read_vlq in parquet/src/parquet_thrift.rs reads bytes until it finds one with the continuation bit clear. A 64-bit Thrift varint can use at most 10 bytes, but read_vlq keeps reading past that limit.

On the 11th byte, wrapping_shl turns a shift of 70 bits into a shift of 6 bits. The decoder then ORs the byte's payload into the low bits of the result. In the example below, decode_metadata accepts a malformed footer and reads the file version as 33. The original version is 1.

I found this while fuzzing a downstream GeoParquet reader. One footer used a 15-byte varint for num_children. The decoded value caused ParquetMetaDataReader::decode_metadata to request about 4.6 GB of memory. Issue #11954 tracks the allocation problem. This issue tracks the varint decoder.

To reproduce

Add this dependency to Cargo.toml:

[dependencies]
parquet = { version = "60.1.0", default-features = false }

Save this as src/main.rs:

use parquet::basic::{Repetition, Type};
use parquet::column::writer::ColumnWriter;
use parquet::file::metadata::ParquetMetaDataReader;
use parquet::file::properties::WriterProperties;
use parquet::file::writer::SerializedFileWriter;
use parquet::schema::types::Type as SchemaType;
use std::sync::Arc;

// This 84-byte footer encodes the first schema element's `num_children`
// as a 15-byte varint: fourteen 0x93 bytes followed by 0x04.
const FOOTER: [u8; 84] = [
    0x15, 0x02, 0x19, 0x3c, 0x35, 0x00, 0x18, 0x0d, 0x64, 0x75, 0x63, 0x6b,
    0x5f, 0x68, 0x64, 0x73, 0x63, 0x62, 0x65, 0x6d, 0x61, 0x15, 0x93, 0x93,
    0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93, 0x93,
    0x04, 0x00, 0x15, 0x04, 0x25, 0x02, 0x18, 0x02, 0x69, 0x64, 0x19, 0x01,
    0x00, 0x15, 0x0c, 0x25, 0x02, 0x18, 0x08, 0x67, 0x65, 0x7a, 0x6f, 0x6d,
    0x65, 0x19, 0x1c, 0x19, 0x90, 0x01, 0x26, 0xfc, 0xf6, 0x02, 0x16, 0x20,
    0x62, 0x31, 0x24, 0x30, 0x61, 0x16, 0xfa, 0x91, 0x01, 0x16, 0x29, 0x00,
];

/// Write a small Parquet file and return its Thrift metadata bytes.
fn valid_metadata() -> Vec<u8> {
    let schema = SchemaType::group_type_builder("s")
        .with_fields(vec![Arc::new(
            SchemaType::primitive_type_builder("a", Type::INT32)
                .with_repetition(Repetition::REQUIRED)
                .build()
                .unwrap(),
        )])
        .build()
        .unwrap();

    let mut out = Vec::new();
    let props = Arc::new(WriterProperties::builder().build());
    let mut writer = SerializedFileWriter::new(&mut out, Arc::new(schema), props).unwrap();
    let mut row_group = writer.next_row_group().unwrap();
    let mut column = row_group.next_column().unwrap().unwrap();
    if let ColumnWriter::Int32ColumnWriter(column) = column.untyped() {
        column.write_batch(&[1], None, None).unwrap();
    }
    column.close().unwrap();
    row_group.close().unwrap();
    writer.close().unwrap();

    let end = out.len() - 8;
    let len = u32::from_le_bytes(out[end..end + 4].try_into().unwrap()) as usize;
    out[end - len..end].to_vec()
}

fn main() {
    match std::env::args().nth(1).unwrap().as_str() {
        "wrap" => {
            let metadata = valid_metadata();
            // 0x15 is the version field header. 0x02 encodes version 1.
            assert_eq!(&metadata[..2], &[0x15, 0x02]);
            let original = ParquetMetaDataReader::decode_metadata(&metadata).unwrap();
            println!("original version = {}", original.file_metadata().version());

            // Replace the version with an 11-byte varint:
            // 0x82, nine 0x80 bytes, then 0x01.
            let mut malformed = vec![0x15, 0x82];
            malformed.extend([0x80; 9]);
            malformed.push(0x01);
            malformed.extend_from_slice(&metadata[2..]);

            let decoded = ParquetMetaDataReader::decode_metadata(&malformed).unwrap();
            println!("11-byte varint version = {}", decoded.file_metadata().version());
        }
        "oom" => {
            println!("{:?}", ParquetMetaDataReader::decode_metadata(&FOOTER).map(|_| ()));
        }
        _ => unreachable!(),
    }
}

Run cargo run -- wrap:

original version = 1
11-byte varint version = 33

To reproduce the allocation failure, run cargo build in a project named repro-1, then run this command with a memory limit:

(ulimit -v 2000000 && timeout 60 ./target/debug/repro-1 oom)
memory allocation of 4599335088 bytes failed
timeout: the monitored command dumped core

Both cases produced the same results on parquet 60.1.0 and main at bb47f80, with rustc 1.99.1 on Linux aarch64.

Expected behavior

read_vlq should reject any 64-bit varint that uses more than 10 bytes or has a 10th byte greater than 0x01. decode_metadata should return Err for both malformed footers above.

Suggested fix

On the 10th byte, reject any value above 0x01 before shifting. Only one payload bit fits at that position, and the continuation bit must be clear. This check lets the decoder use a regular shift and prevents it from reading an 11th byte.

This change would reject the 15-byte num_children before it reaches the allocation code. Issue #11954 tracks how the reader handles large counts that decode successfully.

Activity

  1. self-assigned this
    on Nov 6, 2026
  2. amitcoder79 commented on Nov 9, 2026

    @amitcoder79

    Opened #12132 with the fix — added a shift > 63 check in read_vlq that returns a new ThriftProtocolError::InvalidVlq error. Compiles and existing tests pass.

    I'll look at #12100 (the read_bytes_owned allocation issue) next — same fuzzing session, similar fix pattern.

  3. added a commit that references this issue on Dec 3, 2026
    50b25a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions