From edf8de90d6fa87bccf61eb47c977e5319792a007 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 15:53:49 -0600 Subject: [PATCH] [subset] Don't retain ItemVariationStore rows the source lacks The subset plan turned every variation index collected by the COLR, GDEF and BASE closures into a row to keep, checking only the outer index against the subtable count and never the inner index against VarData::itemCount. A font could therefore name tens of thousands of rows in a VarData whose itemCount is 2, and partial instancing then sized its work from that inflated row count, allocating regionIndexCount x rows x 5 bytes (3.2 GB from a 240 KB COLRv1 font). generate_varstore_inner_maps() now takes the store and keeps only rows that ItemVariationStore::has_delta_set() confirms exist, and remap_variation_indices() maps such an index to HB_OT_LAYOUT_NO_VARIATIONS_INDEX without consuming a new row. That preserves the runtime meaning of an out-of-range index (delta 0) and keeps the two functions in step; all consumers already handle the sentinel. This covers the COLR, GDEF and BASE callers. Well-formed fonts are byte-identical. The HVAR/VVAR route noted in the report (its planner builds row maps itself, one per retained glyph) is left for a follow-up: folding the missing rows into a shared zero row changes the output form of a map-less HVAR whose glyph count exceeds the store's itemCount, which is better landed on its own. Adds an API regression test (test_subset_colr_phantom_rows) that fails on the unpatched tree. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-0TLE41I0. Co-Authored-By: Claude Opus 4.8 --- src/hb-subset-plan-layout.cc | 3 +- src/hb-subset-plan-var.cc | 32 ++++++-- src/hb-subset-plan.cc | 3 +- src/hb-subset-plan.hh | 5 +- test/api/fonts/colr-phantom-rows.ttf | Bin 0 -> 940 bytes .../fonts/generate-colr-phantom-rows-font.py | 44 +++++++++++ test/api/test-subset-colr.c | 69 ++++++++++++++++++ 7 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 test/api/fonts/colr-phantom-rows.ttf create mode 100644 test/api/fonts/generate-colr-phantom-rows-font.py diff --git a/src/hb-subset-plan-layout.cc b/src/hb-subset-plan-layout.cc index 06f0c055e..7c6fd4f8a 100644 --- a/src/hb-subset-plan-layout.cc +++ b/src/hb-subset-plan-layout.cc @@ -564,8 +564,7 @@ collect_layout_variation_indices (hb_subset_plan_t* plan) plan->all_axes_pinned, plan->layout_variation_idx_delta_map); - unsigned subtable_count = gdef->get_var_store ().get_sub_table_count (); - generate_varstore_inner_maps (varidx_set, subtable_count, plan->gdef_varstore_inner_maps); + generate_varstore_inner_maps (gdef->get_var_store (), varidx_set, plan->gdef_varstore_inner_maps); plan->check_success (!plan->layout_condition_values.in_error ()); } #endif diff --git a/src/hb-subset-plan-var.cc b/src/hb-subset-plan-var.cc index 446069178..72216e2c0 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -36,11 +36,13 @@ #ifndef HB_NO_VAR + template void - generate_varstore_inner_maps (const hb_set_t& varidx_set, - unsigned subtable_count, + generate_varstore_inner_maps (const ItemVarStore &var_store, + const hb_set_t& varidx_set, hb_vector_t &inner_maps /* OUT */) { + unsigned subtable_count = var_store.get_sub_table_count (); if (varidx_set.is_empty () || subtable_count == 0) return; if (unlikely (!inner_maps.resize (subtable_count))) return; @@ -49,12 +51,20 @@ uint16_t major = idx >> 16; uint16_t minor = idx & 0xFFFF; - if (major >= subtable_count) + /* Runtime treats out-of-range delta-set indices as zero; don't + * retain rows the store doesn't have. */ + if (!var_store.has_delta_set (idx)) continue; inner_maps[major].add (minor); } } + template + void + generate_varstore_inner_maps (const OT::ItemVariationStore &var_store, + const hb_set_t& varidx_set, + hb_vector_t &inner_maps /* OUT */); + /* Evaluate a variation-region tent at v, mirroring VarRegionAxis::evaluate * (invalid regions evaluate as constant 1). */ static double @@ -392,7 +402,7 @@ auto *store_cache = var_store.create_cache (); unsigned new_major = 0, new_minor = 0; - unsigned last_major = (variation_indices.get_min ()) >> 16; + unsigned last_major = (unsigned) -1; for (unsigned idx : variation_indices) { int delta = 0; @@ -408,7 +418,15 @@ uint16_t major = idx >> 16; if (major >= subtable_count) break; - if (major != last_major) + /* Keep in sync with generate_varstore_inner_maps (): rows the store + * doesn't have are not retained, so such an index keeps its runtime + * meaning (no delta) instead of consuming a new row. */ + if (!var_store.has_delta_set (idx)) + { + variation_idx_delta_map.set (idx, hb_pair_t (HB_OT_LAYOUT_NO_VARIATIONS_INDEX, delta)); + continue; + } + if (last_major != (unsigned) -1 && major != last_major) { new_minor = 0; ++new_major; @@ -444,15 +462,13 @@ hb_set_t varidx_set; base->collect_variation_indices (plan, varidx_set); const OT::ItemVariationStore &var_store = base->get_var_store (); - unsigned subtable_count = var_store.get_sub_table_count (); - remap_variation_indices (var_store, varidx_set, plan->normalized_coords, !plan->pinned_at_default, plan->all_axes_pinned, plan->base_variation_idx_map); - generate_varstore_inner_maps (varidx_set, subtable_count, plan->base_varstore_inner_maps); + generate_varstore_inner_maps (var_store, varidx_set, plan->base_varstore_inner_maps); } #endif diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index efe1f1383..0a3aed9fb 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -147,8 +147,7 @@ static void _colr_closure (hb_subset_plan_t* plan, { const OT::ItemVariationStore &var_store = colr.get_var_store (); // generated inner_maps is used by ItemVariationStore serialize(), which is subset only - unsigned subtable_count = var_store.get_sub_table_count (); - generate_varstore_inner_maps (variation_indices, subtable_count, plan->colrv1_varstore_inner_maps); + generate_varstore_inner_maps (var_store, variation_indices, plan->colrv1_varstore_inner_maps); if (!plan->all_axes_pinned) remap_variation_indices (var_store, diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index bb7c95275..3c7356caf 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -382,9 +382,10 @@ remap_colrv1_delta_set_index_indices (const DeltaSetIndexMap &index_map, hb_map_t &new_deltaset_idx_varidx_map /* OUT */); +template HB_INTERNAL void -generate_varstore_inner_maps (const hb_set_t& varidx_set, - unsigned subtable_count, +generate_varstore_inner_maps (const ItemVarStore &var_store, + const hb_set_t& varidx_set, hb_vector_t &inner_maps /* OUT */); HB_INTERNAL bool diff --git a/test/api/fonts/colr-phantom-rows.ttf b/test/api/fonts/colr-phantom-rows.ttf new file mode 100644 index 0000000000000000000000000000000000000000..4c2795e9057d5c0be8eef1e1077335f8c20d69be GIT binary patch literal 940 zcmaJ4f25dPloW}|Y62997Q%MnWnhy)AK3rDPu+_3@ALNt%u=Jv?$?Qyr4OM*p8 zOR>>XyEKA@gp?^Qw6{oMqxb_1qzIhfyxk*-0S9(wzM1dMo0+#z0H&}D8}&-9>OZSI z+y|`hjJ~VX7pqLq0Ctynwze{VcWH6yC!n4Zudc$z#CIUG@f-Yl{PxB52gk6p>Mw79+GPe~D?Y_k zHXhH|-iUW(e8!~Gc~K@}t~KE^Z_MId>NO_dj069%7pgy^kgK8d9z1;c=d5?Gvut>q zj3OtHN=4pD2DM|b9*u~oNAEAq!310^=W!)(o%IZs;=, 34); + unsigned list = read_uint_be (data + 14, 4); + g_assert_cmpuint (list + 10, <=, length); + g_assert_cmpuint (read_uint_be (data + list, 4), ==, 1); + unsigned paint = list + read_uint_be (data + list + 6, 4); + g_assert_cmpuint (paint + 16, <=, length); + g_assert_cmpuint (data[paint], ==, variable ? 5 : 4); + unsigned line = paint + read_uint_be (data + paint + 1, 3); + unsigned stride = variable ? 10 : 6; + g_assert_cmpuint (line + 3 + 3 * stride, <=, length); + g_assert_cmpuint (read_uint_be (data + line + 1, 2), ==, 3); + for (unsigned stop = 0; stop < 3; stop++) + { + unsigned at = line + 3 + stop * stride; + /* Only the first stop names rows the store has; the others keep + * their runtime meaning of no variation. */ + unsigned offset = stop * 8192, alpha = 16384; + if (!variable && !stop) + { + offset = 1024; + alpha = 14336; + } + g_assert_cmpuint (read_uint_be (data + at, 2), ==, offset); + g_assert_cmpuint (read_uint_be (data + at + 2, 2), ==, stop % 2); + g_assert_cmpuint (read_uint_be (data + at + 4, 2), ==, alpha); + if (variable) + g_assert_cmpuint (read_uint_be (data + at + 6, 4), ==, stop ? 0xFFFFFFFFu : 0); + } + unsigned store = read_uint_be (data + 30, 4); + if (!variable) + g_assert_cmpuint (store, ==, 0); + else + { + g_assert_cmpuint (store + 12, <=, length); + g_assert_cmpuint (read_uint_be (data + store + 6, 2), ==, 1); + unsigned var_data = store + read_uint_be (data + store + 8, 4); + g_assert_cmpuint (var_data + 6, <=, length); + /* No rows are invented for the stops the source store doesn't cover. */ + g_assert_cmpuint (read_uint_be (data + var_data, 2), ==, 2); + } + hb_blob_destroy (blob); +} + +static void +test_subset_colr_phantom_rows (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/colr-phantom-rows.ttf"); + for (unsigned mode = 0; mode < 3; mode++) + { + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_glyph_set (input), 0, 1); + if (mode == 1) + g_assert_true (hb_subset_input_set_axis_range (input, face, HB_TAG ('w','g','h','t'), 0, 0.5f, 0)); + if (mode == 2) + g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w','g','h','t'), 1)); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + check_colr_phantom_rows (subset, mode != 2); + hb_face_destroy (subset); + } + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -896,6 +964,7 @@ main (int argc, char **argv) hb_test_add (test_subset_colr_var_overflow); hb_test_add (test_subset_colr_layers_out_of_range); hb_test_add (test_subset_colr_shared_layers); + hb_test_add (test_subset_colr_phantom_rows); return hb_test_run(); }