Skip to content

Commit a0b76ad

Browse files
committed
fix: fix potential OOM involving snappy data, found by fuzzer
1 parent 59070f6 commit a0b76ad

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/protocol/record.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ where
630630
// There are "normal" compression libs, and there is Java
631631
// See https://github.com/edenhill/librdkafka/blob/2b76b65212e5efda213961d5f84e565038036270/src/rdkafka_msgset_reader.c#L307-L318
632632
let output = if input.starts_with(JAVA_MAGIC) {
633-
let mut cursor = Cursor::new(&input[JAVA_MAGIC.len()..]);
633+
let cursor_content = &input[JAVA_MAGIC.len()..];
634+
let mut cursor = Cursor::new(cursor_content);
634635

635636
let mut buf_version = [0u8; 4];
636637
cursor.read_exact(&mut buf_version)?;
@@ -653,6 +654,11 @@ where
653654
let mut buf_chunk_length = [0u8; 4];
654655
cursor.read_exact(&mut buf_chunk_length)?;
655656
let chunk_length = u32::from_be_bytes(buf_chunk_length) as usize;
657+
let bytes_left = cursor_content.len() - (cursor.position() as usize);
658+
if chunk_length > bytes_left {
659+
// do NOT try to allocate massive buffer for `chunk_data` but instead fail early
660+
return Err(ReadError::Malformed(format!("Java-specific Snappy-compressed data has illegal chunk length, got {chunk_length} bytes but only {bytes_left} bytes are left.").into()));
661+
}
656662

657663
let mut chunk_data = vec![0u8; chunk_length];
658664
cursor.read_exact(&mut chunk_data)?;

0 commit comments

Comments
 (0)