[CFF2] Charge blend region-scalar and default-value evaluation to the budget (#6637)

* [CFF2] Charge blend region-scalar evaluation to the work budget

cff2_cs_interp_env_t::process_blend() evaluates one VarRegionList scalar
per entry of the VarData's region indices (up to 65535, each walking up
to axisCount axes) the first time a charstring hits `blend`, but the
only budget charge for blend is the operand-proportional one in
cff2_cs_opset_t::process_blend(), which runs afterwards and is skipped
when the operand check fails.  So `1 blend` did regionIndexCount x
axisCount scalar evaluations for one budget unit, and a VARC fan-out
re-ran it tens of thousands of times within 2% of HB_BUDGET_GLYPH.

Charge region_count x max(1, axisCount) through the env's existing
spend_budget() right before resizing and filling the scalars vector,
matching how VARC charges values x coords for its own
ItemVariationStore evaluations; a failed charge errors the interpreter
exactly as a failed resize does.  spend_budget() is a no-op without a
budget, so the subsetter and instancer paths are unchanged, and
seen_blend gates the work to once per charstring run.

Adds test_hb_draw_cff2_blend_region_budget: a CFF2 charstring blending
one value over 256 region indices of a 16-axis region list must consume
at least 256 x 16 units and fail under a 2048-unit budget.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [CFF2] Charge the blend default values to the work budget

cff2_cs_opset_t::process_blend() walks all (k+1)*n values of a blend
(n default values plus k deltas each) but charged only the k*n deltas,
total - n.  With a VarData of regionIndexCount 0 (or no variation
store) k is 0, so `n blend` charges nothing, pops nothing, and the same
operands can be blended again and again for the price of two tokens.

Charge `total` = (k+1)*n, exactly what the loop touches.  spend_budget()
is a no-op without a caller budget, so only budgeted callers (draw,
extents, paint, shape, and the subsetter passes that now pass a budget)
are affected, and a well-formed charstring pays one extra unit per
blended value.

Adds test_hb_draw_cff2_blend_budget: a charstring that pushes 100
numbers and blends them 100 times over a region-less VarData must
consume at least 100 x 100 units and fail under a 5000-unit budget.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 authored and GitHub committed 2026-11-11 17:51:25 -06:00
1 parent 6aa278829e
commit 41a2880106
2 files changed
+123 -2

No files matched your search

+9 -2
View File
@@ -151,7 +151,12 @@ struct cff2_cs_interp_env_t : cs_interp_env_t<ELEM, CFF2Subrs>
region_count = varStore->varStore.get_region_index_count (get_ivs ());
if (do_blend)
{
if (unlikely (!scalars->resize_exact (region_count)))
/* Evaluating the scalars walks up to axisCount axes for each region
* index, both font-controlled 16-bit counts. Charge that before
* doing it, so a blend which then fails its operand check has paid. */
unsigned int axis_count = varStore->varStore.get_region_list ().axisCount;
if (unlikely (!SUPER::spend_budget (region_count, hb_max (1u, axis_count)) ||
!scalars->resize_exact (region_count)))
SUPER::set_error ();
else
varStore->varStore.get_region_scalars (get_ivs (), coords, num_coords,
@@ -329,7 +334,9 @@ struct cff2_cs_opset_t : cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PAR
env.set_error ();
return;
}
if (unlikely (!env.spend_budget (HB_BUDGET_1, total - n)))
/* Charge the default values too: with no regions there are no deltas to
* pop, so the same operands can be blended over and over. */
if (unlikely (!env.spend_budget (HB_BUDGET_1, total)))
{
env.set_error ();
return;
+114
View File
@@ -1150,6 +1150,52 @@ test_hb_draw_cff_budget (void)
g_assert_cmpint (cff2_blend, >, 512);
}
static void
test_hb_draw_cff2_blend_budget (void)
{
/* A charstring that pushes 100 numbers and then blends all of them 100
* times. Its VarData has no regions, so a blend has no deltas to pop;
* walking the default values must consume budget all the same. */
char cff2_data[59 + 100 + 100 * 2] = {
2, 0, 5, 0, 7, /* header */
(char) (139 + 50), 17, (char) (139 + 16), 24, (char) (139 + 40), 12, 36, /* Top DICT */
0, 0, 0, 0, /* global subrs */
0, 22, 0, 1, 0, 0, 0, 12, 0, 1, 0, 0, 0, 16, /* vstore */
0, 1, 0, 0, /* region list */
0, 0, 0, 0, 0, 0, /* VarData */
0, 0, 0, 1, 1, 1, 4, (char) 139, (char) (139 + 50), 18, /* FDArray, empty Private DICT */
0, 0, 0, 1, 2, 0, 1, 0x01, 0x2D /* CharStrings */
};
memset (cff2_data + 59, 139, 100);
for (unsigned i = 0; i < 100; i++)
{
cff2_data[159 + i * 2] = (char) (139 + 100);
cff2_data[159 + i * 2 + 1] = 16; /* blend */
}
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "CFF2", cff2_data);
hb_face_set_glyph_count (face, 1);
hb_font_t *font = hb_font_create (face);
draw_budget_data_t data = {HB_BUDGET_DEFAULT, 0};
hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
hb_draw_funcs_set_set_budget_func (draw_funcs, draw_set_budget, NULL, NULL);
hb_draw_funcs_set_get_budget_remaining_func (draw_funcs, draw_get_budget_remaining, NULL, NULL);
g_assert_true (hb_draw_set_budget (draw_funcs, &data, 20000));
g_assert_true (hb_font_draw_glyph_or_fail (font, 0, draw_funcs, &data));
g_assert_cmpint (data.live, <, 20000 - 100 * 100);
g_assert_true (hb_draw_set_budget (draw_funcs, &data, 5000));
g_assert_false (hb_font_draw_glyph_or_fail (font, 0, draw_funcs, &data));
g_assert_cmpint (data.live, <, 0);
hb_draw_funcs_destroy (draw_funcs);
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_hb_draw_glyf_composite_budget (void)
{
@@ -1193,6 +1239,72 @@ test_hb_draw_glyf_composite_budget (void)
hb_face_destroy (face);
}
static void
test_hb_draw_cff2_blend_region_budget (void)
{
/* A CFF2 font whose only charstring blends one value over 256 region
* indices, all naming the one region of a 16-axis region list. Evaluating
* the region scalars must be charged for every (region index, axis) pair,
* independently of the blend's own operand count. */
char cff2_data[938] = {0};
cff2_data[0] = 2;
cff2_data[2] = 5;
cff2_data[4] = 19;
/* Top DICT: CharStrings at 28, vstore at 296, FDArray at 928. */
cff2_data[5] = 0x1d; cff2_data[9] = 28; cff2_data[10] = 17;
cff2_data[11] = 0x1d; cff2_data[14] = 1; cff2_data[15] = 40; cff2_data[16] = 24;
cff2_data[17] = 0x1d; cff2_data[20] = 3; cff2_data[21] = (char) 160;
cff2_data[22] = 12; cff2_data[23] = 36;
/* Empty Global Subr INDEX at 24; CharStrings INDEX at 28: one 259-byte
* charstring: 257 zeros, 1, blend. */
cff2_data[31] = 1; cff2_data[32] = 2;
cff2_data[34] = 1; cff2_data[35] = 1; cff2_data[36] = 4;
for (unsigned i = 0; i < 257; i++)
cff2_data[37 + i] = (char) 139;
cff2_data[294] = (char) 140;
cff2_data[295] = 16;
/* VariationStore at 296: 630 bytes; region list at +12, one VarData at +112. */
cff2_data[296] = 2; cff2_data[297] = 118;
cff2_data[299] = 1; cff2_data[303] = 12; cff2_data[305] = 1; cff2_data[309] = 112;
cff2_data[311] = 16; cff2_data[313] = 1;
for (unsigned i = 0; i < 16; i++)
cff2_data[314 + i * 6 + 2] = cff2_data[314 + i * 6 + 4] = 0x40;
cff2_data[414] = 1;
/* FDArray INDEX at 928: one FD with an empty Private DICT. */
cff2_data[931] = 1; cff2_data[932] = 1; cff2_data[933] = 1; cff2_data[934] = 4;
cff2_data[935] = (char) 139;
cff2_data[936] = (char) 139;
cff2_data[937] = 18;
/* One axis; the font is moved off its default so that drawing blends. */
const char fvar_data[] = {0, 1, 0, 0, 0, 16, 0, 2, 0, 1, 0, 20, 0, 0, 0, 8,
'w', 'g', 'h', 't', 0, 100, 0, 0, 1, 144, 0, 0, 3, 132, 0, 0, 0, 0, 1, 0};
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "CFF2", cff2_data);
HB_FACE_ADD_TABLE (face, "fvar", fvar_data);
hb_face_set_glyph_count (face, 1);
hb_font_t *font = hb_font_create (face);
hb_variation_t variation = {HB_TAG ('w','g','h','t'), 800};
hb_font_set_variations (font, &variation, 1);
draw_budget_data_t data = {HB_BUDGET_DEFAULT, 0};
hb_draw_funcs_t *draw_funcs = hb_draw_funcs_create ();
hb_draw_funcs_set_set_budget_func (draw_funcs, draw_set_budget, NULL, NULL);
hb_draw_funcs_set_get_budget_remaining_func (draw_funcs, draw_get_budget_remaining, NULL, NULL);
g_assert_true (hb_draw_set_budget (draw_funcs, &data, 100000));
g_assert_true (hb_font_draw_glyph_or_fail (font, 0, draw_funcs, &data));
g_assert_cmpint (100000 - data.live, >=, 256 * 16);
g_assert_true (hb_draw_set_budget (draw_funcs, &data, 2048));
g_assert_false (hb_font_draw_glyph_or_fail (font, 0, draw_funcs, &data));
g_assert_cmpint (data.live, <, 0);
hb_draw_funcs_destroy (draw_funcs);
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_hb_draw_budget (void)
{
@@ -1361,7 +1473,9 @@ main (int argc, char **argv)
hb_test_add (test_hb_draw_immutable);
hb_test_add (test_hb_draw_budget);
hb_test_add (test_hb_draw_cff_budget);
hb_test_add (test_hb_draw_cff2_blend_budget);
hb_test_add (test_hb_draw_glyf_composite_budget);
hb_test_add (test_hb_draw_cff2_blend_region_budget);
const char **font_funcs = hb_font_list_funcs ();
for (const char **font_funcs_name = font_funcs; *font_funcs_name; font_funcs_name++)