[subset] Charge Coverage and ClassDef glyph walks to a work budget (#6628)

* [subset] Charge Coverage and ClassDef glyph walks to a work budget

Coverage::subset() expands a format 2 Coverage glyph by glyph, up to
numGlyphs, every time an offset to it is subset, and nothing bounded
that work.  A 10-byte range Coverage referenced by 30,000 two-byte
offsets in a subtable listed 8 times costs 1.6e10 iterator steps,
while the sanitizer only bounds work by table bytes; a 60 KB font
keeps hb-subset busy ~50 s.  GDEF's MarkGlyphSets reaches the same
walk with no feature or lookup needing to survive planning.

Give hb_subset_context_t a per-table work budget
(HB_BUDGET_SUBSET_TABLE, in the hb_budget_spend() idiom of
hb-limits.hh) with a check_ops() helper that fails the serializer once
exhausted, and charge Coverage::subset() its capped population before
the walk.  Charging inside Coverage::subset() covers every caller
(ReverseChainSingleSubst, (Chain)ContextFormat3, GDEF MarkGlyphSets,
...) without touching them.  The same unbounded per-reference pattern
in ClassDefFormat1_3 / ClassDefFormat2_4::subset() (the range or
glyphset walk and the glyph_filter count) is charged the same way.

A hostile table that exceeds the budget now fails the subset cleanly
(hb_subset_or_fail returns NULL) in a fraction of a second instead of
succeeding slowly; valid-font output is byte-identical and the
heaviest local table spends a small fraction of the budget.
test_subset_shared_coverage_budget builds the reverse-chaining GSUB in
memory and asserts 256 shared references subset while 2048 fail.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [subset] Charge layout glyph walks to the existing subset budget

Drop the separate HB_BUDGET_SUBSET_TABLE budget and the second
hb_subset_context_t field it needed; check_ops() now charges
budget_remaining (HB_BUDGET_SUBSET_GLYF, 1 << 28), the per-table
budget glyf instancing already uses.  Both are per-table and shared
across buffer-size retries, and no table spends both.

The larger budget still bounds the attack (fails after 2^28 glyph
steps, ~3 s), so test_subset_shared_coverage_budget now expects the
failure at 8192 shared offsets instead of 2048.

Subsetting all glyphs with --layout-features='*' of Gulzar,
NotoSansKR-VF and GoogleSansFlex-VF keeps GSUB/GPOS/GDEF with output
byte-identical to before; the heaviest table (Gulzar GPOS) spends
~98K units, under 0.04% of the budget.  Full meson test: 278 OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 5.5 authored and GitHub committed 2026-11-10 16:50:50 -06:00
1 parent 7f65c5d820
commit df02261eca
5 files changed
+125 -5

No files matched your search

+5
View File
@@ -190,6 +190,11 @@ struct Coverage
{
TRACE_SUBSET (this);
const hb_subset_plan_t *plan = c->plan;
/* Ranges are walked glyph by glyph, once per offset that points at us. */
if (unlikely (!c->check_ops (hb_min (get_population (), plan->source->get_num_glyphs ()))))
return_trace (false);
auto it =
+ iter ()
| hb_take (plan->source->get_num_glyphs ())
+4 -1
View File
@@ -251,7 +251,10 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
* variable fonts spend -- the largest glyf variable font under test/
* (MPLUS1-Variable, 6,434 glyphs retained) uses ~1.2M units, and even a
* font at the 65,535-glyph maximum stays far below -- while keeping the
* adversarial worst case bounded. */
* adversarial worst case bounded. Layout tables charge their Coverage
* and ClassDef glyph walks to the same budget: a glyph range costs a few
* bytes and any number of offsets can share it, so neither the table size
* nor the sanitizer's budget bounds that work. */
#ifndef HB_BUDGET_SUBSET_GLYF
#define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28)
#endif
+11 -2
View File
@@ -1614,6 +1614,8 @@ struct ClassDefFormat1_3
if (use_class_zero)
{
const hb_set_t& glyphset = *c->plan->glyphset_gsub ();
if (glyph_filter && unlikely (!c->check_ops (glyphset.get_population ())))
return_trace (false);
unsigned glyph_count = glyph_filter
? hb_len (hb_iter (glyphset) | hb_filter (glyph_filter))
: glyphset.get_population ();
@@ -1866,8 +1868,13 @@ struct ClassDefFormat2_4
hb_sorted_vector_t<hb_codepoint_pair_t> glyph_and_klass;
hb_set_t orig_klasses;
if (glyph_set.get_population () * hb_bit_storage ((unsigned) rangeRecord.len)
< get_population ())
/* Either walk is repeated for every offset that points at us. */
unsigned glyph_set_cost = glyph_set.get_population () * hb_bit_storage ((unsigned) rangeRecord.len);
unsigned population = get_population ();
if (unlikely (!c->check_ops (hb_min (glyph_set_cost, population))))
return_trace (false);
if (glyph_set_cost < population)
{
for (hb_codepoint_t g : glyph_set)
{
@@ -1902,6 +1909,8 @@ struct ClassDefFormat2_4
}
const hb_set_t& glyphset = *c->plan->glyphset_gsub ();
if (glyph_filter && unlikely (!c->check_ops (glyphset.get_population ())))
return_trace (false);
unsigned glyph_count = glyph_filter
? hb_len (hb_iter (glyphset) | hb_filter (glyph_filter))
: glyphset.get_population ();
+8 -2
View File
@@ -55,12 +55,18 @@ struct hb_subset_context_t :
dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
/* Charge COUNT units of work that the size of the source table does
* not bound. Fails the table once its budget is exhausted. */
bool check_ops (unsigned count)
{ return serializer->check_success (hb_budget_spend (budget_remaining, count)); }
hb_blob_t *source_blob;
hb_subset_plan_t *plan;
hb_serialize_context_t *serializer;
hb_tag_t table_tag;
/* Outline work left for instancing this table. Lives here rather than in
* the table's subset() so retries with a larger buffer share it. */
/* Work left for subsetting this table: outline work for glyf instancing,
* Coverage and ClassDef glyph walks for layout tables. Lives here rather
* than in the table's subset() so retries with a larger buffer share it. */
int64_t budget_remaining = HB_BUDGET_SUBSET_GLYF;
hb_subset_context_t (hb_blob_t *source_blob_,
+97
View File
@@ -94,6 +94,102 @@ test_subset_crash (void)
hb_face_destroy (face);
}
static char *
_put16 (char *p, unsigned v)
{
*p++ = (char) (v >> 8);
*p++ = (char) (v & 0xFF);
return p;
}
/* A GSUB with one reverse-chaining lookup whose backtrack_count backtrack
* offsets all point at one Coverage holding the single range 0..range_last,
* in a face declaring 65535 glyphs. Each offset costs two bytes but makes
* the subsetter walk the whole range. */
static hb_face_t *
_create_shared_coverage_face (unsigned backtrack_count, unsigned range_last)
{
static const char cmap_data[] = {
0, 0, 0, 1, 0, 3, 0, 1, 0, 0, 0, 12,
0, 6, 0, 12, 0, 0, 0, 'a', 0, 1, 0, 1, /* format 6: 'a' -> glyph 1 */
};
static const char maxp_data[] = { 0, 0, 0x50, 0, (char) 0xFF, (char) 0xFF };
unsigned subtable_length = 12 + 2 * backtrack_count + 6 + 10;
unsigned gsub_length = 10 + 20 + 14 + 4 + 8 + subtable_length;
char *gsub = g_malloc (gsub_length);
char *p = gsub;
p = _put16 (p, 1); p = _put16 (p, 0);
p = _put16 (p, 10); p = _put16 (p, 30); p = _put16 (p, 44);
/* ScriptList: DFLT, default LangSys using feature 0. */
p = _put16 (p, 1); memcpy (p, "DFLT", 4); p += 4; p = _put16 (p, 8);
p = _put16 (p, 4); p = _put16 (p, 0);
p = _put16 (p, 0); p = _put16 (p, 0xFFFF); p = _put16 (p, 1); p = _put16 (p, 0);
/* FeatureList: calt, using lookup 0. */
p = _put16 (p, 1); memcpy (p, "calt", 4); p += 4; p = _put16 (p, 8);
p = _put16 (p, 0); p = _put16 (p, 1); p = _put16 (p, 0);
/* LookupList: one type 8 lookup with one subtable. */
p = _put16 (p, 1); p = _put16 (p, 4);
p = _put16 (p, 8); p = _put16 (p, 0); p = _put16 (p, 1); p = _put16 (p, 8);
/* ReverseChainSingleSubstFormat1 substituting glyph 1 by itself. */
p = _put16 (p, 1); p = _put16 (p, 12 + 2 * backtrack_count);
p = _put16 (p, backtrack_count);
for (unsigned i = 0; i < backtrack_count; i++)
p = _put16 (p, 12 + 2 * backtrack_count + 6);
p = _put16 (p, 0);
p = _put16 (p, 1); p = _put16 (p, 1);
p = _put16 (p, 1); p = _put16 (p, 1); p = _put16 (p, 1);
p = _put16 (p, 2); p = _put16 (p, 1); p = _put16 (p, 0); p = _put16 (p, range_last); p = _put16 (p, 0);
g_assert_cmpuint (p - gsub, ==, gsub_length);
hb_face_t *builder = hb_face_builder_create ();
hb_blob_t *blob = hb_blob_create (gsub, gsub_length, HB_MEMORY_MODE_WRITABLE, gsub, g_free);
hb_face_builder_add_table (builder, HB_TAG ('G','S','U','B'), blob);
hb_blob_destroy (blob);
HB_FACE_ADD_TABLE (builder, "cmap", cmap_data);
HB_FACE_ADD_TABLE (builder, "maxp", maxp_data);
blob = hb_face_reference_blob (builder);
hb_face_t *face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
hb_face_destroy (builder);
return face;
}
static void
test_subset_shared_coverage_budget (void)
{
/* Walking 65535 glyphs per offset must run out of the per-table work
* budget (HB_BUDGET_SUBSET_GLYF) at 8192 offsets, but not at 256. */
static const struct { unsigned backtrack_count, range_last; hb_bool_t expected; } cases[] = {
{ 8192, 1, TRUE },
{ 256, 65534, TRUE },
{ 8192, 65534, FALSE },
};
for (unsigned i = 0; i < G_N_ELEMENTS (cases); i++)
{
hb_face_t *face = _create_shared_coverage_face (cases[i].backtrack_count, cases[i].range_last);
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
hb_set_add (hb_subset_input_unicode_set (input), 'a');
hb_face_t *subset = hb_subset_or_fail (face, input);
g_assert_true (!subset == !cases[i].expected);
if (subset)
{
hb_blob_t *gsub = hb_face_reference_table (subset, HB_TAG ('G','S','U','B'));
g_assert_cmpuint (hb_blob_get_length (gsub), >, 0);
hb_blob_destroy (gsub);
}
hb_subset_input_destroy (input);
hb_face_destroy (subset);
hb_face_destroy (face);
}
}
#ifndef HB_NO_VAR
static void
assert_lookup_variations_shape (hb_face_t *face,
@@ -1706,6 +1802,7 @@ main (int argc, char **argv)
hb_test_add (test_subset_32_tables);
hb_test_add (test_subset_no_inf_loop);
hb_test_add (test_subset_crash);
hb_test_add (test_subset_shared_coverage_budget);
#ifndef HB_NO_VAR
hb_test_add (test_subset_lookup_variations);
hb_test_add (test_subset_lookup_variation_fractional_value);