Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

security(affinity): bound raw child-slot traversal work

Dylan M. Taylor7c07280b660c
diff --git a/crates/affinity/src/access.rs b/crates/affinity/src/access.rs
index 0b60ed7db..732feeef4 100644
--- a/crates/affinity/src/access.rs
+++ b/crates/affinity/src/access.rs
@@ -20,6 +20,15 @@ impl Stream {
self.class(id) == Some(Tag::of(class))
}
+ /// The raw slot count of an object's array field (including null slots), for charging
+ /// traversal work before nulls are filtered out. `None` when the field is not an array.
+ pub fn array_len(&self, id: ObjId, tag: &[u8; 4]) -> Option<usize> {
+ match self.field(id, tag)? {
+ Value::Array(a) => Some(a.len()),
+ _ => None,
+ }
+ }
+
/// A child object (shared, linked or inline); `None` for null or missing.
pub fn obj(&self, id: ObjId, tag: &[u8; 4]) -> Option<ObjId> {
match self.field(id, tag)? {
diff --git a/crates/affinity/src/model.rs b/crates/affinity/src/model.rs
index 7c0507241..f77c970ae 100644
--- a/crates/affinity/src/model.rs
+++ b/crates/affinity/src/model.rs
@@ -283,6 +283,12 @@ const MAX_TREE_DEPTH: usize = 128;
/// Total layer nodes imported.
const MAX_NODES: usize = 500_000;
+/// Total raw `Chld` slots inspected across the whole traversal, including null slots and slots
+/// reached through repeated references to a shared group. The node limit only counts produced
+/// nodes, so a shared group holding a large null-filled child array could still be rescanned
+/// many times; this bounds that work. Generous relative to MAX_NODES for real documents.
+const MAX_CHILD_SLOTS: usize = 8_000_000;
+
/// Read a document: archive, `doc.dat` and the model.
pub fn read(bytes: &[u8], limits: Limits) -> Result<Document, Error> {
let mut archive = Archive::open(bytes, limits)?;
@@ -297,6 +303,7 @@ pub(crate) struct Reader<'s, 'a, 'b> {
pub(crate) dpi: f64,
warnings: BTreeMap<String, usize>,
nodes: usize,
+ child_slots: usize,
active: HashSet<ObjId>,
pub(crate) geometry_budget: crate::geometry::Budget,
/// Decoded pixels still available ([`Limits::max_pixel_bytes`] less what the layers read so
@@ -312,6 +319,7 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> {
dpi: 72.0,
warnings: BTreeMap::new(),
nodes: 0,
+ child_slots: 0,
active: HashSet::new(),
geometry_budget: crate::geometry::Budget::default(),
pixels_left: limits.max_pixel_bytes,
@@ -383,6 +391,13 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> {
let s = self.s;
let mut out = Vec::new();
let mut mask = None;
+ // Charge every raw child slot (null slots included) against a document-wide budget
+ // before filtering. A shared group with a large null-filled Chld array could otherwise
+ // be rescanned through each reference without the node limit noticing.
+ self.child_slots = self.child_slots.saturating_add(s.array_len(parent, b"Chld").unwrap_or(0));
+ if self.child_slots > MAX_CHILD_SLOTS {
+ return Err(Error::Limit("too many child slots in the layer tree"));
+ }
for c in s.objs(parent, b"Chld") {
if s.is(c, b"MRst") {
// A mask layer inside a group or layer masks that container.