This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 361c0de7042e81bda768a38b700a66e6fe04257b
Author: geoHeil <[email protected]>
AuthorDate: Fri Nov 6 02:56:53 2026 +0000

    fix(arrow-ipc): cap the decompression buffer reserved from the declared 
length (#12077)
    
    # Which issue does this PR close?
    
    - Closes #12030
    
    # Rationale for this change
    
    The reader sizes the decompression buffer from the uncompressed length
    the sender writes in front of each compressed body buffer, and it does
    so before decompressing anything. A peer can declare `i64::MAX` on an
    LZ4 buffer. The reader then asks for that much memory, and the failed
    allocation aborts the process. ZSTD takes the same path for a frame that
    records no content size. Any Flight client or server is affected, and so
    is any other service that decodes IPC from a peer.
    
    ---------
    
    Co-authored-by: geoHeil <[email protected]>
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 arrow-ipc/src/compression.rs | 43 ++++++++++++++++++++++++++++++++++++++++++-
 arrow-ipc/src/reader.rs      |  2 +-
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/arrow-ipc/src/compression.rs b/arrow-ipc/src/compression.rs
index bf084418b9..b441f220d6 100644
--- a/arrow-ipc/src/compression.rs
+++ b/arrow-ipc/src/compression.rs
@@ -296,7 +296,7 @@ fn compress_lz4(_input: &[u8], _output: &mut Vec<u8>) -> 
Result<(), ArrowError>
 #[cfg(feature = "lz4")]
 fn decompress_lz4(input: &[u8], decompressed_size: usize) -> Result<Vec<u8>, 
ArrowError> {
     use std::io::Read;
-    let mut output = Vec::with_capacity(decompressed_size);
+    let mut output = 
Vec::with_capacity(decompressed_size.min(crate::reader::MAX_PREALLOC_BYTES));
     let mut decoder = lz4_flex::frame::FrameDecoder::new(input);
     decoder
         .by_ref()
@@ -356,6 +356,25 @@ fn decompress_zstd(
     decompressed_size: usize,
     context: &mut DecompressionContext,
 ) -> Result<Vec<u8>, ArrowError> {
+    if decompressed_size > crate::reader::MAX_PREALLOC_BYTES {
+        // The bulk decompressor writes into a buffer reserved up front, so 
stream instead.
+        use std::io::Read;
+        let mut output =
+            
Vec::with_capacity(decompressed_size.min(crate::reader::MAX_PREALLOC_BYTES));
+        let mut decoder = zstd::stream::read::Decoder::with_buffer(input)?;
+        decoder
+            .by_ref()
+            .take(decompressed_size as u64)
+            .read_to_end(&mut output)?;
+
+        // Probe without growing `output` to reject data exceeding the 
advertised size.
+        if decoder.read(&mut [0])? != 0 {
+            return Err(ArrowError::IpcError(format!(
+                "ZSTD decompressed buffer exceeds advertised size of 
{decompressed_size}"
+            )));
+        }
+        return Ok(output);
+    }
     let output = context
         .zstd_decompressor()
         .decompress(input, decompressed_size)?;
@@ -450,6 +469,28 @@ mod tests {
         assert_eq!(input_bytes, result.as_slice());
     }
 
+    /// A buffer larger than `MAX_PREALLOC_BYTES` round-trips, and a size its 
data does not back fails.
+    #[test]
+    #[cfg(any(feature = "lz4", feature = "zstd"))]
+    fn test_decompression_beyond_prealloc() {
+        let input = vec![7u8; crate::reader::MAX_PREALLOC_BYTES + 2];
+        for codec in [
+            #[cfg(feature = "lz4")]
+            super::CompressionCodec::Lz4Frame,
+            #[cfg(feature = "zstd")]
+            
super::CompressionCodec::Zstd(super::DEFAULT_ZSTD_COMPRESSION_LEVEL),
+        ] {
+            let mut compressed = Vec::new();
+            codec
+                .compress(&input, &mut compressed, &mut Default::default())
+                .unwrap();
+            let decompress = |len| codec.decompress(&compressed, len, &mut 
Default::default());
+            assert_eq!(decompress(input.len()).unwrap(), input, "{codec:?}");
+            assert!(decompress(input.len() - 1).is_err(), "{codec:?}");
+            assert!(decompress(usize::MAX).is_err(), "{codec:?}");
+        }
+    }
+
     #[test]
     fn test_read_uncompressed_size_rejects_short_prefix() {
         let err = super::read_uncompressed_size(&[1, 2, 3, 4, 5, 6, 7])
diff --git a/arrow-ipc/src/reader.rs b/arrow-ipc/src/reader.rs
index 83b8b5d85e..8564103dfc 100644
--- a/arrow-ipc/src/reader.rs
+++ b/arrow-ipc/src/reader.rs
@@ -1920,7 +1920,7 @@ pub(crate) enum IpcMessage {
 /// start here and the buffer doubles as the data arrives, so a body of `n` 
bytes costs
 /// `log2(n / MAX_PREALLOC_BYTES)` reallocations and the buffer never runs 
more than a factor
 /// of two ahead of the bytes actually received.
-const MAX_PREALLOC_BYTES: usize = 64 * 1024 * 1024;
+pub(crate) const MAX_PREALLOC_BYTES: usize = 64 * 1024 * 1024;
 
 /// Reads exactly `len` bytes of message body, without reserving `len` before 
reading it.
 fn read_body_bounded<R: Read>(reader: &mut R, len: usize) -> 
Result<MutableBuffer, ArrowError> {

Reply via email to