From 76f67c0f10366b6c7376e7c1daa21fd6d1efbd36 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 06:03:09 -0500 Subject: [PATCH 1/3] Portable mode: create the write probe exclusively, never through an existing entry (#10963) The portable data folder's writability check wrote a fixed, PID-named file with std::fs::write, which follows an existing symlink and truncates its target. Create the probe with create_new, try another name on a collision, and remove only a probe this call created. --- apps/photocraft/src/app_dirs.rs | 27 ++++++++++++++++++++++----- apps/photocraft/src/app_dirs/tests.rs | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/apps/photocraft/src/app_dirs.rs b/apps/photocraft/src/app_dirs.rs index 4405608842..4d5330d58c 100644 --- a/apps/photocraft/src/app_dirs.rs +++ b/apps/photocraft/src/app_dirs.rs @@ -491,14 +491,31 @@ pub fn portable_marker(exe_dir: &Path) -> Option { PORTABLE_MARKERS.iter().map(|m| exe_dir.join(m)).find(|p| p.is_file()) } +/// Probe names tried before giving up on a portable data folder. +const WRITE_PROBE_ATTEMPTS: u32 = 64; + /// Create `dir` and prove a file can be written in it (a folder can exist yet be read-only). +/// +/// The probe is created exclusively (`create_new`), so an entry already at its name (a link +/// someone else placed there, say) is never opened, written through or removed: the next name +/// is tried instead. Only a probe this call created is removed. fn ensure_writable(dir: &Path) -> std::io::Result<()> { std::fs::create_dir_all(dir)?; - let probe = dir.join(format!(".write-test-{}", std::process::id())); - std::fs::write(&probe, b"")?; - // A leftover probe is harmless; don't fail portable mode over it. - let _ = std::fs::remove_file(&probe); - Ok(()) + let pid = std::process::id(); + for attempt in 0..WRITE_PROBE_ATTEMPTS { + let probe = dir.join(format!(".write-test-{pid}-{attempt}")); + match std::fs::OpenOptions::new().write(true).create_new(true).open(&probe) { + Ok(file) => { + drop(file); + // A leftover probe is harmless; don't fail portable mode over it. + let _ = std::fs::remove_file(&probe); + return Ok(()); + } + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + } + } + Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "every write-test name is already taken")) } #[cfg(test)] diff --git a/apps/photocraft/src/app_dirs/tests.rs b/apps/photocraft/src/app_dirs/tests.rs index c54299fb58..b7f750ba37 100644 --- a/apps/photocraft/src/app_dirs/tests.rs +++ b/apps/photocraft/src/app_dirs/tests.rs @@ -443,3 +443,28 @@ fn missing_exe_dir_or_env_never_panics() { let d = resolve(env_of(&[("PHOTOCRAFT_CONFIG_DIR", &empty), ("HOME", &empty), ("APPDATA", &empty)]), None); assert_eq!(d.dir, None, "empty variables are ignored"); } + +#[cfg(unix)] +#[test] +fn the_write_probe_never_follows_an_entry_already_at_its_name() { + let root = temp("probe-link"); + let data = root.join("data"); + std::fs::create_dir_all(&data).unwrap(); + let victim = root.join("victim.txt"); + std::fs::write(&victim, b"keep me").unwrap(); + // Every name the probe tries, except the last, is a link to a file outside the folder. + let pid = std::process::id(); + for attempt in 0..WRITE_PROBE_ATTEMPTS - 1 { + std::os::unix::fs::symlink(&victim, data.join(format!(".write-test-{pid}-{attempt}"))).unwrap(); + } + ensure_writable(&data).unwrap(); + assert_eq!(std::fs::read(&victim).unwrap(), b"keep me"); + // The links are left alone, and the probe that was created is gone. + assert!(std::fs::symlink_metadata(data.join(format!(".write-test-{pid}-0"))).unwrap().file_type().is_symlink()); + assert!(!data.join(format!(".write-test-{pid}-{}", WRITE_PROBE_ATTEMPTS - 1)).exists()); + // With every name taken the folder is reported unwritable, still without touching the file. + std::os::unix::fs::symlink(&victim, data.join(format!(".write-test-{pid}-{}", WRITE_PROBE_ATTEMPTS - 1))).unwrap(); + assert!(ensure_writable(&data).is_err()); + assert_eq!(std::fs::read(&victim).unwrap(), b"keep me"); + let _ = std::fs::remove_dir_all(&root); +} From 4a65ae3fb0b8a1518f2e1da7051a6285f82af4e5 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 06:03:16 -0500 Subject: [PATCH 2/3] codecs: refuse EXR preview attributes larger than their value (#10982) The exr crate sizes a preview attribute's pixel buffer from its declared dimensions (up to 4 MiB at once, zero-filled) before it finds the attribute too short, and in the non-pedantic mode every reader here uses, it then skips the attribute and continues. A header with thousands of tiny malformed previews therefore cost thousands of 4 MiB allocations before any size check ran. check_previews walks the raw headers and refuses a preview whose declared width x height x 4 exceeds its value size. It runs before every header read: read_meta, read_layer, the Cryptomatte listing and decode_deep_exr. --- crates/codecs/src/codecs/exr.rs | 97 +++++++++++++++++++++ crates/codecs/src/codecs/exr_cryptomatte.rs | 1 + crates/codecs/src/lib.rs | 1 + 3 files changed, 99 insertions(+) diff --git a/crates/codecs/src/codecs/exr.rs b/crates/codecs/src/codecs/exr.rs index e179b13044..ec0956a4ec 100644 --- a/crates/codecs/src/codecs/exr.rs +++ b/crates/codecs/src/codecs/exr.rs @@ -33,9 +33,63 @@ fn map_exr(e: exr::error::Error) -> CodecError { } } +/// Refuses headers whose `preview` attributes declare more pixels than their value holds. +/// +/// The `exr` crate sizes a preview's pixel buffer from its declared dimensions (up to 4 MiB at +/// once, zero-filled) before it finds the attribute too short, and in the non-pedantic mode +/// every reader here uses it then skips the attribute and goes on: thousands of tiny malformed +/// previews would each cost that allocation. This walks the raw headers (name, type, size, +/// value per attribute; an empty name ends a header, an empty header ends a multi-part list) +/// and checks each preview against its own size, so the crate only reads consistent ones. +/// Anything else odd is left to the crate to report. +pub(crate) fn check_previews(b: &[u8]) -> Result<(), CodecError> { + if b.get(..4) != Some(&[0x76, 0x2f, 0x31, 0x01][..]) { + return Ok(()); + } + let Some(flags) = b.get(4..8).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes) else { return Ok(()) }; + let multipart = flags & 0x1000 != 0; + let cstr = |at: usize| -> Option<(&[u8], usize)> { + let rest = b.get(at..)?; + let n = rest.iter().position(|&c| c == 0)?; + Some((rest.get(..n)?, at + n + 1)) + }; + let u32_at = |at: usize| b.get(at..at.checked_add(4)?).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes); + let mut at = 8; + loop { + // One header's attributes. + loop { + let Some((name, next)) = cstr(at) else { return Ok(()) }; + at = next; + if name.is_empty() { + break; + } + let Some((kind, next)) = cstr(at) else { return Ok(()) }; + let Some(size) = u32_at(next).and_then(|n| i32::try_from(n).ok()).and_then(|n| usize::try_from(n).ok()) else { return Ok(()) }; + let value = next + 4; + let end = match value.checked_add(size) { + Some(end) if end <= b.len() => end, + _ => return Ok(()), + }; + if kind == b"preview" { + let declared = + u32_at(value).zip(u32_at(value + 4)).and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)).filter(|_| size >= 8); + if declared.is_none_or(|n| n > (size - 8) as u64) { + return Err(err("a preview attribute declares more pixels than it holds")); + } + } + at = end; + } + // A multi-part list ends with an empty header (a lone NUL). + if !multipart || b.get(at) == Some(&0) { + return Ok(()); + } + } +} + /// Read the headers without the `exr` crate's validation (it rejects deep parts outright), /// after checking every part's declared size against the limits, before any pixel allocation. pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result { + check_previews(bytes)?; let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(err)?; // Flat decoding reads every part at once, so the parts' sizes are also capped together. let mut total: u64 = 0; @@ -130,6 +184,7 @@ impl<'s, C: ReadChannels<'s>> ReadChannels<'s> for OnlyPart { /// Decode one part's flat layer via the `exr` crate (it handles every flat compression). /// Other parts are not decompressed, so an AOV file costs one part per request, not the file. pub(super) fn read_layer(bytes: &[u8], index: usize) -> Result>, CodecError> { + check_previews(bytes)?; let only = OnlyPart { inner: read().no_deep_data().largest_resolution_level().all_channels(), index, seen: std::cell::Cell::new(0) }; only.first_valid_layer().all_attributes().from_buffered(Cursor::new(bytes)).map(|image| image.layer_data).map_err(map_exr) } @@ -334,3 +389,45 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result Vec { + let mut attrs = LayerAttributes::named("rgb"); + attrs.preview = Some(Preview { size: exr::math::Vec2(2, 1), pixel_data: vec![7; 8] }); + let list = ["R", "G", "B"].into_iter().map(|n| AnyChannel::new(n, FlatSamples::F32(vec![0.5; 4]))).collect(); + let layer = Layer::new( + (2, 2), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (2, 2))), vec![layer]); + let mut out = Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).unwrap(); + out.into_inner() + } + + #[test] + fn a_preview_declaring_more_pixels_than_it_holds_is_refused_before_the_crate_reads_it() { + let good = with_preview(); + check_previews(&good).unwrap(); + assert_eq!(decode(&good, &Limits::default()).unwrap().dimensions(), (2, 2)); + // Patch the preview to 1024x1024 (4 MiB) while its value still holds 8 bytes. + let tag = b"preview\0preview\0"; + let at = good.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len() + 4; + let mut bad = good.clone(); + bad[at..at + 4].copy_from_slice(&1024u32.to_le_bytes()); + bad[at + 4..at + 8].copy_from_slice(&1024u32.to_le_bytes()); + for r in [check_previews(&bad), decode(&bad, &Limits::default()).map(|_| ()), read_layer(&bad, 0).map(|_| ())] { + assert!(matches!(&r, Err(CodecError::Malformed { message, .. }) if message.contains("preview")), "{r:?}"); + } + // A header cut off mid-attribute is left to the crate. + check_previews(&good[..at]).unwrap(); + } +} diff --git a/crates/codecs/src/codecs/exr_cryptomatte.rs b/crates/codecs/src/codecs/exr_cryptomatte.rs index 370c7c7e56..62fd2e1e0d 100644 --- a/crates/codecs/src/codecs/exr_cryptomatte.rs +++ b/crates/codecs/src/codecs/exr_cryptomatte.rs @@ -148,6 +148,7 @@ fn layer_streams(header: &exr::meta::header::Header, name: &str) -> Vec /// Lists every Cryptomatte layer of the file from the headers alone: what a Cryptomatte /// picker offers. The embedded manifest (capped, JSON) is parsed into (ID, name) pairs. pub(crate) fn layers(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + super::exr::check_previews(bytes)?; let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(|e| CodecError::malformed(F, e))?; // Manifests can be large; bound the embedded text before parsing anything. const MANIFEST_CAP: usize = 16 << 20; diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs index 7d71f7ef4e..8588019d79 100644 --- a/crates/codecs/src/lib.rs +++ b/crates/codecs/src/lib.rs @@ -275,6 +275,7 @@ pub fn decode_exr_channels(bytes: &[u8], part: usize, names: &[&str], opts: &Dec /// [`decode_as_with`] instead flattens a deep file into a normal [`Image`] with a /// [`DecodeWarning::DeepFlattened`]. `Err(Unsupported)` when the file has no deep part. pub fn decode_deep_exr(bytes: &[u8], limits: &Limits) -> Result { + codecs::exr::check_previews(bytes)?; let mut cur = std::io::Cursor::new(bytes); let meta = ::exr::meta::MetaData::read_from_buffered(&mut cur, false).map_err(|e| CodecError::malformed(Format::OpenExr, e))?; codecs::deep_exr::decode_deep(&meta, bytes, limits) From 9779daf3577eb1d831fa078e3094a7c70b7b23cf Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 06:03:23 -0500 Subject: [PATCH 3/3] security(affinity): charge allocation records, empty subpaths, paint lists, tile indexes and shape lookups (#10966) * security(affinity): charge allocation records, empty subpaths, paint lists, tile indexes and shape lookups Several readers still did work per occurrence of a shared object that no document-wide budget counted: - Archive::open walked every allocation table's records, and overlapping tables could reparse one long run thousands of times. Records are now charged against MAX_TABLE_RECORDS before each table is walked. - curves() charged a subpath's records but not the subpath, so a shared curve full of empty subpaths was walked for free. Each subpath now costs one record. - Colours and gradient positions decoded their whole float record (up to 64 KiB) to read two to five components; only those are decoded now. - Paint selection collected whole BFFl/LIFl/LILn lists without charging their slots; the lists are charged and walked once without collecting. - Dash patterns were copied per occurrence with no length limit; they are capped at 64 entries and charged against the document budget. - The raster tile index skipped null Idx slots uncharged; every slot walked is charged now. - shapes::last rescanned all fields per lookup and CTyp/ShCR were copied whole; the field index records last occurrences too, and only four corners are read. * test(affinity): cover the allocation record budget across chained tables * test(affinity): directory records count against the allocation record budget --- crates/affinity/src/container.rs | 9 +++ crates/affinity/src/geometry.rs | 4 +- crates/affinity/src/paint.rs | 113 +++++++++++++++++++++++-------- crates/affinity/src/raster.rs | 19 ++++-- crates/affinity/src/shapes.rs | 15 ++-- crates/affinity/src/stream.rs | 46 +++++++++---- crates/affinity/src/tests.rs | 62 +++++++++++++++++ 7 files changed, 216 insertions(+), 52 deletions(-) diff --git a/crates/affinity/src/container.rs b/crates/affinity/src/container.rs index 96fdbd706c..569ab65c03 100644 --- a/crates/affinity/src/container.rs +++ b/crates/affinity/src/container.rs @@ -20,6 +20,11 @@ const MAX_ENTRIES: usize = 1 << 22; /// Bytes of entry names the allocation tables may name. A name is retained twice (in the entry /// and as its map key), and tables can point many names at the same bytes. const MAX_NAME_BYTES: usize = 32 << 20; +/// File and directory records parsed across every allocation table. Each table walks its own +/// records, and tables may start inside one another and share a long suffix, so the per-table +/// counts do not bound the work: a few thousand overlapping tables could reparse one large run +/// of records each. Real files hold a few hundred saves of a few thousand entries. +const MAX_TABLE_RECORDS: usize = 1 << 25; /// zstd window ceiling: real documents use a few MiB; this stops a header from reserving gigabytes. const MAX_ZSTD_WINDOW: u64 = 64 << 20; @@ -175,6 +180,7 @@ impl<'a> Archive<'a> { let mut seen = HashSet::new(); let mut next = header.fat_offset; let mut tables = 0usize; + let mut records_left = MAX_TABLE_RECORDS; while next != 0 { if !seen.insert(next) { return Err(Error::Malformed("allocation tables form a cycle")); @@ -199,6 +205,9 @@ impl<'a> Archive<'a> { c.take(8)?; let dirs = c.u16()?; c.take(1)?; + // Charge the declared records before walking them. + let declared = usize::try_from(files).unwrap_or(usize::MAX).saturating_add(usize::from(dirs)); + records_left = records_left.checked_sub(declared).ok_or(Error::Limit("too many allocation records"))?; for _ in 0..files { let id = c.u32()?; let flag = c.u8()?; diff --git a/crates/affinity/src/geometry.rs b/crates/affinity/src/geometry.rs index 0c1a871b27..f834e50598 100644 --- a/crates/affinity/src/geometry.rs +++ b/crates/affinity/src/geometry.rs @@ -177,7 +177,9 @@ pub(crate) fn curves(r: &mut Reader, crvs: ObjId) -> Option { r.warn("curves with more than four million nodes"); return None; } - if let Err(reason) = r.geometry_budget.records(len) { + // Charge the subpath itself as well as its records: an empty subpath adds no records + // or points, but a shared curve full of them is still walked for every occurrence. + if let Err(reason) = r.geometry_budget.records(len.saturating_add(1)) { r.warn(reason); return None; } diff --git a/crates/affinity/src/paint.rs b/crates/affinity/src/paint.rs index 904f642123..2f1fb86e2d 100644 --- a/crates/affinity/src/paint.rs +++ b/crates/affinity/src/paint.rs @@ -125,18 +125,24 @@ pub struct Stroke { /// Gradient stops per gradient. const MAX_STOPS: usize = 1024; +/// Dash and gap lengths per stroke. Affinity's editor offers three dash/gap pairs; a longer +/// pattern would be copied into every shape that shares its style. +const MAX_DASHES: usize = 64; -fn floats(v: &Value) -> Option> { +/// The first `n` components of a float vector or packed `f32` record. Colours and gradient +/// positions use a few leading components; a shared colour with a huge record is decoded for +/// every occurrence, so the rest is neither converted nor copied. +fn floats(v: &Value, n: usize) -> Option> { match v { - Value::Floats(f) => Some(f.clone()), - Value::Bytes(b) if b.len() % 4 == 0 => Some(f32_samples(b)), + Value::Floats(f) => Some(f.iter().take(n).copied().collect()), + Value::Bytes(b) if b.len() % 4 == 0 => Some(f32_samples(b, n)), _ => None, } } -/// Little-endian `f32` samples widened to `f64`. -fn f32_samples(b: &[u8]) -> Vec { - b.as_chunks::<4>().0.iter().map(|c| f64::from(f32::from_le_bytes(*c))).collect() +/// Up to `n` little-endian `f32` samples widened to `f64`. +fn f32_samples(b: &[u8], n: usize) -> Vec { + b.as_chunks::<4>().0.iter().take(n).map(|c| f64::from(f32::from_le_bytes(*c))).collect() } /// Spot colours defined through spot colours. A spot colour (`Pant`) is read through its RGB @@ -154,26 +160,27 @@ pub(crate) fn color(r: &mut Reader, id: ObjId) -> Option { fn color_at(r: &mut Reader, id: ObjId, depth: usize) -> Option { let s = r.s; let class = s.class(id)?; - let col = || -> Option> { - let v = floats(s.field(id, b"_col")?)?; + // `n` is how many components the model reads; only those are decoded and checked. + let col = |n: usize| -> Option> { + let v = floats(s.field(id, b"_col")?, n)?; v.iter().all(|f| f.is_finite()).then_some(v) }; Some(match &class.0.to_be_bytes() { b"RGBA" => { - let v = col()?; + let v = col(4)?; Color::Rgb { r: *v.first()?, g: *v.get(1)?, b: *v.get(2)?, a: v.get(3)?.clamp(0.0, 1.0) } } b"HSLA" => { - let v = col()?; + let v = col(4)?; let (r2, g, b) = hsl(*v.first()?, *v.get(1)?, *v.get(2)?); Color::Rgb { r: r2, g, b, a: v.get(3)?.clamp(0.0, 1.0) } } b"CMYK" => { - let v = col()?; + let v = col(5)?; Color::Cmyk { c: *v.first()?, m: *v.get(1)?, y: *v.get(2)?, k: *v.get(3)?, a: v.get(4)?.clamp(0.0, 1.0) } } b"GRAY" => { - let v = col()?; + let v = col(2)?; Color::Gray { v: *v.first()?, a: v.get(1)?.clamp(0.0, 1.0) } } b"LABA" => { @@ -245,9 +252,10 @@ pub(crate) fn descriptor(r: &mut Reader, id: ObjId, local: Affine) -> Option> = match s.field(grad, b"Posn") { - Some(Value::Array(a)) => a.iter().filter_map(floats).take(MAX_STOPS).collect(), + // A position is (offset, bias): later components are not decoded. + Some(Value::Array(a)) => a.iter().filter_map(|v| floats(v, 2)).take(MAX_STOPS).collect(), Some(Value::Records { size, data }) if *size % 4 == 0 && *size > 0 => { - data.chunks_exact(usize::from(*size)).map(f32_samples).take(MAX_STOPS).collect() + data.chunks_exact(usize::from(*size)).map(|b| f32_samples(b, 2)).take(MAX_STOPS).collect() } _ => Vec::new(), }; @@ -274,23 +282,52 @@ pub(crate) fn descriptor(r: &mut Reader, id: ObjId, local: Affine) -> Option Option { - let items = r.s.objs(id, list); - let i = r.s.int(id, index).and_then(|i| usize::try_from(i).ok()).unwrap_or(0); - items.get(i).or_else(|| items.first()).copied() +/// The active entry of a paint list (the `index`-th object, else the first) and how many +/// objects the list holds. Every raw slot, null ones included, is charged before the list is +/// walked: a shared layer's lists are read again for each occurrence. A list beyond the +/// document's work budget reads as empty. +fn current(r: &mut Reader, id: ObjId, list: &[u8; 4], index: &[u8; 4]) -> (Option, usize) { + let s = r.s; + let Some(Value::Array(items)) = s.field(id, list) else { return (None, 0) }; + if r.charge_slots(items.len()).is_err() { + r.warn("fill and stroke lists beyond the document work budget (left empty)"); + return (None, 0); + } + let want = s.int(id, index).and_then(|i| usize::try_from(i).ok()).unwrap_or(0); + let (mut first, mut chosen, mut count) = (None, None, 0usize); + for o in items.iter().filter_map(|v| if let Value::Obj(Some(o)) = v { Some(*o) } else { None }) { + first = first.or(Some(o)); + if count == want { + chosen = Some(o); + } + count += 1; + } + (chosen.or(first), count) } /// The fills of `list` (artboard backgrounds use `BFFl`). pub(crate) fn fills(r: &mut Reader, id: ObjId, local: Affine, list: &[u8; 4]) -> Vec { - let d = current(r, id, list, b"BFCr").or_else(|| r.s.obj(id, b"BFil")); - d.and_then(|d| descriptor(r, d, local)).filter(|p| *p != Paint::None).into_iter().collect() + fills_counted(r, id, local, list).0 +} + +/// [`fills`], and how many entries `list` holds. +fn fills_counted(r: &mut Reader, id: ObjId, local: Affine, list: &[u8; 4]) -> (Vec, usize) { + let (d, count) = current(r, id, list, b"BFCr"); + let d = d.or_else(|| r.s.obj(id, b"BFil")); + (d.and_then(|d| descriptor(r, d, local)).filter(|p| *p != Paint::None).into_iter().collect(), count) } /// A node's fill and stroke. `local` maps the node's space to document pixels. pub(crate) fn node_paint(r: &mut Reader, id: ObjId, local: Affine) -> (Vec, Vec) { let s = r.s; - let fills = fills(r, id, local, b"BFFl"); - if r.s.objs(id, b"BFFl").len() > 1 || r.s.objs(id, b"LIFl").len() > 1 { + let (fills, fill_count) = fills_counted(r, id, local, b"BFFl"); + let (pen, style, pen_count) = if s.field(id, b"LIFl").is_some() { + let (pen, pens) = current(r, id, b"LIFl", b"LICr"); + (pen, current(r, id, b"LILn", b"LICr").0, pens) + } else { + (s.obj(id, b"PFil"), s.obj(id, b"LSty"), 0) + }; + if fill_count > 1 || pen_count > 1 { r.warn("several fills or strokes on one object (only the active one is imported)"); } if s.f64(id, b"FOpc").is_some_and(|o| o < 0.999) { @@ -299,11 +336,6 @@ pub(crate) fn node_paint(r: &mut Reader, id: ObjId, local: Affine) -> (Vec stroke(r, pen, style, local, behind), @@ -341,6 +373,17 @@ fn stroke(r: &mut Reader, pen: ObjId, ldsc: ObjId, local: Affine, behind: bool) _ => Align::Center, }; let dash = match s.field(lsty, b"Patn") { + // Each occurrence of a shared style keeps its own scaled copy: refuse long patterns + // before scanning or copying them. + Some(Value::Array(a)) if a.len() > MAX_DASHES => { + r.warn("dash patterns longer than 64 entries (imported as solid strokes)"); + None + } + // The copies are retained across the document: charge them like other shared slots. + Some(Value::Array(a)) if r.charge_slots(a.len()).is_err() => { + r.warn("dash patterns beyond the document work budget (imported as solid strokes)"); + None + } Some(Value::Array(a)) if data.get(10) == Some(&2) || !a.is_empty() => { let mut v: Vec = a.iter().filter_map(|x| if let Value::Float(f) = x { Some(*f) } else { None }).collect(); while v.len() >= 2 && v.iter().rev().take(2).all(|x| *x == 0.0) { @@ -356,3 +399,19 @@ fn stroke(r: &mut Reader, pen: ObjId, ldsc: ObjId, local: Affine, behind: bool) } Some(Stroke { paint, width, cap, join, miter_limit, align, dash, behind }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_the_components_a_schema_reads_are_decoded() { + // A 64 KiB record decodes to the four components asked for, not sixteen thousand. + let record = Value::Bytes(1.0f32.to_le_bytes().repeat(16_383)); + assert_eq!(floats(&record, 4), Some(vec![1.0; 4])); + assert_eq!(floats(&Value::Floats(vec![0.5; 6]), 2), Some(vec![0.5; 2])); + // Fewer components than asked for are returned as they are, for the caller to refuse. + assert_eq!(floats(&Value::Floats(vec![0.5; 3]), 5), Some(vec![0.5; 3])); + assert_eq!(floats(&Value::Bytes(vec![0; 3]), 4), None); + } +} diff --git a/crates/affinity/src/raster.rs b/crates/affinity/src/raster.rs index 226c66a459..b0a8c2daab 100644 --- a/crates/affinity/src/raster.rs +++ b/crates/affinity/src/raster.rs @@ -187,12 +187,12 @@ fn plane( Some(Value::Array(a)) => a, _ => &[], }; - let mut tiles = match s.field(bitmap, &tag(b"Idx", c)) { - Some(Value::Array(a)) => a.as_slice(), + // Tile references in order. Null slots are skipped, but every slot walked (null or not) is + // charged below, so a shared bitmap's long null prefix is not rescanned for free. + let mut tiles: &[Value] = match s.field(bitmap, &tag(b"Idx", c)) { + Some(Value::Array(a)) => a, _ => &[], - } - .iter() - .filter_map(|v| if let Value::Obj(Some(o)) = v { Some(*o) } else { None }); + }; // The scan below runs for every occurrence of a shared bitmap, whatever it produces. r.charge_slots(states.len().min(tw.saturating_mul(th)))?; let row = (w as usize) * bps; @@ -206,7 +206,14 @@ fn plane( 0 | 1 => None, 2 => Some(0xFF), 4 => { - let Some(blck) = tiles.next() else { return Err(Error::Malformed("missing pixel tile")) }; + let Some(at) = tiles.iter().position(|v| matches!(v, Value::Obj(Some(_)))) else { + r.charge_slots(tiles.len())?; + return Err(Error::Malformed("missing pixel tile")); + }; + r.charge_slots(at + 1)?; + let Some(Value::Obj(Some(blck))) = tiles.get(at) else { return Err(Error::Malformed("missing pixel tile")) }; + let blck = *blck; + tiles = tiles.get(at + 1..).unwrap_or_default(); let (ox, oy) = (tx * TILE, ty * TILE); if ox >= bx1 || ox + TILE <= bx0 || oy >= py1 || oy + TILE <= py0 { continue; diff --git a/crates/affinity/src/shapes.rs b/crates/affinity/src/shapes.rs index c7751da885..a79f7eac03 100644 --- a/crates/affinity/src/shapes.rs +++ b/crates/affinity/src/shapes.rs @@ -12,9 +12,10 @@ const KAPPA: f64 = 0.552_284_749_830_793_4; const MAX_SIDES: i64 = 10_000; /// The last value of a tag: a shape changed from one kind to another keeps a stale first block. +/// Indexed: a shared shape is read again for every occurrence, and rescanning a large field +/// list each time multiplied into work far beyond the file's size. fn last<'a>(r: &'a Reader, id: ObjId, tag: &[u8; 4]) -> Option<&'a Value> { - let t = Tag::of(tag); - r.s.object(id)?.fields.iter().rev().find(|(k, _)| *k == t).map(|(_, v)| v) + r.s.object(id)?.get_last(Tag::of(tag)) } fn num(r: &Reader, id: ObjId, tag: &[u8; 4]) -> Option { @@ -51,12 +52,14 @@ pub(crate) fn shape(r: &mut Reader, shape: ObjId, b: [f64; 4], world: Affine) -> let class = r.s.class(shape)?; let sub = match &class.0.to_be_bytes() { b"ShNR" | b"ShRR" => { - let types = match last(r, shape, b"CTyp") { - Some(Value::Array(a)) => a.iter().map(|t| if let Value::Enum { id, .. } = t { Some(*id) } else { None }).collect(), + // Only the four corners are read: a longer array is not scanned or copied, which + // would otherwise repeat for every occurrence of a shared shape. + let types: Vec> = match last(r, shape, b"CTyp") { + Some(Value::Array(a)) => a.iter().take(4).map(|t| if let Value::Enum { id, .. } = t { Some(*id) } else { None }).collect(), _ => Vec::new(), }; - let radii = match last(r, shape, b"ShCR") { - Some(Value::Floats(v)) => v.iter().map(|f| if f.is_finite() && *f > 0.0 { *f } else { 0.0 }).collect(), + let radii: Vec = match last(r, shape, b"ShCR") { + Some(Value::Floats(v)) => v.iter().take(4).map(|f| if f.is_finite() && *f > 0.0 { *f } else { 0.0 }).collect(), _ => Vec::new(), }; // Corners follow the first one (top left) unless they were unlocked; then each has diff --git a/crates/affinity/src/stream.rs b/crates/affinity/src/stream.rs index 348fc6540a..74f240b873 100644 --- a/crates/affinity/src/stream.rs +++ b/crates/affinity/src/stream.rs @@ -90,26 +90,34 @@ pub struct Object { pub chain: Vec, /// Fields in stream order; positional fields have tag 0. pub fields: Vec<(Tag, Value)>, - /// Index for objects with more than [`SCAN`] fields: (tag, index of its first occurrence) - /// sorted by tag, so `get` is a binary search. A shared object can be referenced by many - /// sibling layers, and each reference used to rescan all of its fields; indexing once keeps - /// repeated lookups from multiplying into quadratic work. Smaller objects have no index - /// (and no per-object hash map to build): scanning a handful of fields is cheaper. - index: Vec<(Tag, u32)>, + /// Index for objects with more than [`SCAN`] fields: (tag, index of its first occurrence, + /// index of its last occurrence) sorted by tag, so `get` and `get_last` are a binary search. + /// A shared object can be referenced by many sibling layers, and each reference used to + /// rescan all of its fields; indexing once keeps repeated lookups from multiplying into + /// quadratic work. Smaller objects have no index (and no per-object hash map to build): + /// scanning a handful of fields is cheaper. + index: Vec<(Tag, u32, u32)>, } /// Objects with at most this many fields are scanned instead of indexed. const SCAN: usize = 12; -fn build_index(fields: &[(Tag, Value)]) -> Vec<(Tag, u32)> { +fn build_index(fields: &[(Tag, Value)]) -> Vec<(Tag, u32, u32)> { if fields.len() <= SCAN { return Vec::new(); } - // Positional fields share tag 0 and are not looked up by tag. Sorting by (tag, index) and - // keeping the first of each tag preserves first-match semantics. - let mut index: Vec<(Tag, u32)> = fields.iter().zip(0u32..).filter(|((t, _), _)| t.0 != 0).map(|((t, _), i)| (*t, i)).collect(); + // Positional fields share tag 0 and are not looked up by tag. Sorting by (tag, index) puts + // each tag's occurrences together in stream order: the first and last of each run keep + // first-match semantics for `get` and last-match semantics for `get_last`. + let mut index: Vec<(Tag, u32, u32)> = fields.iter().zip(0u32..).filter(|((t, _), _)| t.0 != 0).map(|((t, _), i)| (*t, i, i)).collect(); index.sort_unstable(); - index.dedup_by_key(|e| e.0); + index.dedup_by(|later, kept| { + let same = later.0 == kept.0; + if same { + kept.2 = later.2; + } + same + }); index } @@ -127,7 +135,21 @@ impl Object { return self.fields.iter().find(|(t, _)| *t == tag).map(|(_, v)| v); } let at = self.index.binary_search_by_key(&tag, |e| e.0).ok()?; - self.index.get(at).and_then(|&(_, i)| self.fields.get(i as usize)).map(|(_, v)| v) + self.index.get(at).and_then(|&(_, i, _)| self.fields.get(i as usize)).map(|(_, v)| v) + } + + /// The last field with `tag`, for objects that may repeat a tag (a shape changed from one + /// kind to another keeps a stale first block). Indexed like [`Object::get`], so repeated + /// lookups on a large shared object do not rescan its fields. + pub fn get_last(&self, tag: Tag) -> Option<&Value> { + if tag.0 == 0 { + return None; + } + if self.fields.len() <= SCAN { + return self.fields.iter().rev().find(|(t, _)| *t == tag).map(|(_, v)| v); + } + let at = self.index.binary_search_by_key(&tag, |e| e.0).ok()?; + self.index.get(at).and_then(|&(_, _, i)| self.fields.get(i as usize)).map(|(_, v)| v) } /// Replaces the fields (the root object is filled in after it is pushed) and rebuilds the diff --git a/crates/affinity/src/tests.rs b/crates/affinity/src/tests.rs index 021b532529..a8a1187da5 100644 --- a/crates/affinity/src/tests.rs +++ b/crates/affinity/src/tests.rs @@ -297,6 +297,42 @@ mod archive { assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Malformed("allocation tables form a cycle")); } + #[test] + fn allocation_records_are_charged_before_they_are_walked() { + let d = doc(); + let mut file = synth::container(&[("doc.dat", &d, Method::Stored)], None); + let fat = u64::from_le_bytes(file[16..24].try_into().unwrap()) as usize; + // The table's file count sits after its tag, next, date and 24 reserved bytes. + let files = fat + 4 + 8 + 8 + 24; + file[files..files + 4].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Limit("too many allocation records")); + // A count within the budget still reaches the records (and here fails on missing data). + file[files..files + 4].copy_from_slice(&1_000_000u32.to_le_bytes()); + assert!(matches!(Archive::open(&file, Limits::default()), Err(Error::Malformed(_)))); + } + + #[test] + fn allocation_records_are_charged_across_every_table() { + let d = doc(); + let mut file = synth::container(&[("doc.dat", &d, Method::Stored), ("other.dat", &d, Method::Stored)], None); + let fat = u64::from_le_bytes(file[16..24].try_into().unwrap()) as usize; + // Chain a copy of the (valid, two-record) table after it; the copy is the last table. + let second = file.len(); + let table = file[fat..].to_vec(); + file.extend(&table); + file[fat + 4..fat + 12].copy_from_slice(&(second as u64).to_le_bytes()); + let files = second + 4 + 8 + 8 + 24; + // Each count alone is within the archive budget of 2^25; together they exceed it by one. + file[files..files + 4].copy_from_slice(&((1u32 << 25) - 1).to_le_bytes()); + assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Limit("too many allocation records")); + // Exactly the budget is allowed: the second table is walked (and fails on missing data). + file[files..files + 4].copy_from_slice(&((1u32 << 25) - 2).to_le_bytes()); + assert!(matches!(Archive::open(&file, Limits::default()), Err(Error::Malformed(_)))); + // Directory records count too: one more (after the file count and 8 bytes) is over. + file[files + 12..files + 14].copy_from_slice(&1u16.to_le_bytes()); + assert_eq!(Archive::open(&file, Limits::default()).unwrap_err(), Error::Limit("too many allocation records")); + } + #[test] fn hostile_streams_fail_cleanly() { let deep = (0..2000).fold(Vec::new(), |inner, _| vec![(tag(b"Chld"), F::Obj(tag(b"Grup"), inner))]); @@ -416,6 +452,20 @@ mod shapes { rx.recv_timeout(std::time::Duration::from_secs(20)).unwrap_or_else(|_| panic!("reading the document did not finish")).unwrap() } + #[test] + fn empty_subpaths_of_a_shared_curve_are_charged_for_every_occurrence() { + // One curve of 100k empty subpaths, placed 100 times: no records and no points, but + // ten million subpaths walked unless each one is charged. + let mut data = vec![F::U8(0), F::U32(100_000)]; + for _ in 0..100_000 { + data.extend([F::Bool(false), F::Records(18, Vec::new())]); + } + let curve = F::Def(5, vec![tag(b"PCrv")], vec![(tag(b"Crvs"), F::Obj(tag(b"PCvD"), vec![(tag(b"Data"), F::Pos(data))]))]); + let group = F::Def(6, vec![tag(b"Grup")], vec![(tag(b"Chld"), F::Shared((0..100).map(|_| F::Ref(5)).collect()))]); + let d = read(vec![curve, group], 6); + assert!(d.warnings.iter().any(|w| w.contains("curve geometry budget exhausted")), "{:?}", d.warnings); + } + fn path(document: &Document) -> &Path { match &document.spreads[0].nodes[0].kind { Kind::Shape { path, .. } => path, @@ -786,6 +836,18 @@ mod stream_storage { } } + #[test] + fn last_lookup_keeps_last_match_for_every_object_size() { + for n in 0..60u32 { + let fields: Vec<(Tag, Value)> = (0..n).map(|i| (if i % 5 == 0 { Tag(0) } else { Tag(100 + i % 7) }, Value::UInt(u64::from(i)))).collect(); + let object = Object::new(Tag(1), Kind::Inline, vec![Tag(1)], fields.clone()); + for t in 0..120 { + let expected = if t == 0 { None } else { fields.iter().rev().find(|(k, _)| *k == Tag(t)).map(|(_, v)| v) }; + assert_eq!(object.get_last(Tag(t)), expected, "{n} fields, tag {t}"); + } + } + } + #[test] fn a_fixed_size_array_is_one_buffer_and_one_value_per_record() { let records: Vec = (0..18 * 5).map(|i| i as u8).collect();