From 79146f902ecc8ff7523ada4a1ffeb5b001c57e8d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 15:43:37 -0600 Subject: [PATCH] [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 Co-Authored-By: Claude Opus 5.5 --- src/hb-limits.hh | 8 ++++++ src/hb-ot-layout-gsubgpos.hh | 23 ++++++++++++++++-- test/api/test-ot-depend.c | 13 ++++++++++ .../gsub-context-lookup-permutations.ttf | Bin 0 -> 262 bytes 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 test/fuzzing/fonts/gsub-context-lookup-permutations.ttf diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 319418746..4a8a057a7 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -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. */ diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 8a47ef7e2..724c33c3a 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -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_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> recurse_sets; diff --git a/test/api/test-ot-depend.c b/test/api/test-ot-depend.c index 8aae59bed..d6430a782 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -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); diff --git a/test/fuzzing/fonts/gsub-context-lookup-permutations.ttf b/test/fuzzing/fonts/gsub-context-lookup-permutations.ttf new file mode 100644 index 0000000000000000000000000000000000000000..76216728707af761ae41a75f3be1c4afb78e1982 GIT binary patch literal 262 zcmXYsF$=;#428dIwbpZS>?9pKICSh-M6`oLL4}Sw2rhzP2Y;u3s3vzgcznsdz)NsI zB9eDJUw5%$EBw9g-*@bFhzx~iBd*&YP8V`!YD`mb-1zv6@<-@a6qHodG_-W|4BU7! y5B^~6jh(SIHpbdm8B1efTpM#zN%fKz)AgcMr2A=NCR&JeyQRp>bT|rGjN}iHKo2Vb literal 0 HcmV?d00001