[depend] Cap the number of stored dependency-graph edges

hb_depend_data_builder_t::add_edge() stored one record per
(source, target, feature tag) with no bound.  In GSUB a lookup
reached by F feature tags multiplies its N*M substitutions by F, so
a lookup whose covered glyphs all share one AlternateSet, selected
by many feature tags, produces N*M*F edges from a font that grows
only with N+M+F.  A 5 KB font yields 33M edges and ~1.4 GB of
builder memory; COLRv0 overlapping layer slices reach the same sink
without any GSUB.  The builder also kept walking after fail(), so an
allocation failure still cost the full N*M*F iterations.

This adds HB_DEPEND_MAX_EDGES (1<<22, about 4M, next to
HB_MAX_GRAPH_EDGE_COUNT in hb-limits.hh and overridable the same
way; 16x the largest graph in the test corpus) and enforces it in
add_edge(), the single choke point every table's extractor stores
through, so hb_subset_depend_from_face_or_fail() returns NULL
instead of building an unbounded graph.  add_edge() and
add_gsub_lookup() return early once the builder has failed, and
GSUB_accelerator_t::depend()'s lookup loop stops as well, matching
the existing !successful checks in the context walkers, so a failed
build does bounded work.

A regression test builds the fan-out font in memory with
hb_face_builder (no binary test font needed) and checks that a
16x16x4 fan-out is recorded in full and that the 256x256x512 one is
refused.  test-ot-depend and the api suite pass on the ASan
experimental build, and the finding's reproducer now returns NULL
in ~1 s.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-10 16:38:50 -06:00
1 parent 7f65c5d820
commit d377bc81f2
4 files changed
+132 -1

No files matched your search

+8
View File
@@ -507,6 +507,9 @@ struct hb_depend_data_builder_t
bool add_edge (hb_codepoint_t source, const hb_depend_edge_t &record)
{
if (unlikely (!successful))
return false;
/* Store only a source and per-glyph edge index in each bucket. The edge
* itself already lives in the output vector, where it can be compared to
* resolve hash collisions exactly. Adding one to the encoded source
@@ -529,6 +532,9 @@ struct hb_depend_data_builder_t
slot = (slot + ++step) & (edge_slots.length - 1);
}
if (unlikely (edge_population >= HB_DEPEND_MAX_EDGES))
return fail ();
auto &dependencies = data.glyph_dependencies[source].dependencies;
unsigned int index = dependencies.length;
if (unlikely (!dependencies.push_or_fail (record)))
@@ -561,6 +567,8 @@ struct hb_depend_data_builder_t
hb_codepoint_t lig_set = HB_CODEPOINT_INVALID,
hb_codepoint_t context_set = HB_CODEPOINT_INVALID)
{
if (unlikely (!successful))
return false;
if (context_set == HB_CODEPOINT_INVALID)
context_set = current_context_set_index;
hb_subset_depend_edge_flags_t flags = current_edge_flags;
+8
View File
@@ -103,6 +103,14 @@
#define HB_MAX_GRAPH_EDGE_COUNT 16384
#endif
/* Maximum number of edges stored in one glyph dependency graph. GSUB
* edges are keyed by (source, target, feature tag), so a lookup reached
* by many feature tags multiplies its substitutions by the tag count and
* a small table can describe a graph of hundreds of millions of edges. */
#ifndef HB_DEPEND_MAX_EDGES
#define HB_DEPEND_MAX_EDGES (1u << 22)
#endif
#ifndef HB_VAR_COMPOSITE_MAX_AXES
#define HB_VAR_COMPOSITE_MAX_AXES 4096
#endif
+1 -1
View File
@@ -183,7 +183,7 @@ GSUB_accelerator_t::depend (hb_depend_data_builder_t *builder, hb_face_t *face)
hb_depend_context_t c (builder, face, &all_glyphs);
for (unsigned i = 0; i < num_lookups; i++)
for (unsigned i = 0; i < num_lookups && builder->successful; i++)
{
auto features = builder->get_lookup_features (i);
if (!features)
+115
View File
@@ -814,6 +814,120 @@ test_depend_context_format6 (void)
}
#endif
static void
append_uint16 (GByteArray *array, unsigned value)
{
guint8 bytes[2] = { value >> 8, value & 0xFF };
g_byte_array_append (array, bytes, 2);
}
/* Build a face whose GSUB has one AlternateSubst lookup in which covered
* glyphs 1..N all share one AlternateSet of glyphs 1..M, selected by F
* distinct feature tags. The dependency graph of such a font has N*M*F
* edges while the font itself grows with only N+M+F. */
static hb_face_t *
create_fanout_face (unsigned covered_glyphs, unsigned alternates, unsigned features)
{
GByteArray *gsub = g_byte_array_new ();
unsigned script_list_len = 18 + 2 * features;
unsigned feature_list_len = 8 + 6 * features;
unsigned i;
/* Header */
append_uint16 (gsub, 1);
append_uint16 (gsub, 0);
append_uint16 (gsub, 10);
append_uint16 (gsub, 10 + script_list_len);
append_uint16 (gsub, 10 + script_list_len + feature_list_len);
/* ScriptList: DFLT, whose default LangSys enables every feature */
append_uint16 (gsub, 1);
g_byte_array_append (gsub, (const guint8 *) "DFLT", 4);
append_uint16 (gsub, 8);
append_uint16 (gsub, 4);
append_uint16 (gsub, 0);
append_uint16 (gsub, 0);
append_uint16 (gsub, 0xFFFF);
append_uint16 (gsub, features);
for (i = 0; i < features; i++)
append_uint16 (gsub, i);
/* FeatureList: F records that all point at one Feature selecting lookup 0 */
append_uint16 (gsub, features);
for (i = 0; i < features; i++)
{
guint8 tag[4] = { 'f', '0' + i / 100 % 10, '0' + i / 10 % 10, '0' + i % 10 };
g_byte_array_append (gsub, tag, 4);
append_uint16 (gsub, 2 + 6 * features);
}
append_uint16 (gsub, 0);
append_uint16 (gsub, 1);
append_uint16 (gsub, 0);
/* LookupList: one AlternateSubstFormat1 lookup */
append_uint16 (gsub, 1);
append_uint16 (gsub, 4);
append_uint16 (gsub, 3);
append_uint16 (gsub, 0);
append_uint16 (gsub, 1);
append_uint16 (gsub, 8);
append_uint16 (gsub, 1);
append_uint16 (gsub, 6 + 2 * covered_glyphs);
append_uint16 (gsub, covered_glyphs);
for (i = 0; i < covered_glyphs; i++)
append_uint16 (gsub, 6 + 2 * covered_glyphs + 10);
/* Coverage format 2: one range, glyphs 1..N */
append_uint16 (gsub, 2);
append_uint16 (gsub, 1);
append_uint16 (gsub, 1);
append_uint16 (gsub, covered_glyphs);
append_uint16 (gsub, 0);
/* The shared AlternateSet: glyphs 1..M */
append_uint16 (gsub, alternates);
for (i = 1; i <= alternates; i++)
append_uint16 (gsub, i);
GByteArray *maxp = g_byte_array_new ();
append_uint16 (maxp, 0);
append_uint16 (maxp, 0x5000);
append_uint16 (maxp, MAX (covered_glyphs, alternates) + 1);
hb_face_t *face = hb_face_builder_create ();
hb_blob_t *blob = hb_blob_create ((const char *) gsub->data, gsub->len,
HB_MEMORY_MODE_READONLY, gsub, (hb_destroy_func_t) g_byte_array_unref);
hb_face_builder_add_table (face, HB_TAG ('G','S','U','B'), blob);
hb_blob_destroy (blob);
blob = hb_blob_create ((const char *) maxp->data, maxp->len,
HB_MEMORY_MODE_READONLY, maxp, (hb_destroy_func_t) g_byte_array_unref);
hb_face_builder_add_table (face, HB_TAG ('m','a','x','p'), blob);
hb_blob_destroy (blob);
return face;
}
/* Test that graph construction fails instead of storing an unbounded number
* of edges when a small font fans out into a huge dependency graph. */
static void
test_depend_edge_limit (void)
{
/* A modest fan-out is recorded in full: 16 * 16 * 4 edges. */
hb_face_t *face = create_fanout_face (16, 16, 4);
hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
g_assert_cmpuint (hb_subset_depend_lookup_glyph (depend, 16, 0, NULL, NULL), ==, 16 * 4);
hb_tag_t layout_tag = HB_TAG ('f','0','0','3');
g_assert_true (find_dependency (depend, 16, 1, HB_TAG ('G','S','U','B'),
&layout_tag, NULL));
hb_subset_depend_destroy (depend);
hb_face_destroy (face);
/* 256 * 256 * 512 edges, from a font of about five kilobytes. */
face = create_fanout_face (256, 256, 512);
depend = hb_subset_depend_from_face_or_fail (face);
g_assert_null (depend);
hb_face_destroy (face);
}
int
main (int argc, char **argv)
{
@@ -827,6 +941,7 @@ main (int argc, char **argv)
hb_test_add (test_depend_varc);
hb_test_add (test_depend_gsub_formats);
hb_test_add (test_depend_feature_variations);
hb_test_add (test_depend_edge_limit);
#ifndef HB_NO_BEYOND_64K
hb_test_add (test_depend_context_format6);
#endif