From fa1f10112fea5c71068d2913a0ff6c726bc1334c Mon Sep 17 00:00:00 2001 From: seidl Date: Thu, 3 Dec 2026 10:22:30 -0800 Subject: [PATCH 1/6] fix(parquet): enforce compact-Thrift integer and length bounds --- parquet/src/parquet_thrift.rs | 87 +++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/parquet/src/parquet_thrift.rs b/parquet/src/parquet_thrift.rs index f8006028862d..a90a458a331e 100644 --- a/parquet/src/parquet_thrift.rs +++ b/parquet/src/parquet_thrift.rs @@ -441,12 +441,12 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { /// Read an `i16`. fn read_i16(&mut self) -> ThriftProtocolResult { - Ok(self.read_zig_zag()? as _) + Ok(i16::try_from(self.read_zig_zag()?)?) } /// Read an `i32`. fn read_i32(&mut self) -> ThriftProtocolResult { - Ok(self.read_zig_zag()? as _) + Ok(i32::try_from(self.read_zig_zag()?)?) } /// Read an `i64`. @@ -458,13 +458,18 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { fn read_double(&mut self) -> ThriftProtocolResult; /// Skip a ULEB128 encoded varint. + #[inline(always)] fn skip_vlq(&mut self) -> ThriftProtocolResult<()> { - loop { + for index in 0..10 { let byte = self.read_byte()?; + if index == 9 && byte > 1 { + return Err(ThriftProtocolError::IntegerOverflow); + } if byte & 0x80 == 0 { return Ok(()); } } + unreachable!() } /// Skip a thrift [binary]. @@ -905,11 +910,12 @@ impl ThriftCompactOutputProtocol { /// Used to indicate the start of a list of `element_type` elements. pub(crate) fn write_list_begin(&mut self, element_type: ElementType, len: usize) -> Result<()> { + let len = i32::try_from(len)?; if len < 15 { self.write_byte(((len as u8) << 4) | element_type as u8) } else { self.write_byte(0xf0u8 | element_type as u8)?; - self.write_vlq(len as _) + self.write_vlq(len as u64) } } @@ -922,7 +928,7 @@ impl ThriftCompactOutputProtocol { /// Serialize a slice of `u8`s. This will encode a length, and then write the bytes without /// further encoding. pub(crate) fn write_bytes(&mut self, val: &[u8]) -> Result<()> { - self.write_vlq(val.len() as u64)?; + self.write_vlq(i32::try_from(val.len())? as u64)?; self.writer.write_all(val)?; Ok(()) } @@ -1298,6 +1304,45 @@ pub(crate) mod tests { } } + #[test] + fn test_skip_vlq_stops_at_tenth_byte() { + // `last` is the tenth varint byte, after nine continuation bytes. + // 0 and 1 terminate valid values; 2 exceeds the single remaining bit in u64; + // 0x80 requests an invalid eleventh byte. The following 42 must remain unread. + for last in [0, 1, 2, 0x80] { + let mut bytes = vec![0x80; 9]; + bytes.extend_from_slice(&[last, 42]); + let mut slice = ThriftSliceInputProtocol::new(&bytes); + let mut remaining = bytes.as_slice(); + let stream_result = ThriftReadInputProtocol::new(&mut remaining).skip_vlq(); + let slice_result = slice.skip_vlq(); + if last <= 1 { + slice_result.unwrap(); + stream_result.unwrap(); + } else { + assert!(matches!( + slice_result, + Err(ThriftProtocolError::IntegerOverflow) + )); + assert!(matches!( + stream_result, + Err(ThriftProtocolError::IntegerOverflow) + )); + } + assert_eq!(slice.as_slice(), &[42]); + assert_eq!(remaining, &[42]); + } + + let bytes = [0x80; 9]; + let mut slice = ThriftSliceInputProtocol::new(&bytes); + assert!(matches!(slice.skip_vlq(), Err(ThriftProtocolError::Eof))); + let mut stream = ThriftReadInputProtocol::new(bytes.as_slice()); + assert!(matches!( + stream.skip_vlq(), + Err(ThriftProtocolError::IO(e)) if e.kind() == ErrorKind::UnexpectedEof + )); + } + #[test] fn test_binary_length_bound() { let oversized = [0x80, 0x80, 0x80, 0x80, 8]; // i32::MAX + 1 @@ -1448,6 +1493,38 @@ pub(crate) mod tests { } } + #[test] + fn test_unknown_integer_before_known_field() { + // `last` is the tenth varint byte, after nine continuation bytes. + // 0 terminates a padded zero; 2 exceeds the single remaining bit in u64; + // 0x80 sets the continuation bit, requesting an invalid eleventh byte. + for last in [0, 2, 0x80] { + // Unknown I64 field 1, then known field 2 containing i32(42). + let mut bytes = vec![0x16]; + bytes.extend_from_slice(&[0x80; 9]); + bytes.extend_from_slice(&[last, 0x15, 84, 0]); + let mut slice = ThriftSliceInputProtocol::new(&bytes); + let mut stream = ThriftReadInputProtocol::new(bytes.as_slice()); + if last == 0 { + assert_eq!(KnownField::read_thrift(&mut slice).unwrap().value, 42); + assert_eq!(KnownField::read_thrift(&mut stream).unwrap().value, 42); + } else { + assert!( + KnownField::read_thrift(&mut slice) + .unwrap_err() + .to_string() + .contains("integer overflow") + ); + assert!( + KnownField::read_thrift(&mut stream) + .unwrap_err() + .to_string() + .contains("integer overflow") + ); + } + } + } + #[test] fn test_skip_boolean_collections_before_known_field() { for collection_type in [FieldType::List, FieldType::Set] { From ddf18eb609c55ad9da0015232d00d5825dd66dcb Mon Sep 17 00:00:00 2001 From: seidl Date: Thu, 3 Dec 2026 13:03:38 -0800 Subject: [PATCH 2/6] add some test comments --- parquet/src/parquet_thrift.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/parquet/src/parquet_thrift.rs b/parquet/src/parquet_thrift.rs index a90a458a331e..735d6f452c60 100644 --- a/parquet/src/parquet_thrift.rs +++ b/parquet/src/parquet_thrift.rs @@ -1329,10 +1329,12 @@ pub(crate) mod tests { Err(ThriftProtocolError::IntegerOverflow) )); } + // we only get here if we've properly skipped the preceding int64 assert_eq!(slice.as_slice(), &[42]); assert_eq!(remaining, &[42]); } + // also test truncated VLQ int (all continuation bits set) let bytes = [0x80; 9]; let mut slice = ThriftSliceInputProtocol::new(&bytes); assert!(matches!(slice.skip_vlq(), Err(ThriftProtocolError::Eof))); @@ -1500,6 +1502,8 @@ pub(crate) mod tests { // 0x80 sets the continuation bit, requesting an invalid eleventh byte. for last in [0, 2, 0x80] { // Unknown I64 field 1, then known field 2 containing i32(42). + // The decoder will call skip_vlq for field 1, and then read field 2. + // A malformed field 1 should now error. let mut bytes = vec![0x16]; bytes.extend_from_slice(&[0x80; 9]); bytes.extend_from_slice(&[last, 0x15, 84, 0]); From c7e890bccfbd2c2dae9e3c0af3d1f25f12a046f3 Mon Sep 17 00:00:00 2001 From: seidl Date: Thu, 3 Dec 2026 16:08:41 -0800 Subject: [PATCH 3/6] remove inline from skip_vlq --- parquet/src/parquet_thrift.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/parquet/src/parquet_thrift.rs b/parquet/src/parquet_thrift.rs index 735d6f452c60..3427b52c7b3c 100644 --- a/parquet/src/parquet_thrift.rs +++ b/parquet/src/parquet_thrift.rs @@ -458,7 +458,6 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { fn read_double(&mut self) -> ThriftProtocolResult; /// Skip a ULEB128 encoded varint. - #[inline(always)] fn skip_vlq(&mut self) -> ThriftProtocolResult<()> { for index in 0..10 { let byte = self.read_byte()?; From 6c33ca1d2de4faf33047a10459b12e99eb53ce0d Mon Sep 17 00:00:00 2001 From: seidl Date: Fri, 4 Dec 2026 08:21:59 -0800 Subject: [PATCH 4/6] Revert "remove inline from skip_vlq" This reverts commit c7e890bccfbd2c2dae9e3c0af3d1f25f12a046f3. --- parquet/src/parquet_thrift.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/parquet/src/parquet_thrift.rs b/parquet/src/parquet_thrift.rs index 3427b52c7b3c..735d6f452c60 100644 --- a/parquet/src/parquet_thrift.rs +++ b/parquet/src/parquet_thrift.rs @@ -458,6 +458,7 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { fn read_double(&mut self) -> ThriftProtocolResult; /// Skip a ULEB128 encoded varint. + #[inline(always)] fn skip_vlq(&mut self) -> ThriftProtocolResult<()> { for index in 0..10 { let byte = self.read_byte()?; From 7bb32df746f88fa5a670a2702638065526c11f62 Mon Sep 17 00:00:00 2001 From: seidl Date: Fri, 4 Dec 2026 08:23:00 -0800 Subject: [PATCH 5/6] try inlining read_vlq --- parquet/src/parquet_thrift.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/parquet/src/parquet_thrift.rs b/parquet/src/parquet_thrift.rs index 735d6f452c60..476acca0ac5d 100644 --- a/parquet/src/parquet_thrift.rs +++ b/parquet/src/parquet_thrift.rs @@ -293,6 +293,7 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { } /// Read a ULEB128 encoded unsigned varint from the input. + #[inline(always)] fn read_vlq(&mut self) -> ThriftProtocolResult { // try the happy path first let byte = self.read_byte()?; From 198149f4ead15c6643081d8d702bacf11bfd9cdc Mon Sep 17 00:00:00 2001 From: Ed Seidl Date: Fri, 4 Dec 2026 09:28:32 -0800 Subject: [PATCH 6/6] revert changes in 7bb32df746f88fa5a670a2702638065526c11f62 --- parquet/src/parquet_thrift.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/parquet/src/parquet_thrift.rs b/parquet/src/parquet_thrift.rs index 476acca0ac5d..735d6f452c60 100644 --- a/parquet/src/parquet_thrift.rs +++ b/parquet/src/parquet_thrift.rs @@ -293,7 +293,6 @@ pub(crate) trait ThriftCompactInputProtocol<'a> { } /// Read a ULEB128 encoded unsigned varint from the input. - #[inline(always)] fn read_vlq(&mut self) -> ThriftProtocolResult { // try the happy path first let byte = self.read_byte()?;