| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use ec_core::Error; |
| 9 | use ec_image::{Limits, bmp, jpeg, png}; |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | const VP8_IDCT_OVERFLOW: &[u8] = &[ |
| 15 | 0x52, 0x49, 0x46, 0x46, 0x82, 0x02, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, |
| 16 | 0x56, 0x50, 0x38, 0x20, 0x76, 0x02, 0x00, 0x00, 0xb0, 0x10, 0x00, 0x9d, |
| 17 | 0x01, 0x2a, 0x3d, 0x00, 0x2f, 0x00, 0x3e, 0x6d, 0x2a, 0x91, 0x45, 0xa4, |
| 18 | 0x22, 0x21, 0x96, 0xab, 0x37, 0x6c, 0x40, 0x06, 0xc4, 0xb6, 0x00, 0x4e, |
| 19 | 0x9c, 0xa0, 0xbf, 0x00, 0xfc, 0x00, 0xe5, 0xf0, 0x85, 0x1d, 0x27, 0xcc, |
| 20 | 0xb2, 0xa2, 0xfd, 0x9f, 0x66, 0x38, 0x81, 0x58, 0x23, 0xd5, 0x16, 0xda, |
| 21 | 0xde, 0x79, 0x0f, 0x49, 0x9e, 0xa8, 0x7d, 0xe9, 0x9f, 0xf4, 0x76, 0x8b, |
| 22 | 0x2d, 0x67, 0xf0, 0xa0, 0x61, 0x22, 0x90, 0xc3, 0xc2, 0x43, 0x4f, 0x9b, |
| 23 | 0x15, 0xd8, 0xdd, 0x22, 0xaa, 0x29, 0x99, 0x51, 0x08, 0xcd, 0x64, 0x5d, |
| 24 | 0x48, 0x10, 0x4f, 0x68, 0x8b, 0x8e, 0x98, 0x52, 0x30, 0x86, 0xba, 0x0d, |
| 25 | 0xd3, 0x38, 0x09, 0x70, 0x10, 0x95, 0x7c, 0x44, 0x3c, 0xb7, 0x09, 0x7c, |
| 26 | 0x00, 0xa6, 0x44, 0xdb, 0x21, 0x37, 0xa1, 0xbe, 0xd4, 0x35, 0x23, 0x9e, |
| 27 | 0xe1, 0xb9, 0x25, 0x6b, 0xc4, 0x23, 0x8c, 0x27, 0x26, 0x7d, 0x27, 0x1d, |
| 28 | 0xf6, 0xbb, 0xef, 0x8c, 0x00, 0xfe, 0xeb, 0x63, 0x21, 0xb4, 0x19, 0xe2, |
| 29 | 0x01, 0x60, 0x3f, 0xc9, 0x61, 0x4e, 0x79, 0xeb, 0x5e, 0x30, 0x1f, 0x7d, |
| 30 | 0x3d, 0x61, 0xb7, 0x6a, 0x3f, 0xd5, 0xbf, 0x0c, 0x93, 0xbf, 0x6f, 0x64, |
| 31 | 0x9d, 0xd4, 0xec, 0xd3, 0x91, 0x24, 0x32, 0x58, 0x0a, 0xd8, 0x31, 0x4e, |
| 32 | 0x6b, 0x71, 0x92, 0x36, 0x62, 0xfc, 0xe4, 0x4c, 0x40, 0x2a, 0xd7, 0x74, |
| 33 | 0x3d, 0x7a, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x19, 0x2a, 0x96, 0xc7, |
| 34 | 0xe5, 0x8a, 0xa5, 0x95, 0x9e, 0x24, 0x4a, 0x1b, 0x29, 0x60, 0x1b, 0x30, |
| 35 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 36 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 37 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 38 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 39 | 0xff, 0xff, 0x59, 0x25, 0x44, 0xfa, 0x70, 0x3d, 0xfa, 0x59, 0xe4, 0x2a, |
| 40 | 0x8d, 0x7b, 0xbf, 0xd7, 0xfe, 0xb1, 0xbf, 0x9c, 0xfb, 0x92, 0xec, 0x30, |
| 41 | 0x40, 0x87, 0x00, 0x81, 0x8d, 0xe8, 0xd4, 0xaf, 0xff, 0x43, 0x27, 0x37, |
| 42 | 0x9a, 0x0f, 0x30, 0x1b, 0x2d, 0x89, 0x22, 0xd9, 0xf0, 0x36, 0x01, 0x42, |
| 43 | 0x9c, 0xb3, 0x5b, 0xe3, 0x0e, 0xe5, 0x5d, 0x54, 0xaa, 0xae, 0xe4, 0x20, |
| 44 | 0x1c, 0x05, 0xab, 0xf9, 0xb9, 0x7a, 0xf8, 0x0f, 0x53, 0x21, 0x63, 0xf0, |
| 45 | 0x19, 0x2a, 0x96, 0xc7, 0xe5, 0x8a, 0xa5, 0x95, 0x9e, 0x24, 0x4a, 0x1b, |
| 46 | 0x29, 0x60, 0x1b, 0x30, 0xa5, 0x22, 0x76, 0x7b, 0xd4, 0x95, 0x41, 0x71, |
| 47 | 0x89, 0x89, 0x36, 0x6d, 0xf8, 0xd0, 0xde, 0xb0, 0x98, 0x5d, 0x80, 0xfa, |
| 48 | 0x80, 0x38, 0xf2, 0xb7, 0x1d, 0x65, 0xd7, 0x52, 0xd0, 0xed, 0xb3, 0xf6, |
| 49 | 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, |
| 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x90, 0x00, 0x55, 0xc0, 0xe4, 0x80, |
| 51 | 0xfa, 0x80, 0x38, 0xf2, 0xb7, 0x1d, 0x65, 0xd7, 0x52, 0xd0, 0xed, 0xb3, |
| 52 | 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, |
| 53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x90, 0x00, 0xe5, 0x76, 0x58, |
| 54 | 0xa5, 0x22, 0x76, 0x7b, 0xd4, 0x95, 0x41, 0x71, 0x89, 0x89, 0x36, 0x6d, |
| 55 | 0xf8, 0xd0, 0xde, 0xb0, 0x98, 0x5d, 0x63, 0xc4, 0x87, 0x67, 0xc5, 0x24, |
| 56 | 0x6b, 0x4e, 0xc4, 0x63, 0x9c, 0xad, 0x37, 0x36, 0xb0, 0xd1, 0xbd, 0x09, |
| 57 | 0x76, 0xee, 0x51, 0x9b, 0xe5, 0x66, 0xce, 0x02, 0x00, 0x00, 0x00, 0x00, |
| 58 | 0x00, 0x00, 0x00, 0x55, 0xc0, 0xe4, 0x80, 0xfa, 0x80, 0x38, 0xf2, 0xb7, |
| 59 | 0x1d, 0x65, 0xd7, 0x52, 0xd0, 0xed, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, |
| 60 | 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 61 | 0x00, 0x55, 0x90, 0x00, 0xc0, 0xe4, 0x80, 0xfa, 0x80, 0x38, 0xf2, 0xb7, |
| 62 | 0x1d, 0x65, 0xd7, 0x52, 0xd0, 0xed, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, |
| 63 | 0x37, 0x05, 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 64 | 0x00, 0x55, 0x90, 0x00, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, |
| 65 | 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, |
| 66 | 0x90, 0x00, 0xc0, 0xe4, 0x80, 0xfa, 0x80, 0x38, 0xf2, 0xb7, 0x1d, 0x65, |
| 67 | 0xd7, 0x52, 0xd0, 0xed, 0xb3, 0xf6, 0xa8, 0x0d, 0x0a, 0xdd, 0x37, 0x05, |
| 68 | 0x1b, 0xd9, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, |
| 69 | 0x90, 0x00, |
| 70 | ]; |
| 71 | |
| 72 | |
| 73 | const JPEG_SCAN_WITHOUT_COMPONENTS: &[u8] = &[ |
| 74 | 0x42, 0x4d, 0x9b, 0x04, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x36, 0x00, |
| 75 | 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x30, 0x00, |
| 76 | 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x65, 0x04, |
| 77 | 0x00, 0x00, 0x13, 0x0b, 0x00, 0x00, 0x13, 0x0b, 0x00, 0x1e, 0x1e, 0x1e, |
| 78 | 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0xff, 0xc0, 0x00, 0x11, |
| 79 | 0x08, 0x00, 0x30, 0x00, 0x40, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01, |
| 80 | 0x03, 0x11, 0x01, 0x00, 0xff, 0xda, 0x00, 0x0b, 0x00, 0x00, 0x13, 0x0b, |
| 81 | 0x00, 0x00, 0x00, 0x00, 0xff, 0xd9, |
| 82 | ]; |
| 83 | |
| 84 | #[test] |
| 85 | fn vp8_idct_overflow_does_not_panic_under_any_entry_point() { |
| 86 | let limits = Limits { |
| 87 | max_pixels: 4096 * 4096, |
| 88 | max_alloc: 1 << 26, |
| 89 | max_total_alloc: 1 << 27, |
| 90 | }; |
| 91 | let _ = ec_image::decode(VP8_IDCT_OVERFLOW); |
| 92 | let _ = ec_image::decode_with_limits(VP8_IDCT_OVERFLOW, limits); |
| 93 | let _ = ec_image::decode_animation(VP8_IDCT_OVERFLOW); |
| 94 | let _ = ec_image::webp::decode(VP8_IDCT_OVERFLOW, limits); |
| 95 | let _ = ec_image::webp::decode_frames(VP8_IDCT_OVERFLOW, limits); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn a_jpeg_scan_naming_no_components_is_refused_not_indexed() { |
| 100 | let err = bmp::decode(JPEG_SCAN_WITHOUT_COMPONENTS, Limits::default()) |
| 101 | .expect_err("a scan with no components has nothing to decode"); |
| 102 | assert!(matches!(err, Error::Corrupt { .. }), "{err}"); |
| 103 | let _ = jpeg::decode(JPEG_SCAN_WITHOUT_COMPONENTS, Limits::default()); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | fn png_with(width: u32, height: u32, colour: u8, idat: &[u8]) -> Vec<u8> { |
| 108 | let mut out = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]; |
| 109 | let mut ihdr = Vec::new(); |
| 110 | ihdr.extend_from_slice(&width.to_be_bytes()); |
| 111 | ihdr.extend_from_slice(&height.to_be_bytes()); |
| 112 | ihdr.extend_from_slice(&[8, colour, 0, 0, 0]); |
| 113 | for (kind, body) in [ |
| 114 | (b"IHDR", ihdr.as_slice()), |
| 115 | (b"IDAT", idat), |
| 116 | (b"IEND", &[][..]), |
| 117 | ] { |
| 118 | out.extend_from_slice(&(body.len() as u32).to_be_bytes()); |
| 119 | let start = out.len(); |
| 120 | out.extend_from_slice(kind); |
| 121 | out.extend_from_slice(body); |
| 122 | let crc = crc32(&out[start..]); |
| 123 | out.extend_from_slice(&crc.to_be_bytes()); |
| 124 | } |
| 125 | out |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn a_png_expansion_buffer_is_budgeted_before_it_is_sized() { |
| 130 | let _heavy = heavy(); |
| 131 | |
| 132 | |
| 133 | let png = png_with(1000, 1000, 2, &[0x78, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff]); |
| 134 | let limits = Limits { |
| 135 | max_alloc: 4_000_000, |
| 136 | ..Limits::default() |
| 137 | }; |
| 138 | let err = png::decode(&png, limits).expect_err("the expansion is past the cap"); |
| 139 | assert!(format!("{err}").contains("PNG pixels"), "{err}"); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn a_bmp_pixel_buffer_is_budgeted_against_the_caller_limit() { |
| 144 | let _heavy = heavy(); |
| 145 | |
| 146 | |
| 147 | let mut bmp = Vec::new(); |
| 148 | bmp.extend_from_slice(b"BM"); |
| 149 | bmp.extend_from_slice(&54u32.to_le_bytes()); |
| 150 | bmp.extend_from_slice(&[0; 4]); |
| 151 | bmp.extend_from_slice(&54u32.to_le_bytes()); |
| 152 | bmp.extend_from_slice(&40u32.to_le_bytes()); |
| 153 | bmp.extend_from_slice(&1000i32.to_le_bytes()); |
| 154 | bmp.extend_from_slice(&1000i32.to_le_bytes()); |
| 155 | bmp.extend_from_slice(&1u16.to_le_bytes()); |
| 156 | bmp.extend_from_slice(&24u16.to_le_bytes()); |
| 157 | bmp.extend_from_slice(&[0; 24]); |
| 158 | let limits = Limits { |
| 159 | max_alloc: 2_000_000, |
| 160 | ..Limits::default() |
| 161 | }; |
| 162 | let err = bmp::decode(&bmp, limits).expect_err("the pixels are past the cap"); |
| 163 | assert!(format!("{err}").contains("1000x1000 image"), "{err}"); |
| 164 | } |
| 165 | |
| 166 | #[test] |
| 167 | fn jpeg_coefficients_are_budgeted_across_components() { |
| 168 | let _heavy = heavy(); |
| 169 | |
| 170 | |
| 171 | let mut jpeg = vec![0xff, 0xd8, 0xff, 0xc0, 0x00, 0x11, 8]; |
| 172 | jpeg.extend_from_slice(&8192u16.to_be_bytes()); |
| 173 | jpeg.extend_from_slice(&8192u16.to_be_bytes()); |
| 174 | jpeg.push(3); |
| 175 | for id in 1..=3u8 { |
| 176 | jpeg.extend_from_slice(&[id, 0x11, 0]); |
| 177 | } |
| 178 | let limits = Limits { |
| 179 | max_total_alloc: 256 << 20, |
| 180 | ..Limits::default() |
| 181 | }; |
| 182 | let err = jpeg::decode(&jpeg, limits).expect_err("three planes are past the total"); |
| 183 | assert!(format!("{err}").contains("JPEG coefficients"), "{err}"); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | static HEAVY: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 189 | |
| 190 | fn heavy() -> std::sync::MutexGuard<'static, ()> { |
| 191 | HEAVY.lock().unwrap_or_else(|e| e.into_inner()) |
| 192 | } |
| 193 | |
| 194 | |
| 195 | fn crc32(data: &[u8]) -> u32 { |
| 196 | let mut c = 0xffff_ffffu32; |
| 197 | for &b in data { |
| 198 | c ^= u32::from(b); |
| 199 | for _ in 0..8 { |
| 200 | c = if c & 1 != 0 { |
| 201 | 0xedb8_8320 ^ (c >> 1) |
| 202 | } else { |
| 203 | c >> 1 |
| 204 | }; |
| 205 | } |
| 206 | } |
| 207 | c ^ 0xffff_ffff |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | #[test] |
| 216 | fn large_declared_bmp_pictures_are_refused_before_decoding() { |
| 217 | let inputs: [(&str, &[u8]); 3] = [ |
| 218 | ("bmp rle8 4194368x48 a", include_bytes!("data/fuzz/decode-bmp-rle8-large-timeout-a.bmp")), |
| 219 | ("bmp rle8 4194368x48 b", include_bytes!("data/fuzz/decode-bmp-rle8-large-slow-b.bmp")), |
| 220 | ( |
| 221 | "bmp rle8 64x3014704", |
| 222 | include_bytes!("data/fuzz/decode-bmp-rle8-64x3014704-timeout.bmp"), |
| 223 | ), |
| 224 | ]; |
| 225 | for (name, data) in inputs { |
| 226 | let start = std::time::Instant::now(); |
| 227 | let err = ec_image::decode(data).expect_err(name); |
| 228 | let elapsed = start.elapsed(); |
| 229 | let text = format!("{err}"); |
| 230 | assert!(text.contains("image"), "{name}: {text}"); |
| 231 | assert!(text.contains("536870912"), "{name}: the budget is named: {text}"); |
| 232 | if !cfg!(debug_assertions) { |
| 233 | assert!( |
| 234 | elapsed < std::time::Duration::from_millis(50), |
| 235 | "{name}: refusal took {elapsed:?}" |
| 236 | ); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | #[test] |
| 244 | fn large_declared_jpeg_from_a_small_input_returns() { |
| 245 | let data: &[u8] = include_bytes!("data/fuzz/decode_animation-jpeg-progressive-65519x256.jpg"); |
| 246 | let _ = ec_image::decode_animation(data); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | #[test] |
| 254 | fn a_frame_with_no_scan_is_refused_without_building_the_picture() { |
| 255 | let data: &[u8] = include_bytes!("data/fuzz/decode-jpeg-gray-4353x20297-slow-unit.jpg"); |
| 256 | let err = ec_image::decode(data).expect_err("a frame with no scan has no picture"); |
| 257 | assert!(matches!(err, Error::Corrupt { .. }), "{err}"); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | #[test] |
| 264 | fn a_large_three_component_frame_with_no_scan_is_refused() { |
| 265 | let data: &[u8] = include_bytes!("data/fuzz/decode-jpeg-3comp-167mpx-timeout.jpg"); |
| 266 | let err = ec_image::decode(data).expect_err("a frame with no scan has no picture"); |
| 267 | assert!(matches!(err, Error::Corrupt { .. }), "{err}"); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | #[test] |
| 274 | fn a_scan_that_runs_out_of_data_stops_and_returns() { |
| 275 | let mut jpeg = vec![0xff, 0xd8]; |
| 276 | |
| 277 | jpeg.extend_from_slice(&[0xff, 0xdb, 0x00, 67, 0x00]); |
| 278 | jpeg.extend_from_slice(&[1; 64]); |
| 279 | |
| 280 | jpeg.extend_from_slice(&[0xff, 0xc0, 0x00, 11, 8, 0x4f, 0x49, 0x11, 0x01, 1, 1, 0x11, 0]); |
| 281 | |
| 282 | |
| 283 | for (class_id, symbol) in [(0x00u8, 0u8), (0x10, 0x01)] { |
| 284 | jpeg.extend_from_slice(&[0xff, 0xc4, 0x00, 20, class_id, 1]); |
| 285 | jpeg.extend_from_slice(&[0; 15]); |
| 286 | jpeg.push(symbol); |
| 287 | } |
| 288 | |
| 289 | jpeg.extend_from_slice(&[0xff, 0xda, 0x00, 8, 1, 1, 0x00, 0, 63, 0]); |
| 290 | |
| 291 | jpeg.extend_from_slice(&[0x00, 0x00, 0xff, 0xd9]); |
| 292 | let image = ec_image::decode(&jpeg).expect("a truncated scan decodes what it has"); |
| 293 | assert_eq!((image.width, image.height), (4353, 20297)); |
| 294 | } |