From be66883e29ec384c1215417f9fc2ae37bf96416f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 12 Nov 2026 18:20:28 -0600 Subject: [PATCH] [repacker] Validate ligature array link positions and widths The cached maps divided byte positions by two without checking alignment. An odd-positioned graph link could be mistaken for a valid Offset16 element and then be moved or edited as that element. The nested Ligature array map also lacked an array-index bounds check. Reject misaligned, widened, and out-of-array links in both maps before using the cached ids. Add malformed graphs that pass the general graph checks but fail these table-specific checks; they fail before the fix. Testing: meson test -C build (303 passed, 1 skipped). Assisted-by: OpenAI Codex --- src/graph/ligature-graph.hh | 13 +++++++++--- src/test-repacker.cc | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/graph/ligature-graph.hh b/src/graph/ligature-graph.hh index 569dda394..b1a0e0b4c 100644 --- a/src/graph/ligature-graph.hh +++ b/src/graph/ligature-graph.hh @@ -124,8 +124,11 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 for (const auto& l : c.graph.vertices_[this_index].obj ().real_links) { if (l.position < base_size) continue; unsigned array_index = (l.position - base_size) / SmallTypes::size; - if (likely (array_index < map.length)) - map[array_index] = l.objidx; + if (unlikely (l.width != SmallTypes::size || + (l.position - base_size) % SmallTypes::size || + array_index >= map.length)) + return Err(INVALID_ARGUMENT); + map[array_index] = l.objidx; } return map; } @@ -161,7 +164,11 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1 for (const auto& l : liga_set.vertex->obj ().real_links) { if (l.position < 2) continue; - unsigned array_index = (l.position - 2) / 2; + unsigned array_index = (l.position - 2) / SmallTypes::size; + if (unlikely (l.width != SmallTypes::size || + (l.position - 2) % SmallTypes::size || + array_index >= map.length)) + return Err(INVALID_ARGUMENT); map[array_index] = l.objidx; } return map; diff --git a/src/test-repacker.cc b/src/test-repacker.cc index b664140c4..dbb44d6bc 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -3718,6 +3718,47 @@ static void test_pair_pos_format_2_sanitize_size_overflow () hb_always_assert (!pair_pos->sanitize (vertex).is_ok ()); } +static void test_liga_split_invalid_array_links () +{ + // These positions fit the vertices and do not overlap other links, but + // they do not represent Offset16 array elements. Reject before mutation. + for (unsigned variant = 0; variant < 4; variant++) + { + char buffer[200]; + hb_serialize_context_t c (buffer, sizeof (buffer)); + c.start_serialize (); + unsigned coverage = add_coverage (0, 0, &c); + unsigned liga = add_object ("ligature", 8, &c); + add_liga_set_header (1, &c); + add_offset (liga, &c); + extend ("x", 1, &c); // room for a misaligned or widened link + unsigned set = c.pop_pack (false); + add_liga_header (coverage, 1, &c); + add_offset (set, &c); + extend ("x", 1, &c); + unsigned subtable = c.pop_pack (false); + start_lookup (4, 1, &c); + add_offset (subtable, &c); + unsigned lookup = finish_lookup (&c); + unsigned lookup_list = add_lookup_list (&lookup, 1, &c); + add_gsubgpos_header (lookup_list, &c); + c.end_serialize (); + + // Modify serializer indices before graph creation removes the nil node. + auto& links = c.object_graph ()[variant < 2 ? subtable : set]->real_links; + auto& link = links[links.length - 1]; + if (variant % 2 == 0) link.position++; + else link.width++; + + graph_t graph = graph_t::create (c.object_graph ()).value (); + auto context = graph::gsubgpos_graph_context_t::create (HB_OT_TAG_GSUB, graph).value (); + unsigned lookup_id = *context.lookups.keys (); + auto result = context.lookups.get (lookup_id)->split_subtables_if_needed (context, lookup_id); + hb_always_assert (result.is_err ()); + hb_always_assert (result.error () == graph::INVALID_ARGUMENT); + } +} + static void test_invalid_link_position () { size_t buffer_size = 100; @@ -3752,6 +3793,7 @@ main (int argc, char **argv) test_invalid_link_objidx (); test_invalid_link_add_and_move (); test_invalid_link_position (); + test_liga_split_invalid_array_links (); test_serialize (); test_sort_shortest (); test_will_overflow_1 ();