mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-23 16:01:07 +01:00
Read 24-bit coverage offsets in ContextFormat3_6::depend
ContextFormat3_6<MediumTypes> (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
This commit is contained in:
1 parent
c3fd6a885e
commit
d2202126e2
4 files changed
+100
-1
No files matched your search
@@ -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);
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate a minimal font with a GSUB ContextSubst format 6 lookup.
|
||||
|
||||
Format 6 is the beyond-64k coverage-based context format; its Coverage
|
||||
offsets are 24-bit. Lookup 0 (feature 'liga') matches glyphs 1 2 and
|
||||
calls lookup 1 at sequence index 1. Lookup 1 is a SingleSubst that
|
||||
maps glyph 2 to glyph 3 and is not referenced by any feature, so the
|
||||
2 -> 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)
|
||||
@@ -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 ();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user