From d2202126e242bf99fa727e2f84674255dc1e713f Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Fri, 23 Oct 2026 23:01:13 +0530 Subject: [PATCH] Read 24-bit coverage offsets in ContextFormat3_6::depend ContextFormat3_6 (context format 6) stores its input Coverage offsets as Offset24, but depend() passed them to context_depend_lookup() cast to HBUINT16, so every input position after the first decoded the wrong bytes as its offset. intersects_coverage() then followed that value to a Coverage table sanitize never checked. With small offsets the misread value is 0, which points at the subtable header, and the nested lookup's dependencies are silently dropped. A crafted rule can instead point the misread offset at bytes shaped like a large Coverage, and CoverageFormat1_3::intersects() reads past the end of the GSUB blob (ASan heap-buffer-overflow via hb_subset_depend_from_face_or_fail). The other ContextFormat3_6 methods pass coverageZ.arrayZ + 1 as is and let glyph_data_t pick the width. Do the same here. ChainContextFormat3 has the same cast but only has a 16-bit instantiation. Add a format 6 font and test to test-ot-depend.c; it fails without the fix. Tested with meson test under ASan+UBSan with -Dexperimental_api=true. Assisted-by: Claude --- src/hb-ot-layout-gsubgpos.hh | 2 +- test/api/fonts/context-format6.ttf | Bin 0 -> 160 bytes test/api/generate-context-format6-font.py | 81 ++++++++++++++++++++++ test/api/test-ot-depend.c | 18 +++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 test/api/fonts/context-format6.ttf create mode 100644 test/api/generate-context-format6-font.py diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 946731017..2d3d9d851 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -4057,7 +4057,7 @@ struct ContextFormat3_6 nullptr /* intersected_glyphs_cache */ }; context_depend_lookup (c, - glyphCount, (const HBUINT16 *) (coverageZ.arrayZ + 1), + glyphCount, coverageZ.arrayZ + 1, lookupCount, lookupRecord, this, coverageZ[0], lookup_context); diff --git a/test/api/fonts/context-format6.ttf b/test/api/fonts/context-format6.ttf new file mode 100644 index 0000000000000000000000000000000000000000..e2b741b8144aaddfc0df0b41166fe066f6bb2bdc GIT binary patch literal 160 zcmZQzWME)mVo+cJ((b{bPCy 3 dependency is only found by recursing through the format 6 rule. +""" +from pathlib import Path +import struct + +OUT = Path(__file__).parent / "fonts" / "context-format6.ttf" + + +def u16(v): + return struct.pack(">H", v) + + +def u24(v): + return struct.pack(">I", v)[1:] + + +def u32(v): + return struct.pack(">I", v) + + +def coverage(*glyphs): + return u16(1) + u16(len(glyphs)) + b"".join(u16(g) for g in glyphs) + + +# ContextFormat6: format, glyphCount, lookupCount, Offset24 coverages, +# then LookupRecords. +context_header_len = 6 + 2 * 3 + 4 +cov0 = coverage(1) +cov1 = coverage(2) +context = (u16(6) + u16(2) + u16(1) + + u24(context_header_len) + + u24(context_header_len + len(cov0)) + + u16(1) + u16(1) # sequenceIndex 1 -> lookup 1 + + cov0 + cov1) + +# SingleSubstFormat1: 2 -> 3. +single = u16(1) + u16(6) + u16(1) + coverage(2) + + +def lookup(lookup_type, subtable): + return u16(lookup_type) + u16(0) + u16(1) + u16(8) + subtable + + +lookups = [lookup(5, context), lookup(1, single)] +lookup_list = u16(len(lookups)) +offset = 2 + 2 * len(lookups) +for l in lookups: + lookup_list += u16(offset) + offset += len(l) +lookup_list += b"".join(lookups) + +feature_list = u16(1) + b"liga" + u16(8) + u16(0) + u16(1) + u16(0) +lang_sys = u16(0) + u16(0xFFFF) + u16(1) + u16(0) +script_list = u16(1) + b"DFLT" + u16(8) + u16(4) + u16(0) + lang_sys + +script_list_offset = 10 +feature_list_offset = script_list_offset + len(script_list) +lookup_list_offset = feature_list_offset + len(feature_list) +gsub = (u16(1) + u16(0) + + u16(script_list_offset) + + u16(feature_list_offset) + + u16(lookup_list_offset) + + script_list + feature_list + lookup_list) + +maxp = u32(0x00005000) + u16(4) + +tables = sorted([(b"GSUB", gsub), (b"maxp", maxp)]) +font = u32(0x00010000) + u16(len(tables)) + u16(32) + u16(1) + u16(0) +data = b"" +offset = 12 + 16 * len(tables) +for tag, table in tables: + font += tag + u32(0) + u32(offset + len(data)) + u32(len(table)) + data += table + b"\0" * (-len(table) % 4) +OUT.write_bytes(font + data) diff --git a/test/api/test-ot-depend.c b/test/api/test-ot-depend.c index 7944402f8..a442c20fe 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -604,6 +604,23 @@ test_depend_varc (void) hb_face_destroy (face); } +/* ContextSubst format 6 (beyond-64k) uses 24-bit Coverage offsets. + * Lookup 0 matches glyphs 1 2 and calls lookup 1 (SingleSubst 2 -> 3) + * at sequence index 1. */ +static void +test_depend_context_format6 (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/context-format6.ttf"); + hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face); + g_assert_nonnull (depend); + + g_assert_true (find_dependency (depend, 2, 3, HB_OT_TAG_GSUB, + NULL, NULL)); + + hb_subset_depend_destroy (depend); + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -615,6 +632,7 @@ main (int argc, char **argv) hb_test_add (test_depend_math); hb_test_add (test_depend_varc); hb_test_add (test_depend_gsub_formats); + hb_test_add (test_depend_context_format6); return hb_test_run (); }