mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 14:31:10 +01:00
[subset] Budget the dense per-point expansion of gvar/cvar tuples
create_from_tuple_var_data() expands every tuple to arrays with one entry per point of its glyph (per cvt value), however few points the tuple actually names, and keeps them until the table is serialized. The tuple count (12-bit) and point count (16-bit) are each bounded per glyph, but their product and its sum over the table are not, and neither factor costs many font bytes: 15 bytes of gvar can expand to ~590 KB, and a 31 KB font reached 2.1 GiB. Give the decode a per-table points budget, HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR per byte of the source table floored at HB_BUDGET_SUBSET_TUPLE_POINTS_MIN, owned by glyph_variations_t for gvar and a local in cvar::subset(), and charge point_count per decoded tuple before expanding it. Exhausting it returns false exactly like the function's existing allocation-failure paths (the table is dropped from the subset). Real gvar tables expand to only a small multiple of their byte size, so the factor leaves ample headroom. The extra copies made when a range limit splits a tuple are already bounded by the instancer split budget in optimize_scratch_t, so they are not charged here again. Adds a unit test (test_tuple_points_budget) covering the decode charge. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-8CH2PGW8. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
171a4fa59f
commit
224fca316c
5 files changed
+81
-9
No files matched your search
@@ -298,6 +298,21 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
|
||||
#endif
|
||||
|
||||
|
||||
/* Instancing one whole gvar or cvar table in the subsetter, in points.
|
||||
* Every tuple is expanded to one entry per point of its glyph (per cvt
|
||||
* value), however few of them it refers to; a tuple takes a dozen bytes
|
||||
* and a run of repeated glyf points a fraction of a byte each, so the
|
||||
* size of the font does not bound the product. The budget is this many
|
||||
* points per byte of the table, but no less than the minimum; exceeding
|
||||
* it fails the table. */
|
||||
#ifndef HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR
|
||||
#define HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR 64
|
||||
#endif
|
||||
#ifndef HB_BUDGET_SUBSET_TUPLE_POINTS_MIN
|
||||
#define HB_BUDGET_SUBSET_TUPLE_POINTS_MIN ((int64_t) 1 << 20)
|
||||
#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.
|
||||
|
||||
+11
-3
@@ -1027,7 +1027,8 @@ struct TupleVariationData
|
||||
const hb_vector_t<unsigned> &shared_indices,
|
||||
const hb_array_t<const F2DOT14> shared_tuples,
|
||||
hb_alloc_pool_t *pool = nullptr,
|
||||
bool is_composite_glyph = false)
|
||||
bool is_composite_glyph = false,
|
||||
int64_t *budget = nullptr)
|
||||
{
|
||||
hb_vector_t<unsigned> private_indices;
|
||||
hb_vector_t<int> deltas_x;
|
||||
@@ -1066,6 +1067,11 @@ struct TupleVariationData
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Every tuple is expanded to point_count entries, however few
|
||||
* points it names; charge that to the table budget, if any. */
|
||||
if (budget && unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, point_count)))
|
||||
return false;
|
||||
|
||||
tuple_delta_t var;
|
||||
var.axis_tuples = std::move (axis_tuples);
|
||||
if (unlikely (!var.indices.allocate_from_pool (pool, point_count) ||
|
||||
@@ -1753,7 +1759,8 @@ struct TupleVariationData
|
||||
const hb_array_t<const F2DOT14> shared_tuples,
|
||||
tuple_variations_t& tuple_variations, /* OUT */
|
||||
hb_alloc_pool_t *pool = nullptr,
|
||||
bool is_composite_glyph = false) const
|
||||
bool is_composite_glyph = false,
|
||||
int64_t *budget = nullptr) const
|
||||
{
|
||||
return tuple_variations.create_from_tuple_var_data (iterator, tupleVarCount,
|
||||
point_count, is_gvar,
|
||||
@@ -1761,7 +1768,8 @@ struct TupleVariationData
|
||||
shared_indices,
|
||||
shared_tuples,
|
||||
pool,
|
||||
is_composite_glyph);
|
||||
is_composite_glyph,
|
||||
budget);
|
||||
}
|
||||
|
||||
bool serialize (hb_serialize_context_t *c,
|
||||
|
||||
@@ -58,7 +58,8 @@ struct cvar
|
||||
hb_blob_t *blob,
|
||||
bool is_gvar,
|
||||
const hb_map_t *axes_old_index_tag_map,
|
||||
TupleVariationData<>::tuple_variations_t& tuple_variations /* OUT */) const
|
||||
TupleVariationData<>::tuple_variations_t& tuple_variations, /* OUT */
|
||||
int64_t *budget = nullptr) const
|
||||
{
|
||||
hb_vector_t<unsigned> shared_indices;
|
||||
TupleVariationData<>::tuple_iterator_t iterator;
|
||||
@@ -71,7 +72,9 @@ struct cvar
|
||||
axes_old_index_tag_map,
|
||||
shared_indices,
|
||||
hb_array<const F2DOT14> (),
|
||||
tuple_variations);
|
||||
tuple_variations,
|
||||
nullptr, false,
|
||||
budget);
|
||||
}
|
||||
|
||||
static bool calculate_cvt_deltas (unsigned axis_count,
|
||||
@@ -152,10 +155,13 @@ struct cvar
|
||||
unsigned point_count = hb_blob_get_length (cvt_blob) / FWORD::static_size;
|
||||
hb_blob_destroy (cvt_blob);
|
||||
|
||||
int64_t budget = hb_max ((int64_t) c->source_blob->length * HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR,
|
||||
HB_BUDGET_SUBSET_TUPLE_POINTS_MIN);
|
||||
if (!decompile_tuple_variations (axis_count, point_count,
|
||||
c->source_blob, false,
|
||||
&(c->plan->axes_old_index_tag_map),
|
||||
tuple_variations))
|
||||
tuple_variations,
|
||||
&budget))
|
||||
return_trace (false);
|
||||
|
||||
optimize_scratch_t scratch;
|
||||
|
||||
@@ -76,6 +76,9 @@ struct glyph_variations_t
|
||||
|
||||
hb_alloc_pool_t pool;
|
||||
|
||||
/* points all tuples of this table may still be expanded to */
|
||||
int64_t budget = 0;
|
||||
|
||||
public:
|
||||
unsigned compiled_shared_tuples_count () const
|
||||
{ return shared_tuples_count; }
|
||||
@@ -92,11 +95,15 @@ struct glyph_variations_t
|
||||
bool create_from_glyphs_var_data (unsigned axis_count,
|
||||
const hb_array_t<const F2DOT14> shared_tuples,
|
||||
const hb_subset_plan_t *plan,
|
||||
const hb_hashmap_t<hb_codepoint_t, hb_bytes_t>& new_gid_var_data_map)
|
||||
const hb_hashmap_t<hb_codepoint_t, hb_bytes_t>& new_gid_var_data_map,
|
||||
unsigned table_length)
|
||||
{
|
||||
if (unlikely (!glyph_variations.alloc_exact (plan->new_to_old_gid_list.length)))
|
||||
return false;
|
||||
|
||||
budget = hb_max ((int64_t) table_length * HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR,
|
||||
HB_BUDGET_SUBSET_TUPLE_POINTS_MIN);
|
||||
|
||||
auto it = hb_iter (plan->new_to_old_gid_list);
|
||||
for (auto &_ : it)
|
||||
{
|
||||
@@ -132,7 +139,8 @@ struct glyph_variations_t
|
||||
shared_indices, shared_tuples,
|
||||
tuple_vars, /* OUT */
|
||||
&pool,
|
||||
is_composite_glyph))
|
||||
is_composite_glyph,
|
||||
&budget))
|
||||
return false;
|
||||
glyph_variations.push (std::move (tuple_vars));
|
||||
}
|
||||
@@ -363,7 +371,8 @@ struct gvar_GVAR
|
||||
if (new_gid_var_data_map.in_error ()) return false;
|
||||
|
||||
hb_array_t<const F2DOT14> shared_tuples = (this+sharedTuples).as_array ((unsigned) sharedTupleCount * (unsigned) axisCount);
|
||||
return glyph_vars.create_from_glyphs_var_data (axisCount, shared_tuples, c->plan, new_gid_var_data_map);
|
||||
return glyph_vars.create_from_glyphs_var_data (axisCount, shared_tuples, c->plan, new_gid_var_data_map,
|
||||
c->source_blob->length);
|
||||
}
|
||||
|
||||
template<typename Iterator,
|
||||
|
||||
@@ -355,6 +355,39 @@ test_tuple_values_decompile_trunc ()
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
decompile_cvar_with_budget (OT::TupleVariationData<>::tuple_variations_t &tuple_variations,
|
||||
int64_t *budget)
|
||||
{
|
||||
const OT::cvar* cvar_table = reinterpret_cast<const OT::cvar*> (cvar_data);
|
||||
hb_map_t axis_idx_tag_map;
|
||||
axis_idx_tag_map.set (0, HB_TAG ('w', 'g', 'h', 't'));
|
||||
|
||||
hb_blob_t *blob = hb_blob_create ((const char *) cvar_data, sizeof (cvar_data),
|
||||
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
|
||||
bool result = cvar_table->decompile_tuple_variations (1, 65, blob, false, &axis_idx_tag_map,
|
||||
tuple_variations, budget);
|
||||
hb_blob_destroy (blob);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
test_tuple_points_budget ()
|
||||
{
|
||||
/* Each tuple is expanded to 65 points; the decode charges them. */
|
||||
{
|
||||
OT::TupleVariationData<>::tuple_variations_t tuple_variations;
|
||||
int64_t budget = 2 * 65 - 1;
|
||||
hb_always_assert (!decompile_cvar_with_budget (tuple_variations, &budget));
|
||||
}
|
||||
|
||||
OT::TupleVariationData<>::tuple_variations_t tuple_variations;
|
||||
int64_t budget = 2 * 65;
|
||||
hb_always_assert (decompile_cvar_with_budget (tuple_variations, &budget));
|
||||
hb_always_assert (tuple_variations.tuple_vars.length == 2);
|
||||
hb_always_assert (budget == 0);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -366,4 +399,5 @@ main (int argc, char **argv)
|
||||
test_tuple_values_decompile_trunc ();
|
||||
test_shared_tuple_scalar_cache ();
|
||||
test_decompile_cvar ();
|
||||
test_tuple_points_budget ();
|
||||
}
|
||||
Reference in new issue
Block a user