From 5ef8ef9281ba6923ed08bb7f44224ff0bec42d06 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:24:33 -0600 Subject: [PATCH] [kerx] Charge format-4 control-point actions' contour-point lookups to max_ops A kerx format-4 control-point action asks the font for two glyph contour points, which the font functions may satisfy by loading the whole glyph outline (two full FT_Load_Glyph calls with hb-ft). The state-machine driver charges a non-advancing transition only one op, so a looping entry turned the whole work budget into glyph loads: two letters and a 680-byte font stalled hb_shape() for seconds. Charge 2 * HB_BUDGET_256 to buffer->max_ops at the action, before the callbacks, using the same decrement-and-return idiom morx insertion uses; once the budget is spent the action is skipped and the driver's existing max_ops check forces the machine forward. The sibling GPOS AnchorFormat2 contour-point path has the same uncharged callback but lives in the layout subsystem and is left for a separate change. GDEF CaretValueFormat2 also calls it but only from hb_ot_layout_get_ligature_carets, bounded per API call. Regression test in test/api/test-shape.c uses a counting glyph_contour_point callback on a builder-made looping kerx font (131074 calls before, <= 256 after). Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EE451WE1. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-kerx-table.hh | 4 ++ test/api/test-shape.c | 85 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/hb-aat-layout-kerx-table.hh b/src/hb-aat-layout-kerx-table.hh index f8e2987e7..7b22098f2 100644 --- a/src/hb-aat-layout-kerx-table.hh +++ b/src/hb-aat-layout-kerx-table.hh @@ -613,6 +613,10 @@ struct KerxSubTableFormat4 const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2]; if (!c->sanitizer.check_array (data, 2)) return; hb_barrier (); + /* Font functions may load the whole glyph outline to find a contour + * point, and a non-advancing transition can repeat that at will. + * Charge the two lookups to the buffer's work budget. */ + if (unlikely ((buffer->max_ops -= 2 * HB_BUDGET_256) <= 0)) return; unsigned int markControlPoint = *data++; unsigned int currControlPoint = *data++; hb_position_t markX = 0; diff --git a/test/api/test-shape.c b/test/api/test-shape.c index 37571e18e..a7e706dba 100644 --- a/test/api/test-shape.c +++ b/test/api/test-shape.c @@ -242,6 +242,88 @@ test_shape_overlong_cluster_remerge (void) } + +#ifndef HB_NO_AAT +static hb_bool_t +count_contour_point_calls (hb_font_t *font HB_UNUSED, + void *font_data, + hb_codepoint_t glyph HB_UNUSED, + unsigned int point_index HB_UNUSED, + hb_position_t *x, + hb_position_t *y, + void *user_data HB_UNUSED) +{ + (*(unsigned *) font_data)++; + *x = *y = 0; + return TRUE; +} + +static void +test_shape_kerx_control_point_actions_are_charged (void) +{ + /* A two-glyph font whose kerx format-4 subtable loops on the letter 'a' + * in state 1 without advancing, performing a control-point action on + * every transition. Each action asks the font for two contour points, + * which may load a whole glyph outline, so shaping must charge them to + * the buffer's work budget. */ + 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}; + static const unsigned char kerx[] = { + 0, 2, 0, 0, 0, 0, 0, 1, /* version 2, one subtable */ + 0, 0, 0, 82, 0, 0, 0, 4, 0, 0, 0, 0, /* length, format 4, tupleCount */ + 0, 0, 0, 5, 0, 0, 0, 20, 0, 0, 0, 28, 0, 0, 0, 48, /* nClasses, class, state, entry offsets */ + 0, 0, 0, 66, /* action type 0, control points at 66 */ + 0, 8, 0, 1, 0, 1, 0, 4, /* class lookup format 8: glyph 1 is class 4 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* state 0: class 4 takes entry 1 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, /* state 1: class 4 takes entry 2 */ + 0, 0, 0x00, 0, 0xFF, 0xFF, /* entry 0: to state 0 */ + 0, 1, 0xC0, 0, 0xFF, 0xFF, /* entry 1: to state 1, Mark, DontAdvance */ + 0, 1, 0x40, 0, 0, 0, /* entry 2: to state 1, DontAdvance, action 0 */ + 0, 0, 0, 0 /* action 0: mark point 0, current point 0 */ + }; + static const char *shapers[] = {"ot", NULL}; + + 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 ((const char *) kerx, sizeof (kerx), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('k','e','r','x'), blob); + hb_blob_destroy (blob); + blob = hb_face_reference_blob (builder); + hb_face_destroy (builder); + hb_face_t *face = hb_face_create (blob, 0); + hb_blob_destroy (blob); + + unsigned calls = 0; + hb_font_t *parent = hb_font_create (face); + hb_font_t *font = hb_font_create_sub_font (parent); + hb_font_funcs_t *funcs = hb_font_funcs_create (); + hb_font_funcs_set_glyph_contour_point_func (funcs, count_contour_point_calls, NULL, NULL); + hb_font_set_funcs (font, funcs, &calls, NULL); + hb_font_funcs_destroy (funcs); + + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape_full (font, buffer, NULL, 0, shapers); + + /* 131074 before the budget was charged. */ + g_assert_cmpuint (calls, >=, 2); + g_assert_cmpuint (calls, <=, 256); + + hb_buffer_destroy (buffer); + hb_font_destroy (font); + hb_font_destroy (parent); + hb_face_destroy (face); +} +#endif + static void test_shape_harfrust_budget (void) { @@ -315,6 +397,9 @@ main (int argc, char **argv) hb_test_add (test_shape_overlong_mark_cluster); hb_test_add (test_shape_overlong_cluster_remerge); hb_test_add (test_shape_harfrust_budget); +#ifndef HB_NO_AAT + hb_test_add (test_shape_kerx_control_point_actions_are_charged); +#endif /* TODO test fallback shaper */ /* TODO test shaper_full */ hb_test_add (test_shape_list);