[subset] Bound the dense delta matrix when rebuilding a VarStore

item_variations_t::as_item_varstore() allocates one int per
(row, region) pair for the whole rebuilt ItemVariationStore, every row
as wide as the full region list. In the font a row only pays for the
regions of its own VarData (possibly none, and several dataSets offsets
may share one VarData), so the row count and the region count are
independent and their product is unbounded: a ~120 KB font drove the
allocation to 2.4 GB, and a 972-byte font with region-less rows to
~800 MB.

Bound the matrix before allocating it, at the single site all
instancing callers (MVAR, HVAR/VVAR, GDEF, BASE, COLR, avar) pass
through: HB_SUBSET_MAX_VARSTORE_ROWS (1<<21, which also bounds the
per-row hb_vector_t overhead and catches the zero-width-row variant)
and HB_SUBSET_MAX_VARSTORE_DELTAS (1<<26 cells, 256 MB of int),
computed in 64-bit. These are flat ceilings on this one allocation
rather than a bound proportional to the source size: optimizer output
routinely has several VarData over disjoint region subsets, so the
dense matrix is legitimately much wider than any single VarData and a
proportional bound would reject it. No store in the test fonts comes
near the cell cap. Returning
false is the path a failed resize() already takes, and every caller
drops the table on it. Rows are fixed-width, so they are now allocated
with resize_exact(), removing the ~19% growth slack per row.

The cap bounds the amplification rather than removing it (a ~75 KB
font just under the cell cap still peaks around 265 MB); the
structural follow-up is to stop making every row as wide as the whole
region list.

Adds a unit test (test_delta_matrix_limits) covering both caps and an
under-cap case.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-10 16:36:35 -06:00
1 parent 7f65c5d820
commit 52152cbf97
3 files changed
+62 -1

No files matched your search

+16
View File
@@ -285,6 +285,22 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
#endif
/* The instancer rebuilds an ItemVariationStore from a dense delta matrix,
* one int per (row, region) pair. In the font a row only pays for the
* regions of its own VarData, so a small font can ask for a huge matrix.
* These are flat ceilings on that one allocation: a proportional bound
* would reject legitimate optimizer output, where several VarData use
* disjoint region subsets so the dense matrix is much wider than any one
* VarData. Exceeding either limit fails instancing of that store. */
#ifndef HB_SUBSET_MAX_VARSTORE_ROWS
#define HB_SUBSET_MAX_VARSTORE_ROWS (1u << 21)
#endif
#ifndef HB_SUBSET_MAX_VARSTORE_DELTAS
#define HB_SUBSET_MAX_VARSTORE_DELTAS ((uint64_t) 1 << 26)
#endif
#ifndef HB_REPACKER_MAX_ITERATIONS
#define HB_REPACKER_MAX_ITERATIONS 500
#endif
+8 -1
View File
@@ -2233,10 +2233,17 @@ struct item_variations_t
for (unsigned major = 0; major < var_data_num_rows.length; major++)
total_rows += var_data_num_rows[major];
/* Every row is allocated as wide as the whole region list, while in the
* font a row only pays for the regions of its own VarData (possibly none).
* Bound the matrix before allocating it. */
if (unlikely (total_rows > HB_SUBSET_MAX_VARSTORE_ROWS ||
(uint64_t) total_rows * num_cols > HB_SUBSET_MAX_VARSTORE_DELTAS))
return false;
if (!delta_rows.resize (total_rows)) return false;
/* init all rows to [0]*num_cols */
for (unsigned i = 0; i < total_rows; i++)
if (!(delta_rows[i].resize (num_cols))) return false;
if (!(delta_rows[i].resize_exact (num_cols))) return false;
/* old VarIdxes -> full encoding_row mapping */
hb_hashmap_t<unsigned, const hb_vector_t<int>*> front_mapping;
+38
View File
@@ -561,6 +561,43 @@ test_item_varstore_instancer_varidx_overflow ()
}
static void
test_delta_matrix_limits ()
{
const unsigned num_rows = 0xFFFFu;
/* One row using every region plus many rows using only the first one: the
* dense matrix is rows x regions while the store only pays for the deltas. */
const unsigned region_counts[] = {2, (unsigned) (HB_SUBSET_MAX_VARSTORE_DELTAS / num_rows) + 1};
for (unsigned num_regions : region_counts)
{
OT::item_variations_t item_vars;
unsigned wide = item_vars.add_vardata (1);
for (unsigned i = 0; i < num_regions; i++)
{
hb_hashmap_t<hb_tag_t, Triple> region;
region.set (HB_TAG ('T','E','S','T'), Triple (0, (i + 1.) / num_regions, 1));
item_vars.add_tuple (wide, std::move (region), 0, 1, 1);
}
unsigned tall = item_vars.add_vardata (num_rows);
hb_hashmap_t<hb_tag_t, Triple> region;
region.set (HB_TAG ('T','E','S','T'), Triple (0, 1. / num_regions, 1));
item_vars.add_tuple (tall, std::move (region), 0, 1, num_rows);
hb_always_assert (item_vars.build_region_list ());
hb_always_assert (item_vars.get_region_list ().length == num_regions);
hb_always_assert (item_vars.as_item_varstore () == (num_regions == 2));
}
/* Rows of a VarData without regions take no space in the font at all. */
OT::item_variations_t item_vars;
unsigned outer = item_vars.add_vardata (1);
item_vars.add_tuple (outer, hb_hashmap_t<hb_tag_t, Triple> (), 0, 1, 1);
for (unsigned i = 0; i < HB_SUBSET_MAX_VARSTORE_ROWS / num_rows + 1; i++)
item_vars.add_vardata (num_rows);
hb_always_assert (item_vars.build_region_list ());
hb_always_assert (!item_vars.as_item_varstore ());
}
int
main (int argc, char **argv)
{
@@ -578,4 +615,5 @@ main (int argc, char **argv)
test_multi_instancer_budget ();
test_condition_budget ();
test_repeated_region_index ();
test_delta_matrix_limits ();
}