[subset] Clamp variable advances to UFWORD range in plan

When instancing, the hmtx/vmtx advance is computed by adding the
HVAR/VVAR delta to the base advance.  The delta is attacker-
controlled and unbounded, so the sum can go negative or exceed
65535.  The negative case, cast to unsigned, produced advances
near 4 billion in hmtx_map; CFF::optimize_widths () then looped
over the full [min_w, max_w] width range, taking minutes (the
oversized cumsum resize fails cleanly, so this was a pure hang,
no memory unsafety).

Clamp the computed advance to [0, 0xFFFF], matching the UFWORD
range that hmtx/vmtx serialization stores and the hb_max (0.f, …)
clamp already used by the runtime metrics path.

Fixes a ClusterFuzz timeout in hb-subset-fuzzer (testcase
4846730722738176, minimized from timeout-fd8f4bd2a650).  The
testcase went from >60s to 5ms; added as a fuzz seed.  Full
meson test suite passes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Sonnet 5 committed 2026-08-10 12:47:41 -06:00
1 parent 7a2a501c2e
commit d94c18b6a6
2 files changed
+5

No files matched your search

+5
View File
@@ -587,6 +587,10 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan)
if (_hmtx.var_table.get_length ())
hori_aw += (int) roundf (_hmtx.var_table->get_advance_delta_unscaled (old_gid, font->coords, font->num_coords,
hvar_store_cache));
/* Malicious deltas can take the advance out of the UFWORD range that
* hmtx serialization stores. Clamp, to keep downstream consumers
* (e.g. CFF width optimizer) bounded and consistent with hmtx. */
hori_aw = hb_clamp (hori_aw, 0, 0xFFFF);
int lsb = extents.x_bearing;
if (!has_bounds_info)
{
@@ -602,6 +606,7 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan)
if (_vmtx.var_table.get_length ())
vert_aw += (int) roundf (_vmtx.var_table->get_advance_delta_unscaled (old_gid, font->coords, font->num_coords,
vvar_store_cache));
vert_aw = hb_clamp (vert_aw, 0, 0xFFFF);
hb_position_t vorg_x = 0;
hb_position_t vorg_y = 0;
int tsb = 0;