mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 07:44:40 +00:00
fix(image-codecs): refuse JPEG inks of frames too large for their scan data
zune-jpeg allocates its whole output before reading a scan, so decode_jpeg_inks now applies decode's scan-data guard (one byte for every 2048 pixels, counted on scan data, not APP padding) first. The comparison test's noise is computed in u64, so it doesn't overflow on 32-bit targets.
This commit is contained in:
1 parent
a5a713d2e6
commit
4a70763629
3 files changed
+20
-2
No files matched your search
@@ -198,6 +198,15 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) {
|
||||
data_end_mode(b, false)
|
||||
}
|
||||
|
||||
/// Whether JPEG `b` holds too little scan data for a `w` × `h` frame to be worth decoding: no scan
|
||||
/// data at all, or under one byte for every [`MAX_PIXELS_PER_BYTE`] pixels (counted on the scan
|
||||
/// data the marker walk crosses, so APP padding doesn't count). `zune-jpeg` allocates the whole
|
||||
/// output before it reads a scan, so a few bytes could otherwise ask for a frame of any size.
|
||||
pub(crate) fn scan_too_short(b: &[u8], w: u32, h: u32) -> bool {
|
||||
let (end, scan_bytes) = data_end(b);
|
||||
end == DataEnd::Empty || u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes
|
||||
}
|
||||
|
||||
/// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused.
|
||||
pub(crate) fn reaches_eoi(b: &[u8]) -> bool {
|
||||
data_end_mode(b, true).0 == DataEnd::Complete
|
||||
|
||||
@@ -57,7 +57,9 @@ pub fn jpeg_icc_profile(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
///
|
||||
/// Errors: bytes that aren't a JPEG with readable headers ([`CodecError::InvalidImage`]); one of
|
||||
/// other than four components, or a frame `zune-jpeg` doesn't decode, such as an arithmetic-coded
|
||||
/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; and data that doesn't decode,
|
||||
/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; scan data too short for the
|
||||
/// frame (under a byte for every 2048 pixels, refused before anything is allocated); and data that
|
||||
/// doesn't decode,
|
||||
/// including frames `zune-jpeg` panics on, which are caught.
|
||||
pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
let bad = |m: &str| CodecError::InvalidImage(format!("JPEG inks: {m}"));
|
||||
@@ -70,6 +72,9 @@ pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result<Image, CodecErr
|
||||
let (w, h) = d.dimensions().ok_or_else(|| bad("no size"))?;
|
||||
let (w, h) = (u32::try_from(w).map_err(|_| bad("width"))?, u32::try_from(h).map_err(|_| bad("height"))?);
|
||||
limits.check(w, h, ChannelLayout::Cmyk, SampleType::U8)?;
|
||||
if crate::codecs::jpeg::scan_too_short(bytes, w, h) {
|
||||
return Err(bad("the scan data is too short for the frame it declares"));
|
||||
}
|
||||
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
|
||||
// zune-jpeg 0.5 asserts on some subsampled four-component frames whose first component isn't
|
||||
// the largest (a scratch row too short for its upsampler): that is an error here, not a panic.
|
||||
|
||||
@@ -115,6 +115,10 @@ fn other_jpegs_are_refused() {
|
||||
other[sof + 4] = precision;
|
||||
assert!(decode_jpeg_inks(&other, &Limits::default()).is_err(), "SOF {marker:#x}, {precision} bits");
|
||||
}
|
||||
// A tiny file declaring a large frame is refused before zune-jpeg allocates its output.
|
||||
let mut huge = cmyk.clone();
|
||||
huge[sof + 5..sof + 9].copy_from_slice(&[0x20, 0x00, 0x20, 0x00]);
|
||||
assert!(matches!(decode_jpeg_inks(&huge, &Limits::default()), Err(CodecError::InvalidImage(m)) if m.contains("too short")));
|
||||
let tight = Limits { max_pixels: 100, ..Limits::default() };
|
||||
assert!(matches!(decode_jpeg_inks(&cmyk, &tight), Err(CodecError::LimitExceeded(_))));
|
||||
assert!(matches!(decode_jpeg_inks(b"not a jpeg", &Limits::default()), Err(CodecError::InvalidImage(_))));
|
||||
@@ -157,7 +161,7 @@ fn matches_the_reference_reader() {
|
||||
let (mut seen, mut refused) = (0, 0);
|
||||
for (i, (w, h)) in [(1u16, 1u16), (7, 3), (8, 8), (17, 9), (33, 31), (64, 5), (100, 77)].into_iter().enumerate() {
|
||||
let (wu, n) = (usize::from(w), usize::from(w) * usize::from(h));
|
||||
let noise: Vec<u8> = (0..n * 4).map(|k| ((k * 2_654_435_761 + i * 97) >> 7) as u8).collect();
|
||||
let noise: Vec<u8> = (0..n * 4).map(|k| ((k as u64).wrapping_mul(2_654_435_761).wrapping_add(i as u64 * 97) >> 7) as u8).collect();
|
||||
let ramp: Vec<u8> = (0..n * 4).map(|k| (k / 4 % wu * 255 / wu.max(2)) as u8).collect();
|
||||
for samples in [&noise, &ramp] {
|
||||
for ty in [ColorType::Cmyk, ColorType::CmykAsYcck] {
|
||||
|
||||
Reference in new issue
Block a user