mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 06:51:01 +01:00
hb_closure_context_t bounded GSUB glyph closure only by counting lookup visits (HB_MAX_LOOKUP_VISIT_COUNT per round), and forgets which lookups are done whenever the glyph set grows, so what one visit walks -- through aliased offsets, many times the table size -- was charged to nothing. A LigatureSubst with 9000 ligatureSet offsets aliased onto one 64-component Ligature, re-walked after every one of 1700 Context triggers that each add a glyph, makes hb-subset --unicodes='*' of an 86 KB font spend ~110 s of CPU. A ReverseChain/ChainContextFormat3 with aliased backtrack Coverages is a second way in. Give the context an int64_t budget (HB_BUDGET_LAYOUT_CLOSURE records, in the hb_budget_spend() idiom) spent as each closure() implementation walks its arrays: dispatch() charges a visit its coverage record count; ligature sets / ligatures / components, sequences, alternates, single-subst intersections, rule sets and rules, context Coverages (via a spend_coverages() helper) and the per-rule-set ClassDef walks are each charged. Coverage and ClassDef gain a get_record_count() helper because intersects()/intersect_set() are linear in records, not population. Once the budget is spent, should_visit_lookup()/lookup_limit_exceeded() stop the closure early, exactly as they do at the visit limit. The budget lives in the context, so both public closure entry points (also reached from hb_ot_shape_glyphs_closure() and the subset plan) are covered with no API/ABI change. Both attack shapes drop to ~1 s and the control font's closure output is byte-identical; the heaviest real font in the corpora uses about 1% of the budget. A hostile GSUB now gets a truncated closure, as one hitting the visit limit already does. test_ot_layout_lookups_substitute_closure_budget builds the hostile table in C and drives hb_ot_layout_lookups_substitute_closure() over it. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-J9K92HA9. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>