Files
harfbuzz/test/api/fonts/cff1_subr_loop.otf
Behdad EsfahbodandClaude Opus 4.8 420bb6d0ba [subset/cff] Bound hint-drop/closure walks and share the budget across the whole subset (#6638)
* [subset] Bound the CFF hint-dropping and closure walks

drop_hints_in_str()/drop_hints_in_subr() and the closure walk
(collect_subr_refs_in_str/_in_subr) recursively walk the subroutine
calls recorded for each glyph with no depth limit and no work budget.
The records need not match what the interpreter executed: a string is
parsed on its first run only, global subr records are shared across
Font DICTs whose local subrs differ, and endchar does not end a walk.
So the recorded call graph can nest deeper than kMaxCallLimit or be
cyclic, and a binary-tree subr structure makes the hint-dropping walk
expand exponentially.  A 360-byte CID font crashed the subsetter with
a stack overflow under --no-hinting (or hb_subset_preprocess), and a
~1 KB font ran unboundedly.  PR #6428 rejected stack-supplied call
operands but added no depth, cycle or work limit to these walks, so
these fonts were still affected.

Hold both walks to the interpreter's call-depth limit (kMaxCallLimit),
which stops the cycle and the long chain before the stack runs out, and
charge the hint-dropping walk to the subsetting pass budget introduced
for the shared-subroutine work, so the exponential expansion is capped
too.  A walk that follows the interpreter's path stays within both, so
only fonts whose records diverge from their execution are affected;
over either limit the subset fails, as other CFF planning failures do.

Adds test_subset_cff1_subr_loop, which subsets the 360-byte cycle font
with --no-hinting and only requires that hb_subset_or_fail() returns.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BCC7WHCA.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [subset] Stop CFF subr walks at the call-depth limit instead of failing

The hint-dropping and subr-closure walks enforce kMaxCallLimit
themselves, because the recorded call graph can nest deeper than the
interpreter allows or be cyclic.  Hitting the limit failed the whole
subset, and the hint-dropping walk signalled it by writing -1 into the
shared work budget, conflating "too deep" with "out of budget" and
clobbering the budget for later passes.

Make both walks best-effort at the limit: the closure walk keeps the
subr it reached but does not walk into it, and the hint-dropping walk
does not walk into the subr and keeps the call and its hints.  Only
running out of the work budget fails the subset now, so
drop_hints_param_t no longer needs set_error() and the budget is only
ever touched by hb_budget_spend().  Walks that follow the
interpreter's path never reach the limit, so real fonts are
unaffected; for fonts whose records diverge the output may keep a few
hints or call a subr whose callees were not retained, but no longer
fails.

test_subset_cff1_subr_loop now also asserts that the subset succeeds;
it failed before this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [subset] Share one CFF charstring budget across a whole subset

Each CFF / CFF2 charstring pass of the subsetter (seac closure, CFF2
extents for instancing, CFF2 usage and instancing passes, subroutine
subsetting with hint dropping, and flattening) created its own budget
with CFF::get_subset_budget(), so one subset could spend that budget
several times over, and the two CFF2 instancing passes reset it
between them.

Keep the budget on hb_subset_plan_t instead: cff_budget() sizes it on
first use from the table, as before, and every pass draws on the same
counter, so the total work of one subset is bounded once.  Retries of
a table with a larger serialize buffer draw on it too.  cff1 depend()
is not part of a subset plan and keeps its own budget per call.

Real fonts use about 1.5 operations per table byte per pass, against
a budget of 32 per byte (at least 2^24).  Tested: full meson test and
the subset suite; hb-subset on SourceSerif4Variable-Roman.otf (CFF2),
Roboto-Regular.otf (CFF), NotoSansCJKhk-VF.otf (30 MB CFF2) with
--gids='*' and --unicodes='*', with and without --no-hinting,
--desubroutinize and partial / full instancing, and on every .otf in
/Library/Fonts with --gids='*', all succeed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [subset] Detect CFF charstring budget exhaustion at zero, not below

The shared CFF charstring budget is only ever spent through the
cs_interpreter_t interpret() cap, which clamps each glyph's operator
count to the remaining budget and charges back exactly what it
consumed.  By construction it never overshoots, so an exhausted
budget lands at exactly 0 rather than going negative.

The exhaustion checks therefore compare budget <= 0 (cff1 depend()'s
seac walk) and budget > 0 (the subset planner's seac-closure and CFF2
get_extents verdicts), not < 0 / >= 0, which that path can never
reach.  With < 0, cff1 depend() never failed, so
hb_subset_depend_from_face_or_fail() returned a result for an
adversarial font that exhausts the budget even though the equivalent
subset fails; that is the test-subset-cff1 shared-subrs case on CI
builds with -Dexperimental_api=true.  Document this at each check.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-11 18:33:22 -06:00

360 B