[aat] Reject kern/kerx/mort/morx tables declaring too many subtables

The kern/kerx accelerator keeps two hb_bit_set_t per subtable and
mort/morx keeps one per subtable, all built eagerly when the shape
plan is compiled and held for the life of the face.  A 14-byte kern
format-3 subtable fills both sets with every glyph, so heap use was
about 2100x the font size with nothing bounding the subtable count;
morx is worse because overlapping chains can claim each other's
subtables.  A ~560 KB kern font drove shaping one letter to 1.1 GiB.

Add HB_AAT_MAX_SUBTABLES (1024) to hb-limits.hh and enforce it in
sanitize, the way GSUB/GPOS already bound their subtables via
visit_subtables (whose 0x4000 limit is too loose here -- 16383 kern
subtables still peak at hundreds of MiB).  KerxTable::sanitize()
(shared by kerx, OpenType kern and Apple kern) fails when tableCount
exceeds the limit, and mortmorx::sanitize() fails once the chains'
subtable counts add up past it.  Over-limit tables are dropped
entirely (the font shapes without kerning/morx); real fonts under
test/ declare at most three kern/kerx subtables and one morx chain.

Regression test in test/api/test-aat-layout.c builds kern and morx
faces with hb_face_builder and checks both sides of the limit.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-5N52Y0UN.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-02 10:51:40 -06:00
1 parent 843b32d48c
commit cf9889b73f
4 files changed
+148

No files matched your search

+3
View File
@@ -1141,6 +1141,9 @@ struct KerxTable
const SubTable *st = &thiz()->firstSubTable;
unsigned int count = thiz()->tableCount;
/* The accelerator keeps two glyph sets per subtable. */
if (unlikely (count > HB_AAT_MAX_SUBTABLES))
return_trace (false);
for (unsigned int i = 0; i < count; i++)
{
if (unlikely (!st->u.header.sanitize (c)))
+5
View File
@@ -1471,11 +1471,16 @@ struct mortmorx
const Chain<Types> *chain = &firstChain;
unsigned int count = chainCount;
unsigned int subtable_count = 0;
for (unsigned int i = 0; i < count; i++)
{
if (!chain->sanitize (c, version))
return_trace (false);
hb_barrier ();
/* The accelerator keeps a glyph set per subtable. */
subtable_count += chain->get_subtable_count ();
if (unlikely (subtable_count > HB_AAT_MAX_SUBTABLES))
return_trace (false);
chain = &StructAfter<Chain<Types>> (*chain);
}
+7
View File
@@ -92,6 +92,13 @@
#define HB_MAX_LOOKUP_VISIT_COUNT 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. */
#ifndef HB_AAT_MAX_SUBTABLES
#define HB_AAT_MAX_SUBTABLES 1024
#endif
#ifndef HB_MAX_GRAPH_EDGE_COUNT
#define HB_MAX_GRAPH_EDGE_COUNT 16384
#endif
+133
View File
@@ -116,6 +116,137 @@ test_aat_has (void)
hb_face_destroy (trak);
}
static hb_bool_t
count_started_subtables (hb_buffer_t *buffer HB_UNUSED,
hb_font_t *font HB_UNUSED,
const char *message,
void *user_data)
{
if (!strncmp (message, "start subtable ", 15) ||
!strncmp (message, "start chainsubtable ", 20))
(*(unsigned *) user_data)++;
return true;
}
/* Shapes "aa" with a two-glyph font that has the table, which is
* freed; returns the number of its subtables that were applied.
* A table with more than HB_AAT_MAX_SUBTABLES subtables is rejected
* by sanitize, so none of them is. */
static unsigned
count_applied_subtables (hb_tag_t tag, char *table, unsigned length)
{
static const char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x02};
static const char cmap[] = {0, 0, 0, 1, 0, 3, 0, 10, 0, 0, 0, 12,
0, 12, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'a', 0, 0, 0, 'a', 0, 0, 0, 1};
hb_face_t *builder = hb_face_builder_create ();
hb_blob_t *blob = hb_blob_create (maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL);
hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob);
hb_blob_destroy (blob);
blob = hb_blob_create (cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL);
hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob);
hb_blob_destroy (blob);
blob = hb_blob_create (table, length, HB_MEMORY_MODE_READONLY, table, free);
hb_face_builder_add_table (builder, tag, blob);
hb_blob_destroy (blob);
blob = hb_face_reference_blob (builder);
hb_face_destroy (builder);
hb_face_t *limit_face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
hb_font_t *font = hb_font_create (limit_face);
hb_face_destroy (limit_face);
unsigned started = 0;
hb_buffer_t *buffer = hb_buffer_create ();
hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
hb_buffer_set_message_func (buffer, count_started_subtables, &started, NULL);
hb_shape (font, buffer, NULL, 0);
hb_buffer_destroy (buffer);
hb_font_destroy (font);
return started;
}
/* A kern table of format-3 subtables. */
static char *
make_kern_table (unsigned num_subtables, unsigned *length)
{
*length = 4 + 14 * num_subtables;
char *kern = (char *) calloc (*length, 1);
kern[2] = (char) (num_subtables >> 8);
kern[3] = (char) (num_subtables & 0xFF);
for (unsigned i = 0; i < num_subtables; i++)
{
char *subtable = kern + 4 + 14 * i;
subtable[3] = 14; /* length */
subtable[4] = 3; /* format */
subtable[5] = 1; /* coverage: horizontal */
subtable[7] = 1; /* glyphCount */
}
return kern;
}
static void
test_aat_kern_subtable_limit (void)
{
unsigned length;
char *kern;
kern = make_kern_table (1024, &length);
g_assert_cmpuint (1024, ==, count_applied_subtables (HB_TAG ('k','e','r','n'), kern, length));
kern = make_kern_table (1025, &length);
g_assert_cmpuint (0, ==, count_applied_subtables (HB_TAG ('k','e','r','n'), kern, length));
}
/* A morx table of chains of five noncontextual subtables each. */
static char *
make_morx_table (unsigned num_chains, unsigned *length)
{
*length = 8 + (16 + 5 * 20) * num_chains;
char *morx = (char *) calloc (*length, 1);
morx[1] = 2; /* version */
morx[6] = (char) (num_chains >> 8);
morx[7] = (char) (num_chains & 0xFF);
for (unsigned i = 0; i < num_chains; i++)
{
char *chain = morx + 8 + (16 + 5 * 20) * i;
chain[3] = 1; /* defaultFlags */
chain[7] = 16 + 5 * 20; /* length */
chain[15] = 5; /* subtableCount */
for (unsigned j = 0; j < 5; j++)
{
char *subtable = chain + 16 + 20 * j;
subtable[3] = 20; /* length */
subtable[4] = 0x20; /* coverage: all directions */
subtable[7] = 4; /* type */
subtable[11] = 1; /* subFeatureFlags */
subtable[13] = 8; /* lookup format */
subtable[15] = 1; /* firstGlyph */
subtable[17] = 1; /* glyphCount */
subtable[19] = 1; /* value */
}
}
return morx;
}
static void
test_aat_morx_subtable_limit (void)
{
unsigned length;
char *morx;
morx = make_morx_table (204, &length); /* 1020 subtables */
g_assert_cmpuint (1020, ==, count_applied_subtables (HB_TAG ('m','o','r','x'), morx, length));
morx = make_morx_table (205, &length); /* 1025 subtables */
g_assert_cmpuint (0, ==, count_applied_subtables (HB_TAG ('m','o','r','x'), morx, length));
}
#endif
int
@@ -128,6 +259,8 @@ main (int argc, char **argv)
hb_test_add (test_aat_get_feature_types);
hb_test_add (test_aat_get_feature_selectors);
hb_test_add (test_aat_has);
hb_test_add (test_aat_kern_subtable_limit);
hb_test_add (test_aat_morx_subtable_limit);
#endif
face = hb_test_open_font_file ("fonts/aat-feat.ttf");