Skip to content

COLRv1 PaintColrLayers DAG with shared layer subtrees causes unbounded CPU cost — RecursionStack only detects same-path cycles, not cross-branch reuse #221

Closed

Description

@scadastrangelove

colr::RecursionStack (a fixed [usize; 64] push/pop stack) correctly prevents a paint offset
from being revisited on its own active call path — but an entry is popped as soon as its
call returns, so the same offset being reachable via a different sibling branch of a DAG
(never actually cycling on any single path) is not caught. PaintColrLayers's handler loop
(for i in 0..layers_count { ...; self.parse_paint(...); }) also discards each recursive call's
result with no ?, so a failed/cyclic branch never short-circuits exploration of the rest —
which is exactly what makes the shared-subtree construction below force real, uncapped work.

A chain of PaintColrLayers (format 1) records where every layer slot at each level points at
one shared next-level record forces branching^depth calls to parse_paint, using only
depth + 1 distinct paint records.

How bad is this, measured against real data

Measured against ttf-parser's own project-authored colr_1.ttf test font: 201 real COLRv1
glyphs paint in 175µs total, worst single real glyph 91.3µs. Against that baseline, the 579-byte
crafted case below (15.98s) is ~175,000× slower than the single slowest glyph in that font —
for a font barely larger (579 B vs the 21.5 KB colr_1.ttf, though the crafted case only needs
one glyph's worth of paint records to reach that cost).

Reproduction (ttf-parser 0.25.1, and current main @ 6e75b3c)

depth branching font size paint_color_glyph time (before fix) paint() calls
10 2 395 B 60–75 µs 1,024
15 2 465 B ~2 ms 32,768
18 3 579 B ~16–20+ s 387,420,489 (= 3^18 exactly)
20 3 615 B did not complete in 20 s (predicted 3^20 ≈ 3.49e9)

Measured as isolated OS processes against current main @ 6e75b3c. Self-contained
reproducer available (builds a minimal COLR v1 + CPAL sfnt byte-by-byte, using
palette_index = 0xFFFF's foreground-color special case in PaintSolid to avoid needing real
palette data; ~180 lines).

Fix

PR incoming: adds a total paint-node-visit budget directly to RecursionStack (100,000,
matching the same reasoning as a companion fix for glyf/gvar's shared-component-subtree issue —
real color-font paint graphs have at most a few hundred nodes). Since every one of the 25
recursive self.parse_paint(...) call sites across the format handlers already threads
recursion_stack: &mut RecursionStack through, this needed no signature changes beyond
RecursionStack itself and its one construction site in Table::paint.


Discovered by the rust-in-peace security pipeline.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions