//! Regression tests for inputs the fuzzer found, with the minimised input inlined. //! //! Each test names the target it came from. The assertion is that the call returns, one way or //! another, without panicking: a debug build turns the arithmetic these inputs drive into a panic, //! so `cargo test` (debug) is what catches a regression here. The budget tests below pin the //! refusals that header-sized buffers now go through. use ec_core::Error; use ec_image::{Limits, bmp, jpeg, png}; /// A 650-byte WebP (a VP8 lossy frame behind a VP8 chunk) whose residual coefficients drive /// the second pass of the inverse DCT past i32. Found by `decode_with_limits`; the same bug /// surfaced from `decode`, `decode_animation` and `webp`. const VP8_IDCT_OVERFLOW: &[u8] = &[ 0x52, 0x49, 0x46, 0x46, 0x82, 0x02, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x20, 0x76, 0x02, 0x00, 0x00, 0xb0, 0x10, 0x00, 0x9d, 0x01, 0x2a, 0x3d, 0x00, 0x2f, 0x00, 0x3e, 0x6d, 0x2a, 0x91, 0x45, 0xa4, 0x22, 0x21, 0x96, 0xab, 0x37, 0x6c, 0x40, 0x06, 0xc4, 0xb6, 0x00, 0x4e, 0x9c, 0xa0, 0xbf, 0x00, 0xfc, 0x00, 0xe5, 0xf0, 0x85, 0x1d, 0x27, 0xcc, 0xb2, 0xa2, 0xfd, 0x9f, 0x66, 0x38, 0x81, 0x58, 0x23, 0xd5, 0x16, 0xda, 0xde, 0x79, 0x0f, 0x49, 0x9e, 0xa8, 0x7d, 0xe9, 0x9f, 0xf4, 0x76, 0x8b, 0x2d, 0x67, 0xf0, 0xa0, 0x61, 0x22, 0x90, 0xc3, 0xc2, 0x43, 0x4f, 0x9b, 0x15, 0xd8, 0xdd, 0x22, 0xaa, 0x29, 0x99, 0x51, 0x08, 0xcd, 0x64, 0x5d, 0x48, 0x10, 0x4f, 0x68, 0x8b, 0x8e, 0x98, 0x52, 0x30, 0x86, 0xba, 0x0d, 0xd3, 0x38, 0x09, 0x70, 0x10, 0x95, 0x7c, 0x44, 0x3c, 0xb7, 0x09, 0x7c, 0x00, 0xa6, 0x44, 0xdb, 0x21, 0x37, 0xa1, 0xbe, 0xd4, 0x35, 0x23, 0x9e, 0xe1, 0xb9, 0x25, 0x6b, 0xc4, 0x23, 0x8c, 0x27, 0x26, 0x7d, 0x27, 0x1d, 0xf6, 0xbb, 0xef, 0x8c, 0x00, 0xfe, 0xeb, 0x63, 0x21, 0xb4, 0x19, 0xe2, 0x01, 0x60, 0x3f, 0xc9, 0x61, 0x4e, 0x79, 0xeb, 0x5e, 0x30, 0x1f, 0x7d, 0x3d, 0x61, 0xb7, 0x6a, 0x3f, 0xd5, 0xbf, 0x0c, 0x93, 0xbf, 0x6f, 0x64, 0x9d, 0xd4, 0xec, 0xd3, 0x91, 0x24, 0x32, 0x58, 0x0a, 0xd8, 0x31, 0x4e, 0x6b, 0x71, 0x92, 0x36, 0x62, 0xfc, 0xe4, 0x4c, 0x40, 0x2a, 0xd7, 0x74, 0x3d, 0x7a, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x19, 0x2a, 0x96, 0xc7, 0xe5, 0x8a, 0xa5, 0x95, 0x9e, 0x24, 0x4a, 0x1b, 0x29, 0x60, 0x1b, 0x30, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x59, 0x25, 0x44, 0xfa, 0x70, 0x3d, 0xfa, 0x59, 0xe4, 0x2a, 0x8d, 0x7b, 0xbf, 0xd7, 0xfe, 0xb1, 0xbf, 0x9c, 0xfb, 0x92, 0xec, 0x30, 0x40, 0x87, 0x00, 0x81, 0x8d, 0xe8, 0xd4, 0xaf, 0xff, 0x43, 0x27,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66, 0xce, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xc0, 0xe4, 0x80, 0xfa, 0x80, 0x38, 0xf2, 0xb7, 0x1d, 0x65, 0xd7, 0x52, 0xd0, 0xed, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x90, 0x00, 0xc0, 0xe4, 0x80, 0xfa, 0x80, 0x38, 0xf2, 0xb7, 0x1d, 0x65, 0xd7, 0x52, 0xd0, 0xed, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x90, 0x00, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x90, 0x00, 0xc0, 0xe4, 0x80, 0xfa, 0x80, 0x38, 0xf2, 0xb7, 0x1d, 0x65, 0xd7, 0x52, 0xd0, 0xed, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x90, 0x00, ]; /// A 90-byte BMP whose embedded JPEG has a scan header that names no components. Found by `bmp`. const JPEG_SCAN_WITHOUT_COMPONENTS: &[u8] = &[ 0x42, 0x4d, 0x9b, 0x04, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x65, 0x04, 0x00, 0x00, 0x13, 0x0b, 0x00, 0x00, 0x13, 0x0b, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0x30, 0x00, 0x40, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0x00, 0xff, 0xda, 0x00, 0x0b, 0x00, 0x00, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00, 0xff, 0xd9, ]; #[test] fn vp8_idct_overflow_does_not_panic_under_any_entry_point() { let limits = Limits { max_pixels: 4096 * 4096, max_alloc: 1 << 26, max_total_alloc: 1 << 27, }; let _ = ec_image::decode(VP8_IDCT_OVERFLOW); let _ = ec_image::decode_with_limits(VP8_IDCT_OVERFLOW, limits); let _ = ec_image::decode_animation(VP8_IDCT_OVERFLOW); let _ = ec_image::webp::decode(VP8_IDCT_OVERFLOW, limits); let _ = ec_image::webp::decode_frames(VP8_IDCT_OVERFLOW, limits); } #[test] fn a_jpeg_scan_naming_no_components_is_refused_not_indexed() { let err = bmp::decode(JPEG_SCAN_WITHOUT_COMPONENTS, Limits::default()) .expect_err("a scan with no components has nothing to decode"); assert!(matches!(err, Error::Corrupt { .. }), "{err}"); let _ = jpeg::decode(JPEG_SCAN_WITHOUT_COMPONENTS, Limits::default()); } /// Wrap an IHDR and an IDAT payload into a PNG with every chunk CRC filled in. fn png_with(width: u32, height: u32, colour: u8, idat: &[u8]) -> Vec { let mut out = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]; let mut ihdr = Vec::new(); ihdr.extend_from_slice(&width.to_be_bytes()); ihdr.extend_from_slice(&height.to_be_bytes()); ihdr.extend_from_slice(&[8, colour, 0, 0, 0]); for (kind, body) in [ (b"IHDR", ihdr.as_slice()), (b"IDAT", idat), (b"IEND", &[][..]), ] { out.extend_from_slice(&(body.len() as u32).to_be_bytes()); let start = out.len(); out.extend_from_slice(kind); out.extend_from_slice(body); let crc = crc32(&out[start..]); out.extend_from_slice(&crc.to_be_bytes()); } out } #[test] fn a_png_expansion_buffer_is_budgeted_before_it_is_sized() { let _heavy = heavy(); // 1000x1000 RGB8: the scanlines are 3,001,000 bytes, but the 16-bit expansion is 6,000,000. // A cap that admits the scanlines and not the expansion must refuse by name. let png = png_with(1000, 1000, 2, &[0x78, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff]); let limits = Limits { max_alloc: 4_000_000, ..Limits::default() }; let err = png::decode(&png, limits).expect_err("the expansion is past the cap"); assert!(format!("{err}").contains("PNG pixels"), "{err}"); } #[test] fn a_bmp_pixel_buffer_is_budgeted_against_the_caller_limit() { let _heavy = heavy(); // 1000x1000 24-bit: 3,000,000 bytes of output, past a 2,000,000 byte cap. The body is not // needed; the refusal comes before any pixel is read. let mut bmp = Vec::new(); bmp.extend_from_slice(b"BM"); bmp.extend_from_slice(&54u32.to_le_bytes()); bmp.extend_from_slice(&[0; 4]); bmp.extend_from_slice(&54u32.to_le_bytes()); bmp.extend_from_slice(&40u32.to_le_bytes()); bmp.extend_from_slice(&1000i32.to_le_bytes()); bmp.extend_from_slice(&1000i32.to_le_bytes()); bmp.extend_from_slice(&1u16.to_le_bytes()); bmp.extend_from_slice(&24u16.to_le_bytes()); bmp.extend_from_slice(&[0; 24]); let limits = Limits { max_alloc: 2_000_000, ..Limits::default() }; let err = bmp::decode(&bmp, limits).expect_err("the pixels are past the cap"); assert!(format!("{err}").contains("1000x1000 image"), "{err}"); } #[test] fn jpeg_coefficients_are_budgeted_across_components() { let _heavy = heavy(); // An 8192x8192 4:4:4 frame: each component's coefficients are 128 MiB, so every one passes // the per-buffer cap on its own. Three of them together are past a 256 MiB total. let mut jpeg = vec![0xff, 0xd8, 0xff, 0xc0, 0x00, 0x11, 8]; jpeg.extend_from_slice(&8192u16.to_be_bytes()); jpeg.extend_from_slice(&8192u16.to_be_bytes()); jpeg.push(3); for id in 1..=3u8 { jpeg.extend_from_slice(&[id, 0x11, 0]); } let limits = Limits { max_total_alloc: 256 << 20, ..Limits::default() }; let err = jpeg::decode(&jpeg, limits).expect_err("three planes are past the total"); assert!(format!("{err}").contains("JPEG coefficients"), "{err}"); } /// Serializes the budget-sized tests in this binary; see `heavy` in `fuzz.rs` for why. /// Each test binary is its own process, so this lock is separate from the one there. static HEAVY: std::sync::Mutex<()> = std::sync::Mutex::new(()); fn heavy() -> std::sync::MutexGuard<'static, ()> { HEAVY.lock().unwrap_or_else(|e| e.into_inner()) } /// CRC-32 over a chunk's type and body, as PNG frames it. fn crc32(data: &[u8]) -> u32 { let mut c = 0xffff_ffffu32; for &b in data { c ^= u32::from(b); for _ in 0..8 { c = if c & 1 != 0 { 0xedb8_8320 ^ (c >> 1) } else { c >> 1 }; } } c ^ 0xffff_ffff } /// The three harness timeouts. Each input is a few kilobytes but declares a picture of 2 to 4 /// megapixels or more: RLE8 BMPs at 4194368x48 (two inputs) and 64x3014704 (one input). Their /// decoded output is 576 to 578 MB, past the default 512 MiB budget, so `decode` must refuse /// them from the header alone: an error, and fast (50 ms in release; the debug build's /// arithmetic checks make the timing meaningless, so only the refusal is asserted there). #[test] fn large_declared_bmp_pictures_are_refused_before_decoding() { let inputs: [(&str, &[u8]); 3] = [ ("bmp rle8 4194368x48 a", include_bytes!("data/fuzz/decode-bmp-rle8-large-timeout-a.bmp")), ("bmp rle8 4194368x48 b", include_bytes!("data/fuzz/decode-bmp-rle8-large-slow-b.bmp")), ( "bmp rle8 64x3014704", include_bytes!("data/fuzz/decode-bmp-rle8-64x3014704-timeout.bmp"), ), ]; for (name, data) in inputs { let start = std::time::Instant::now(); let err = ec_image::decode(data).expect_err(name); let elapsed = start.elapsed(); let text = format!("{err}"); assert!(text.contains("image"), "{name}: {text}"); assert!(text.contains("536870912"), "{name}: the budget is named: {text}"); if !cfg!(debug_assertions) { assert!( elapsed < std::time::Duration::from_millis(50), "{name}: refusal took {elapsed:?}" ); } } } /// A progressive JPEG declaring 65519x256 (about 50 MB of RGB, under the budget). It is not a /// refusal case; the assertion is only that the decode returns. #[test] fn large_declared_jpeg_from_a_small_input_returns() { let data: &[u8] = include_bytes!("data/fuzz/decode_animation-jpeg-progressive-65519x256.jpg"); let _ = ec_image::decode_animation(data); } /// The fuzz finding `decode-jpeg-gray-4353x20297-slow-unit.jpg`: a grey JPEG whose frame header /// declares an 88-megapixel picture (under the default budget) and whose file ends right after /// it, with no scan. There is no picture data, so the decode refuses it, and it must do so /// without building the picture (the fuzzer's 10 s budget). #[test] fn a_frame_with_no_scan_is_refused_without_building_the_picture() { let data: &[u8] = include_bytes!("data/fuzz/decode-jpeg-gray-4353x20297-slow-unit.jpg"); let err = ec_image::decode(data).expect_err("a frame with no scan has no picture"); assert!(matches!(err, Error::Corrupt { .. }), "{err}"); } /// The fuzz finding `decode-jpeg-3comp-167mpx-timeout.jpg`: a 65535x2559 three-component frame /// (under the default budget) whose file ends after the frame header. It is refused as having no /// scan, without building its 503 MB of RGB. #[test] fn a_large_three_component_frame_with_no_scan_is_refused() { let data: &[u8] = include_bytes!("data/fuzz/decode-jpeg-3comp-167mpx-timeout.jpg"); let err = ec_image::decode(data).expect_err("a frame with no scan has no picture"); assert!(matches!(err, Error::Corrupt { .. }), "{err}"); } /// A grey JPEG whose scan runs out after two bytes. The one-bit Huffman codes make every zero bit /// read as a coefficient, so without the stop at the end of the data every block would be filled /// with garbage coefficients. The decode must return a picture of the declared size. #[test] fn a_scan_that_runs_out_of_data_stops_and_returns() { let mut jpeg = vec![0xff, 0xd8]; // DQT: table 0, all ones. jpeg.extend_from_slice(&[0xff, 0xdb, 0x00, 67, 0x00]); jpeg.extend_from_slice(&[1; 64]); // SOF0: 8-bit, 4353x20297, one component (id 1, 1x1, quant 0). jpeg.extend_from_slice(&[0xff, 0xc0, 0x00, 11, 8, 0x4f, 0x49, 0x11, 0x01, 1, 1, 0x11, 0]); // DHT: DC table 0 and AC table 0, each one one-bit code ("0") -- size 0 for DC, the // coefficient "run 0, size 1" for AC. for (class_id, symbol) in [(0x00u8, 0u8), (0x10, 0x01)] { jpeg.extend_from_slice(&[0xff, 0xc4, 0x00, 20, class_id, 1]); jpeg.extend_from_slice(&[0; 15]); jpeg.push(symbol); } // SOS: one component, table 0/0, full spectral range, no successive approximation. jpeg.extend_from_slice(&[0xff, 0xda, 0x00, 8, 1, 1, 0x00, 0, 63, 0]); // The scan: two zero bytes, then the end of the image. jpeg.extend_from_slice(&[0x00, 0x00, 0xff, 0xd9]); let image = ec_image::decode(&jpeg).expect("a truncated scan decodes what it has"); assert_eq!((image.width, image.height), (4353, 20297)); }