Skip to content

parquet: page header Thrift binary reads reserve Vec::with_capacity(len) from an unchecked length #12100

Closed

Description

@nlebovits

Describe the bug

ThriftReadInputProtocol::read_bytes_owned in parquet/src/parquet_thrift.rs reads a length from the file and passes it to Vec::with_capacity before it reads the value. The reader can reserve far more memory than the file contains.

With page statistics enabled, SerializedPageReader can hit this path when it reads a binary field in a page header. In the example below, a file of a few hundred bytes declares a 1 TB value. The allocation fails and the process aborts. A declared length of 1 GB causes the reader to reserve 1 GB before it returns UnexpectedEof.

I found this while fuzzing a downstream GeoParquet reader. PR #10979 added similar checks for read_thrift_vec. This report covers the Read-based decoder.

To reproduce

Add these dependencies to Cargo.toml:

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

Save this as src/main.rs. The argument sets the declared length of the statistics field.

use bytes::Bytes;
use parquet::basic::{Repetition, Type};
use parquet::column::writer::ColumnWriter;
use parquet::file::properties::{ReaderProperties, WriterProperties};
use parquet::file::reader::{FileReader, SerializedFileReader};
use parquet::file::serialized_reader::ReadOptionsBuilder;
use parquet::file::writer::SerializedFileWriter;
use parquet::schema::types::Type as SchemaType;
use std::sync::Arc;
use std::time::Instant;

fn valid_file() -> 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 file = Vec::new();
    let properties = Arc::new(WriterProperties::builder().build());
    let mut writer = SerializedFileWriter::new(&mut file, Arc::new(schema), properties).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, 2, 3], None, None).unwrap();
    }
    column.close().unwrap();
    row_group.close().unwrap();
    writer.close().unwrap();
    file
}

fn main() {
    let declared_len: u64 = std::env::args().nth(1).unwrap().parse().unwrap();
    let original = valid_file();
    let metadata_end = original.len() - 8;
    let metadata_len =
        u32::from_le_bytes(original[metadata_end..metadata_end + 4].try_into().unwrap())
            as usize;
    let footer = &original[metadata_end - metadata_len..];

    let mut file = b"PAR1".to_vec();
    // Write a data page header with compressed and uncompressed sizes of 100.
    file.extend_from_slice(&[0x15, 0x00, 0x15, 0xc8, 0x01, 0x15, 0xc8, 0x01]);
    // Write the data page fields and start the first binary statistics field.
    file.extend_from_slice(&[
        0x2c, 0x15, 0x06, 0x15, 0x00, 0x15, 0x00, 0x15, 0x00, 0x1c, 0x18,
    ]);

    // Encode the declared length, then append the footer without a field payload.
    let mut remaining = declared_len;
    while remaining >= 0x80 {
        file.push((remaining & 0x7f) as u8 | 0x80);
        remaining >>= 7;
    }
    file.push(remaining as u8);
    file.extend_from_slice(footer);

    let start = Instant::now();
    let properties = ReaderProperties::builder()
        .set_read_page_statistics(true)
        .build();
    let options = ReadOptionsBuilder::new()
        .with_reader_properties(properties)
        .build();
    let reader = SerializedFileReader::new_with_options(Bytes::from(file), options).unwrap();
    let row_group = reader.get_row_group(0).unwrap();
    let mut pages = row_group.get_column_page_reader(0).unwrap();
    let result = pages.get_next_page().map(|page| page.map(|page| page.page_type()));
    println!("n={declared_len}: {result:?}");
    println!("elapsed {:?}", start.elapsed());
}

Run cargo build in a project named repro-3, then run:

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

The process exits with status 134. With 1000 or 1000000000, get_next_page reserves the requested capacity, then returns Err(External(... UnexpectedEof ...)).

I reproduced this on parquet 60.1.0 and main at bb47f80, using rustc 1.99.1 on Linux aarch64.

Expected behavior

get_next_page should return a ParquetError when the input ends before the declared length. The reader should grow the buffer as it receives data.

Suggested fix

Use Read::take(len).read_to_end(&mut bytes) to read into an empty Vec, or cap the initial reservation at a small size such as 8 KiB and grow the buffer as bytes arrive. In either case, return an EOF error if the reader gets fewer than len bytes.

Activity

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

    @amitcoder79

    Opened #12133 — caps the initial Vec::with_capacity at 8 MB so a malformed declared length doesn't OOM before any bytes are read. The io::copy + take bound already handles the actual read.

  3. added a commit that references this issue on Nov 13, 2026
    df096d7
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