mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 23:41:03 +01:00
* [subset] Bound avar2 offset-compensation's quadratic breakpoint work avar::_subset_avar2() builds the offset-compensation knots and runs _avar2_estimate_offset_error() twice per narrowed axis; every breakpoint and every estimator sample is looked up in the old segment map, the instantiated mapping and the knots by linear search, so the cost is quadratic in the axis's avar breakpoints and nothing bounded it (42 s of CPU for a 131 KB font, re-run on each serializer out-of-room retry). Add HB_BUDGET_SUBSET_TABLE to hb-limits.hh (same hb_budget_spend() idiom as the rendering budgets) and, right before the knot loop and the estimator, charge (256 + breakpoints) * 8 * breakpoints from a budget shared by all axes of the table. Charging at the point where the quadratic work starts covers the O(n^2) knot-collection loop and its map_float() calls too. On exhaustion the table fails through c->serializer->check_success(), so the whole subset fails (hb_subset_or_fail() returns NULL) rather than avar being silently dropped from an avar2-instanced font. The budget is 1<<30: a 5,000-breakpoint axis narrowed to -900:900 is still admitted (0.6 s), while 16,385 and 32,769 breakpoints are rejected in 0.01 s (the 42 s DoS). A reviewer may tighten it; real avar maps have a handful of breakpoints, far below the cap. Adds test_avar2_restrict_many_breakpoints to test/api/test-subset-avar2.c. This caps the cost; it does not remove the quadratic algorithm. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-CWJXX1BL. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [subset] Bound avar2 offset-compensation tuple memory to the table size avar::_subset_avar2() adds one item_variations_t tuple per retained avar v1 breakpoint and per biased axis, and add_tuple() stores each as a dense column with one bool and one float per row of the VarData it joins. VarData.itemCount can be 65535 with zero-width rows (a 6-byte VarData with no regions passes sanitize), so breakpoints x rows x 5 bytes is not bounded by the font: a 20 KB font drove hb-subset to 2.8 GB, and add_tuple() silently dropped the failed allocation so the result still exited 0. Charge each tuple's item_count deltas to a budget before the add_tuple() call, at all three avar2 sites (empty-region bias, knot tents, free-axis bias), with the existing hb_budget_spend() idiom. The budget is max(HB_AVAR2_MAX_DELTAS_MIN, HB_AVAR2_MAX_DELTAS_FACTOR x table length) -- 128 deltas per byte, floor 65536, new macros in hb-limits.hh next to the other table-derived limits. Exhaustion fails the subset through c->serializer->check_success(false) rather than dropping avar. The attack font is now rejected with a 19 MB peak instead of 2.8 GB; valid fonts (a few deltas per byte) stay far below the budget. Adds test_avar2_empty_rows to test/api/test-subset-avar2.c: 513 breakpoints (which clear the quadratic estimate budget) with a 65535-row region-less VarData, so the delta budget is what rejects it. The charge is placed in the caller; the sibling improvement of making add_tuple() return failure instead of swallowing the dropped allocation lives in hb-ot-var-common.hh and is left for that file's own change. The as_item_varstore() rows x regions allocation and duplicate_row() remain uncovered, as in the report. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-3HO8JDDV. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * [subset] Binary-search avar2 offset compensation instead of budgeting it Offset compensation looked every breakpoint and estimator sample up in the old segment map, the instantiated mapping and the knots by linear search, and inserted each breakpoint knot by shifting, so its cost was quadratic in the axis's breakpoints. The previous commit capped that with HB_BUDGET_SUBSET_TABLE; remove the quadratic work instead: - SegmentMaps::map_float() takes a sorted flag; for maps whose fromCoords ascend it finds the exact-match run and the segment by binary search, which lands where the linear scans do. - The instantiated mapping is sorted by fromCoord, and the knots by z, so _avar2_map_new_mapping() and _avar2_eval_offset() binary-search. - The estimator pulls the ascending knots back through the mapping with one forward scan over its toCoords. - Breakpoint knots are pushed and then sorted, keeping the first of equal z as _avar2_add_knot() did. An old segment map with fromCoords out of order, which the spec forbids, would still need a linear scan per lookup; for such an axis keep the {-1, 0, +1, z_old} anchors and skip the estimate. Remove HB_BUDGET_SUBSET_TABLE, now unused. test_avar2_restrict_many_breakpoints now expects the 32,769-breakpoint axis to instance, and to match, rather than fail; the whole test runs in 0.2 s. 65,000-breakpoint maps instance in under 0.25 s. Outputs are byte-identical to before for every test-subset-avar2 instance and for 317 random avar2 maps (sorted, with duplicate fromCoords, and with non-monotone toCoords) on random ranges; only maps with unsorted fromCoords differ. Tested: meson test -C build (277/277 OK). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * [subset] Reuse the one bsearch loop for avar2 instead of a new one The avar2 offset-compensation searches went through a hand-rolled _avar_partition_point loop -- a second binary search living next to hb_bsearch_impl. Route them through hb_bsearch_impl instead, with comparators that never return 0 so it reports the insertion position (a lower or upper bound, per the comparator) rather than a match. No behavior change: the full test suite passes and the CWJXX1BL and 3HO8JDDV reproducers stay bounded (0.13 s CPU and 21 MB peak, the table failing cleanly), same as before. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>