mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 15:31:05 +01:00
[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 <noreply@anthropic.com>
This commit is contained in:
1 parent
86c1bbb3b4
commit
d8ca8b132d
4 files changed
+45
No files matched your search
@@ -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. */
|
||||
|
||||
@@ -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;
|
||||
|
||||
Binary file not shown.
@@ -87,6 +87,33 @@ test_instance_cff2_private_blend_overflow (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_instance_cff2_vardata_budget (void)
|
||||
{
|
||||
/* 40 VarData offsets aliasing one 800-region VarData no blend uses:
|
||||
* partial instancing must not build a k×k region transform for each
|
||||
* of them. Over budget, create() fails, so the partial instance fails
|
||||
* safely like any other CFF2 planning failure instead of exhausting
|
||||
* memory. */
|
||||
hb_face_t *face = hb_test_open_font_file ("fonts/CFF2.vardata-budget.otf");
|
||||
|
||||
hb_set_t *glyphs = hb_set_create ();
|
||||
hb_set_add_range (glyphs, 0, 1);
|
||||
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_union (hb_subset_input_glyph_set (input), glyphs);
|
||||
hb_set_destroy (glyphs);
|
||||
g_assert_true (hb_subset_input_set_axis_range (input, face,
|
||||
HB_TAG ('w', 'g', 'h', 't'),
|
||||
200, 600, 400));
|
||||
|
||||
hb_face_t *subset = hb_subset_or_fail (face, input);
|
||||
hb_subset_input_destroy (input);
|
||||
g_assert_null (subset);
|
||||
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_instance_cff2_empty_bounds (void)
|
||||
@@ -376,6 +403,7 @@ main (int argc, char **argv)
|
||||
|
||||
hb_test_add (test_instance_cff2);
|
||||
hb_test_add (test_instance_cff2_private_blend_overflow);
|
||||
hb_test_add (test_instance_cff2_vardata_budget);
|
||||
hb_test_add (test_instance_cff2_empty_bounds);
|
||||
hb_test_add (test_instance_cff2_large_store);
|
||||
hb_test_add (test_instance_cff2_absolute_rounding);
|
||||
|
||||
Reference in new issue
Block a user