mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 07:21:06 +01:00
[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:
1 parent
7f65c5d820
commit
df02261eca
5 files changed
+125
-5
No files matched your search
@@ -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
@@ -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
|
||||
|
||||
@@ -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
@@ -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_,
|
||||
|
||||
Reference in new issue
Block a user