[subset] Bound avar2 offset-compensation's quadratic breakpoint work

avar::_subset_avar2() builds the offset-compensation knots and runs
_avar2_estimate_offset_error() twice per narrowed axis; every
breakpoint and every estimator sample is looked up in the old
segment map, the instantiated mapping and the knots by linear
search, so the cost is quadratic in the axis's avar breakpoints and
nothing bounded it (42 s of CPU for a 131 KB font, re-run on each
serializer out-of-room retry).

Add HB_BUDGET_SUBSET_TABLE to hb-limits.hh (same hb_budget_spend()
idiom as the rendering budgets) and, right before the knot loop and
the estimator, charge (256 + breakpoints) * 8 * breakpoints from a
budget shared by all axes of the table.  Charging at the point where
the quadratic work starts covers the O(n^2) knot-collection loop and
its map_float() calls too.  On exhaustion the table fails through
c->serializer->check_success(), so the whole subset fails
(hb_subset_or_fail() returns NULL) rather than avar being silently
dropped from an avar2-instanced font.

The budget is 1<<30: a 5,000-breakpoint axis narrowed to -900:900 is
still admitted (0.6 s), while 16,385 and 32,769 breakpoints are
rejected in 0.01 s (the 42 s DoS).  A reviewer may tighten it; real
avar maps have a handful of breakpoints, far below the cap.

Adds test_avar2_restrict_many_breakpoints to test/api/test-subset-avar2.c.

This caps the cost; it does not remove the quadratic algorithm.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-10 16:41:56 -06:00
1 parent 21b4457853
commit d35a224377
3 files changed
+99

No files matched your search

+7
View File
@@ -256,6 +256,13 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
#define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28)
#endif
/* The work one table may spend on being subset beyond what is proportional
* to its size. Limiting an axis of an avar2 font is charged the square of
* the axis's avar breakpoints. */
#ifndef HB_BUDGET_SUBSET_TABLE
#define HB_BUDGET_SUBSET_TABLE ((int64_t) 1 << 30)
#endif
/* The flat pixel-work floor for one raster draw or paint session. Pixel and
* outline work use separate live counters, so keep this independent of
* the common outline budget above. */
+16
View File
@@ -937,6 +937,9 @@ struct avar
* Track processed (outer,inner) pairs to avoid adding duplicate biases
* when multiple axes share the same varIdx. */
hb_set_t processed_varidxes;
/* All axes share the budget. A retry with a larger buffer, which the
* added regions make likely, starts afresh. */
int64_t estimate_budget = HB_BUDGET_SUBSET_TABLE;
for (unsigned i = 0; i < axisCount; i++)
{
hb_tag_t *axis_tag_ptr;
@@ -1033,6 +1036,19 @@ struct avar
if (-1.0 < z_old && z_old < 1.0)
_avar2_add_knot (knots, z_old, -z_old);
/* From here on every breakpoint, and every sample of the error
* estimator, which takes a few per breakpoint on top of its
* uniform grid, is looked up in the old mapping, the new mapping
* and the knots by linear search. Charge that quadratic work
* before doing it; failing the table fails the subset, which
* beats silently dropping avar from an avar2-instanced font. */
unsigned breakpoints = old_seg_maps[i]->len + new_mapping.length;
if (unlikely (!c->serializer->check_success (
hb_budget_spend (estimate_budget,
HB_BUDGET_256 + breakpoints,
HB_BUDGET_8 * breakpoints))))
return false;
/* Interior avar v1 breakpoints inside the retained range each put
* a kink in offset(z). Sampling only {-1, 0, +1, z_old} would
* linearly interpolate across those kinks. The instantiated
+76
View File
@@ -684,6 +684,81 @@ test_avar2_restrict_steep_no_varidx (void)
hb_face_destroy (face);
}
/* Replace the test font's avar with a version 2 table that has no VarIdxMap
* and no VarStore, and gives the first axis, wght, an identity mapping with
* a breakpoint every step F2Dot14 units. */
typedef struct
{
hb_face_t *source;
unsigned step;
} breakpoints_t;
static hb_blob_t *
reference_table_with_breakpoints (hb_face_t *face HB_UNUSED,
hb_tag_t tag,
void *user_data)
{
const breakpoints_t *breakpoints = (const breakpoints_t *) user_data;
if (tag != HB_TAG ('a','v','a','r'))
return hb_face_reference_table (breakpoints->source, tag);
unsigned map_count = 2 * 16384 / breakpoints->step + 1;
unsigned length = 8 + (2 + map_count * 4) + 5 * 2 + 8;
unsigned char *data = (unsigned char *) calloc (length, 1);
g_assert_nonnull (data);
data[1] = 2; /* major version */
data[7] = 6; /* axisCount */
data[8] = map_count >> 8;
data[9] = map_count & 0xFF;
for (unsigned i = 0; i < map_count; i++)
{
unsigned char *p = data + 10 + i * 4;
unsigned coord = (unsigned) ((int) (i * breakpoints->step) - 16384);
p[0] = p[2] = (coord >> 8) & 0xFF;
p[1] = p[3] = coord & 0xFF;
}
return hb_blob_create ((const char *) data, length,
HB_MEMORY_MODE_WRITABLE, data, free);
}
/* Offset compensation takes time quadratic in the breakpoints of the
* restricted axis. It must cope with hundreds of them, but give up on tens
* of thousands rather than spend a minute. */
static void
test_avar2_restrict_many_breakpoints (void)
{
hb_face_t *source = open_original ();
breakpoints_t many = {source, 64};
hb_face_t *face = hb_face_create_for_tables (reference_table_with_breakpoints,
&many, NULL);
hb_subset_input_t *input = create_input ();
g_assert_true (hb_subset_input_set_axis_range (input, face, WGHT, 200.f, 700.f, 400.f));
hb_face_t *inst = instance (face, input);
for (unsigned i = 0; i < 9; i++)
{
hb_variation_t vars[1] = {{WGHT, lerp (200.f, 700.f, i, 9)}};
check_same_coords (face, inst, vars, 1, 2);
}
hb_face_destroy (inst);
hb_face_destroy (face);
breakpoints_t too_many = {source, 1};
face = hb_face_create_for_tables (reference_table_with_breakpoints,
&too_many, NULL);
input = create_input ();
g_assert_true (hb_subset_input_set_axis_range (input, face, WGHT, 200.f, 700.f, 400.f));
g_assert_null (hb_subset_or_fail (face, input));
hb_subset_input_destroy (input);
hb_face_destroy (face);
hb_face_destroy (source);
}
int
main (int argc, char **argv)
{
@@ -705,6 +780,7 @@ main (int argc, char **argv)
hb_test_add (test_avar2_culling_aliased_gvar_data);
hb_test_add (test_avar2_restrict_steep);
hb_test_add (test_avar2_restrict_steep_no_varidx);
hb_test_add (test_avar2_restrict_many_breakpoints);
return hb_test_run ();
}