From c9b2348cd7839c7c3bf40dfc2106ccf99063234c Mon Sep 17 00:00:00 2001 From: ByGhost Date: Tue, 13 Oct 2026 16:48:46 +0300 Subject: [PATCH 1/2] codecs/bmp: reject sub-byte indexed color to prevent out-of-bounds slice panic --- src/codecs/bmp/decoder.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/codecs/bmp/decoder.rs b/src/codecs/bmp/decoder.rs index cba61aed68..c068ae60c0 100644 --- a/src/codecs/bmp/decoder.rs +++ b/src/codecs/bmp/decoder.rs @@ -1843,6 +1843,17 @@ impl BmpDecoder { } fn read_palettized_pixel_data(&mut self, buf: &mut [u8]) -> ImageResult<()> { + if self.indexed_color && self.bit_count != 8 { + return Err(ImageError::Unsupported( + UnsupportedError::from_format_and_kind( + ImageFormat::Bmp.into(), + UnsupportedErrorKind::GenericFeature( + "Indexed color decoding is only supported for 8-bit palettized BMP".into(), + ), + ), + )); + } + let num_channels = self.num_channels(); let row_byte_length = ((i32::from(self.bit_count) * self.width + 31) / 32 * 4) as usize; let mut indices = vec![0; row_byte_length]; @@ -3167,4 +3178,25 @@ mod test { "Expected error in strict mode for truncated file" ); } + + #[test] + fn test_indexed_color_sub_byte_rejected() { + use crate::ImageDecoder; + use std::io::Cursor; + + // 1-bit BMP 2x2 image + let data = vec![ + 0x42, 0x4D, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, + 0x28, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, + ]; + + let mut decoder = BmpDecoder::new(Cursor::new(data)).unwrap(); + decoder.set_indexed_color(true); + let mut buf = vec![0u8; 4]; + let res = decoder.read_image(&mut buf); + assert!(res.is_err()); + } } From 600f54f561417299016ffe4f3d6d0f3c88355545 Mon Sep 17 00:00:00 2001 From: ByGhost Date: Tue, 13 Oct 2026 17:36:16 +0300 Subject: [PATCH 2/2] codecs/bmp: reject zero dimensions to prevent unhandled panic --- src/codecs/bmp/decoder.rs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/codecs/bmp/decoder.rs b/src/codecs/bmp/decoder.rs index c068ae60c0..c4f729329a 100644 --- a/src/codecs/bmp/decoder.rs +++ b/src/codecs/bmp/decoder.rs @@ -116,6 +116,13 @@ impl ParsedCoreHeader { let width = i32::from(u16::from_le_bytes(buffer[0..2].try_into().unwrap())); let height = i32::from(u16::from_le_bytes(buffer[2..4].try_into().unwrap())); + if width == 0 { + return Err(DecoderError::NegativeWidth(width).into()); + } + if height == 0 { + return Err(DecoderError::InvalidHeight.into()); + } + let planes = u16::from_le_bytes(buffer[4..6].try_into().unwrap()); if spec_strictness == SpecCompliance::Strict && planes != 1 { return Err(DecoderError::MoreThanOnePlane.into()); @@ -157,17 +164,18 @@ impl ParsedInfoHeader { let width = i32::from_le_bytes(buffer[0..4].try_into().unwrap()); let mut height = i32::from_le_bytes(buffer[4..8].try_into().unwrap()); - // Width cannot be negative - if width < 0 { + if width <= 0 { return Err(DecoderError::NegativeWidth(width).into()); - } else if width > MAX_WIDTH_HEIGHT || height > MAX_WIDTH_HEIGHT { - return Err(DecoderError::ImageTooLarge(width, height).into()); } - if height == i32::MIN { + if height == 0 || height == i32::MIN { return Err(DecoderError::InvalidHeight.into()); } + if width > MAX_WIDTH_HEIGHT || height.abs() > MAX_WIDTH_HEIGHT { + return Err(DecoderError::ImageTooLarge(width, height).into()); + } + // A negative height indicates a top-down DIB let top_down = if height < 0 { height = -height; @@ -3199,4 +3207,21 @@ mod test { let res = decoder.read_image(&mut buf); assert!(res.is_err()); } + + #[test] + fn test_bmp_zero_height_rejected() { + use crate::codecs::bmp::BmpDecoder; + use std::io::Cursor; + + // Minimal 54-byte BMP header with width=10, height=0 + let data = vec![ + 0x42, 0x4D, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, + 0x28, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + + let result = BmpDecoder::new(Cursor::new(data)); + assert!(result.is_err()); + } }