Skip to content

Check valid byte_range in parquet Column Chunk reading? #6255

Open

Description

@mapleFU

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

See: apache/parquet-testing#58 (comment)

When reading a corrupt file, currently, arrow-rs would have:

    /// Returns the offset and length in bytes of the column chunk within the file
    pub fn byte_range(&self) -> (u64, u64) {
        let col_start = match self.dictionary_page_offset() {
            Some(dictionary_page_offset) => dictionary_page_offset,
            None => self.data_page_offset(),
        };
        let col_len = self.compressed_size();
        assert!(
            col_start >= 0 && col_len >= 0,
            "column start and length should not be negative"
        );
        (col_start as u64, col_len as u64)
    }

Would we better check the range here?

Describe the solution you'd like

Checking the range when building the group reader or in "byte_range()"

Describe alternatives you've considered

    /// Returns the offset and length in bytes of the column chunk within the file
    pub fn byte_range(&self) -> Result<(u64, u64)> {
        let col_start = match self.dictionary_page_offset() {
            Some(dictionary_page_offset) => dictionary_page_offset,
            None => self.data_page_offset(),
        };
        let col_len = self.compressed_size();
        if col_len < 0 || col_len < 0{
            return Err(ParquetError::General(
                "column start and length should not be negative".to_string(),
            ));
        }
        (col_start as u64, col_len as u64)
    }

Additional context

Activity

  1. added
    enhancement
    parquet
    on Aug 15, 2024
  2. mapleFU commented on Aug 15, 2024

    @mapleFU
    MemberAuthor

    @alamb I'm willing to try this but I'm not so familar with parquet-rs. Do you think this would better be checked in ColumnChunkMetaData::byte_range or ColumnChunkMetaData::from_thrift? Or this is already checked, we don't require this?

  3. alamb commented on Aug 15, 2024

    @alamb
    Contributor

    @alamb I'm willing to try this but I'm not so familar with parquet-rs. Do you think this would better be checked in ColumnChunkMetaData::byte_range or ColumnChunkMetaData::from_thrift? Or this is already checked, we don't require this?

    I am not sure -- I think the first thing we should do is get a reproducer. Let me see if I can whip up some tests

  4. alamb commented on Aug 15, 2024

    @alamb
    Contributor

    See #6261 / #6262

  5. alamb commented on Aug 15, 2024

    @alamb
    Contributor

    Describe alternatives you've considered

    That looks good to me, FWIW

  6. elshanhsynv commented on Nov 8, 2026

    @elshanhsynv

    This still panics on 60.1.0 (file/metadata/mod.rs:1043), and we hit it while fuzzing a Parquet viewer with libFuzzer, so it is reachable from untrusted files. In case a deterministic reproducer helps, this rewrites a valid file's footer so column 0 has a negative data_page_offset:

    [dependencies]
    parquet = { version = "60.1.0", features = ["arrow"] }
    arrow-array = "60.1.0"
    bytes = "1"
    use std::sync::Arc;
    
    use arrow_array::{Int32Array, RecordBatch};
    use bytes::Bytes;
    use parquet::{
        arrow::{ArrowWriter, arrow_reader::ParquetRecordBatchReaderBuilder},
        file::metadata::{ParquetMetaDataReader, ParquetMetaDataWriter},
    };
    
    fn main() {
        let column = Int32Array::from(vec![1, 2, 3]);
        let batch = RecordBatch::try_from_iter([("i", Arc::new(column) as _)]).unwrap();
        let mut bytes = Vec::new();
        let mut writer = ArrowWriter::try_new(&mut bytes, batch.schema(), None).unwrap();
        writer.write(&batch).unwrap();
        writer.close().unwrap();
    
        let bytes = Bytes::from(bytes);
        let metadata = ParquetMetaDataReader::new().parse_and_finish(&bytes).unwrap();
        let mut builder = metadata.into_builder();
        let mut row_groups = builder.take_row_groups();
        let mut group = row_groups.remove(0).into_builder();
        let mut columns = group.take_columns();
        let first = columns
            .remove(0)
            .into_builder()
            .set_dictionary_page_offset(None)
            .set_data_page_offset(-100)
            .build()
            .unwrap();
        columns.insert(0, first);
        group = group.set_column_metadata(columns);
        let metadata = builder.set_row_groups(vec![group.build().unwrap()]).build();
    
        // Keep the data pages, replace the footer.
        let footer_len =
            u32::from_le_bytes(bytes[bytes.len() - 8..bytes.len() - 4].try_into().unwrap()) as usize;
        let mut forged = bytes[..bytes.len() - 8 - footer_len].to_vec();
        ParquetMetaDataWriter::new(&mut forged, &metadata).finish().unwrap();
    
        let reader = ParquetRecordBatchReaderBuilder::try_new(Bytes::from(forged))
            .unwrap()
            .build()
            .unwrap();
        for batch in reader {
            println!("{:?}", batch.map(|b| b.num_rows())); // expected: Err(...), actual: panic
        }
    }
    thread 'main' panicked at parquet-60.1.0/src/file/metadata/mod.rs:1043:9:
    column start and length should not be negative
    

    Note that ParquetMetaDataReader accepts the negative offset and the panic only happens later, when the column chunk is read. Validating in ColumnChunkMetaData::from_thrift (or when building it) would turn this into an error at open time.

  7. self-assigned this
    on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementparquet

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions