From d8ca8b132dfe61be0035fce2f6e181f027eed53c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 15:45:59 -0600 Subject: [PATCH] [subset/cff2] Budget the region transforms built for partial instancing cff2_instancing_plan_t::create() builds k identity tuples of k cells (then keeps a k-float column per surviving solver tuple) for every entry of the CFF2 ItemVariationStore's VarData offset array, and nothing bounded k (a 16-bit region count), the number of entries, or their aliasing of one VarData. The work is therefore quadratic in a font field, so a 12 KB font cost 2.2 GB. Charge those cells, before they are allocated, to a per-table budget HB_BUDGET_SUBSET_CFF2_CELLS (2^24 cells) via the existing hb_budget_spend helper, at both allocation sites (the k*k identity tuples and the surviving solver columns). Exhausting it fails create() like any other planning failure, so the partial instance fails safely (bounded at ~175 MB worst case) instead of exhausting memory. Adds test_instance_cff2_vardata_budget: a 6.8 KB font with 40 offsets aliasing one unused 800-region VarData must fail to partially instance rather than build a region transform for each; it succeeds (keeping CFF2 at hundreds of MB) before this change. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-I7KWU2X6. Co-Authored-By: Claude Opus 4.8 --- src/hb-limits.hh | 8 +++++++ src/hb-subset-cff2.cc | 9 ++++++++ test/api/fonts/CFF2.vardata-budget.otf | Bin 0 -> 6816 bytes test/api/test-instance-cff2.c | 28 +++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 test/api/fonts/CFF2.vardata-budget.otf diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 5bffa574f..cfa0782a4 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -259,6 +259,14 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28) #endif +/* Partially instancing one CFF2 table in the subsetter, in cells of the + * old-region to new-region matrices built for its VarDatas. A VarData + * with k regions costs k * k cells for 2 * k bytes of font, and many + * offsets can share one VarData. Exceeding the budget fails the table. */ +#ifndef HB_BUDGET_SUBSET_CFF2_CELLS +#define HB_BUDGET_SUBSET_CFF2_CELLS ((int64_t) 1 << 24) +#endif + /* The flat pixel-work floor for one raster draw or paint session. Pixel and * outline work use separate live counters, so keep this independent of * the common outline budget above. */ diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 876384fa2..97e326f5c 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -133,12 +133,17 @@ struct cff2_instancing_plan_t /* Instance each VarData with identity delta rows; the surviving tuples' * delta columns are the old-region → new-region transform. */ OT::optimize_scratch_t scratch; + int64_t budget = HB_BUDGET_SUBSET_CFF2_CELLS; for (unsigned m = 0; m < major_count; m++) { const OT::VarData &var_data = varStore.get_sub_table (m); unsigned k = var_data.get_region_index_count (); vardata_transform_t &t = transforms.arrayZ[m]; + /* k identity tuples of k cells each; neither k nor the number of + * VarDatas is otherwise bounded, and offsets may alias one VarData. */ + if (unlikely (!hb_budget_spend (budget, k, k))) return false; + if (!t.gains.resize_exact (k)) return false; varStore.get_region_scalars (m, plan->normalized_coords.arrayZ, plan->normalized_coords.length, @@ -165,6 +170,10 @@ struct cff2_instancing_plan_t if (!tuples.instantiate (plan->axes_location, plan->axes_triple_distances, scratch)) return false; + /* The solver can split a region into several; the surviving tuples' + * columns are kept for the rest of the table. */ + if (unlikely (!hb_budget_spend (budget, tuples.tuple_vars.length, k))) return false; + for (auto &tuple : tuples.tuple_vars) { if (unlikely (tuple.deltas_x.length != k)) return false; diff --git a/test/api/fonts/CFF2.vardata-budget.otf b/test/api/fonts/CFF2.vardata-budget.otf new file mode 100644 index 0000000000000000000000000000000000000000..f83d3e15f243318fc1d305de7234f86aef842fcc GIT binary patch literal 6816 zcmd7W<98q37Qpen_cUmmHfqx(O`6oUZQDj_+qP2Mwr$(CZQH%Se4ppOyMMslYn{)Y zJ#%K(%)FSr=9I5erMyHFqHwWdnf}?5G9p25ns$xo_)i_2$CZ;bZxYe?-@1V-XKNGD zz5T!SG*@;vaTEVg*O}*hT}nt`)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