mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-24 08:21:32 +01:00
[depend] Bound lookup recursion depth and whole-table path count
hb_depend_context_t::recurse() only refused to re-enter a lookup already on the call stack, so a chain of distinct Context lookups recursed once per lookup (stack overflow around 8600 lookups on an 8 MB stack) and lookups invoking each other in every order were walked once per ordering (a factorial number of interned paths, gigabytes of memory). recurse() is where every sibling context (closure, closure-lookups, apply) enforces HB_MAX_NESTING_LEVEL, so add the same nesting_level_left guard here; shaping never applies lookups nested deeper, so no edge the closure can reach is lost (a chain's final edge drops at exactly the depth the closure stops, 65->66). Bound the number of distinct recursion paths with a counter kept over the whole table (not cleared by reset_recurse_cache, which runs per top-level lookup) against a new HB_MAX_DEPEND_RECURSE_PATHS limit in hb-limits.hh, so it can be tuned on its own; exceeding it fails the build so hb_subset_depend_from_face_or_fail() returns nullptr instead of emitting a truncated graph. The largest whole-table path count among the test fonts is 744 (NotoNastaliqUrdu-Bold), so 35000 leaves ample headroom. Builds only with experimental_api. Adds a small permutation-font fuzz seed and a test-ot-depend test; test-ot-depend, the depend fuzzer and the depend-closure-parity tests pass. Related to PR #6185. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-N28924W8. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c611a0589d
commit
79146f902e
4 files changed
+42
-2
No files matched your search
@@ -92,6 +92,14 @@
|
||||
#define HB_MAX_LOOKUP_VISIT_COUNT 35000
|
||||
#endif
|
||||
|
||||
/* Distinct lookup recursion paths the dependency graph builder interns
|
||||
* over a whole GSUB; building the graph fails when it needs more.
|
||||
* Lookups that invoke each other in every order make a factorial number
|
||||
* of them. The most among the test fonts is 744 (NotoNastaliqUrdu-Bold). */
|
||||
#ifndef HB_MAX_DEPEND_RECURSE_PATHS
|
||||
#define HB_MAX_DEPEND_RECURSE_PATHS 35000
|
||||
#endif
|
||||
|
||||
/* Subtables of one kern, kerx, mort, or morx table; the table is rejected
|
||||
* when it declares more. The table's accelerator keeps glyph sets for
|
||||
* each, which can take far more memory than the subtable has bytes. */
|
||||
|
||||
@@ -556,7 +556,13 @@ struct hb_depend_context_t :
|
||||
*
|
||||
* lookups_seen is pre-seeded with the top-level lookup index in
|
||||
* GSUB_accelerator_t::depend before the direct depend() call, so
|
||||
* that indirect cycles of the form A→B→A are also caught here. */
|
||||
* that indirect cycles of the form A→B→A are also caught here.
|
||||
*
|
||||
* lookups_seen only cuts cycles; a chain of distinct lookups would
|
||||
* otherwise recurse once per lookup in the table, so also apply the
|
||||
* same nesting limit the closure contexts use. Shaping never applies
|
||||
* lookups nested deeper than this, so no reachable edge is lost. */
|
||||
if (unlikely (!nesting_level_left || !depend_data->successful)) return;
|
||||
if (lookups_seen.has (lookup_idx)) return;
|
||||
lookups_seen.add (lookup_idx);
|
||||
|
||||
@@ -570,7 +576,14 @@ struct hb_depend_context_t :
|
||||
if (!recurse_path_to_index.has (path_key, &path))
|
||||
{
|
||||
path_index = recurse_path_to_index.get_population () + 1;
|
||||
if (unlikely (!recurse_path_to_index.set (path_key, path_index)))
|
||||
/* Every distinct path is walked and memoized separately, so lookups
|
||||
* that invoke each other in every order intern a factorial number of
|
||||
* them. reset_recurse_cache() clears recurse_path_to_index per
|
||||
* top-level lookup, so bound the paths over the whole table with a
|
||||
* separate counter and report the incomplete graph as a failure
|
||||
* rather than emit it. */
|
||||
if (unlikely (++total_recurse_paths > HB_MAX_DEPEND_RECURSE_PATHS ||
|
||||
!recurse_path_to_index.set (path_key, path_index)))
|
||||
{
|
||||
depend_data->fail ();
|
||||
lookups_seen.del (lookup_idx);
|
||||
@@ -580,7 +593,9 @@ struct hb_depend_context_t :
|
||||
else
|
||||
path_index = *path;
|
||||
recurse_path = path_index;
|
||||
nesting_level_left--;
|
||||
recurse_func (this, lookup_idx, covered_seq_indices, seq_index, end_index);
|
||||
nesting_level_left++;
|
||||
recurse_path = parent_path;
|
||||
lookups_seen.del (lookup_idx);
|
||||
}
|
||||
@@ -817,6 +832,10 @@ struct hb_depend_context_t :
|
||||
hb_vector_t<active_glyphs_frame_t> active_glyphs_stack;
|
||||
unsigned active_glyphs_stack_depth = 0;
|
||||
recurse_func_t recurse_func;
|
||||
unsigned nesting_level_left = HB_MAX_NESTING_LEVEL;
|
||||
/* Distinct recursion paths interned over the whole table (not cleared by
|
||||
* reset_recurse_cache), bounding the factorial path blow-up. */
|
||||
unsigned total_recurse_paths = 0;
|
||||
hb_codepoint_t lookup_index = HB_CODEPOINT_INVALID;
|
||||
hb_bit_set_t lookups_seen;
|
||||
hb_vector_t<hb::unique_ptr<hb_set_t>> recurse_sets;
|
||||
|
||||
@@ -795,6 +795,18 @@ test_depend_feature_variations (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
/* A set of GSUB Context lookups that invoke each other in every order interns
|
||||
* a factorial number of recursion paths; the whole-table path cap must refuse
|
||||
* the build rather than exhaust memory. */
|
||||
static void
|
||||
test_depend_gsub_recursion_limit (void)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/gsub-context-lookup-permutations.ttf");
|
||||
hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face);
|
||||
g_assert_null (depend);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
/* ContextSubst format 6 (beyond-64k) uses 24-bit Coverage offsets.
|
||||
* Lookup 0 matches glyphs 1 2 and calls lookup 1 (SingleSubst 2 -> 3)
|
||||
@@ -1110,6 +1122,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_depend_feature_variations);
|
||||
hb_test_add (test_depend_edge_limit);
|
||||
hb_test_add (test_depend_gsub_high_bit_tags);
|
||||
hb_test_add (test_depend_gsub_recursion_limit);
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
hb_test_add (test_depend_context_format6);
|
||||
hb_test_add (test_depend_gsub_context_set_glyphs);
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user