mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-26 09:21:06 +01:00
[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
This commit is contained in:
1 parent
f6b7a4cb72
commit
be66883e29
2 files changed
+52
-3
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<char> ();
|
||||
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 ();
|
||||
|
||||
Reference in new issue
Block a user