mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-23 07:51:10 +01:00
[depend] Bound COLR dependency extraction with a shared op budget
COLR::depend() walked the COLRv1 paint graph from scratch for every BaseGlyphPaintRecord (clearing the visited set each time) with no total work limit, so N base glyphs sharing one paint graph cost N times the graph, quadratic in table size (204 s for a 200 KB font); the COLRv0 loop has the same base-glyphs x layers shape. The API is built only with -Dexperimental_api=true. hb_colrv1_depend_context_t, which already owns the per-walk depth and visited-set guards, now also carries max_ops sized like the sanitizer's (table length x HB_SANITIZE_MAX_OPS_FACTOR, clamped to the MIN/MAX), charged one unit per Paint::dispatch(), per COLRv0 layer record, and per PaintColrLayers layer run. Charging the layer run in dependv1() matters because a layer resolving to the null or an unknown-format paint is dropped by Paint::dispatch() before it reaches the context, so without it a PaintColrLayers could still spin its 255-iteration loop uncharged. Exhausting the budget calls depend_data->fail() (the existing error idiom) so hb_subset_depend_from_face_or_fail() returns NULL rather than a graph with silently missing edges, and the base-glyph loop stops once the builder has failed. The reproducer font is now rejected in a fraction of a second; the control still succeeds, and legitimate fonts keep 64 ops per table byte of headroom. Adds test_depend_colr_budget. Follow-up to #6113. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-TO8ND880. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
b732218126
commit
b83dcdbcde
3 files changed
+116
-3
No files matched your search
@@ -242,6 +242,9 @@ struct hb_colrv1_depend_context_t :
|
||||
template <typename T>
|
||||
return_t dispatch (const T &obj)
|
||||
{
|
||||
if (unlikely (!spend_ops ()))
|
||||
return hb_empty_t ();
|
||||
|
||||
if (unlikely (nesting_level_left == 0))
|
||||
return hb_empty_t ();
|
||||
|
||||
@@ -267,6 +270,20 @@ struct hb_colrv1_depend_context_t :
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Layers and paints shared between base glyphs are walked again for each
|
||||
* of them, which sanitize does not charge for. Bound the total work and
|
||||
* fail, rather than return a graph with edges missing. */
|
||||
bool spend_ops (unsigned count = 1)
|
||||
{
|
||||
max_ops -= (int) count;
|
||||
if (unlikely (max_ops < 0))
|
||||
{
|
||||
max_ops = -1;
|
||||
return depend_data->fail ();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const COLR* get_colr_table () const
|
||||
{ return colr; }
|
||||
|
||||
@@ -274,14 +291,19 @@ struct hb_colrv1_depend_context_t :
|
||||
const COLR *colr;
|
||||
hb_depend_data_builder_t *depend_data;
|
||||
hb_set_t visited_paint;
|
||||
int max_ops;
|
||||
unsigned nesting_level_left;
|
||||
hb_codepoint_t source_gid;
|
||||
|
||||
hb_colrv1_depend_context_t (const COLR *colr_,
|
||||
unsigned colr_length,
|
||||
hb_depend_data_builder_t *depend_data_,
|
||||
unsigned nesting_level_left_ = HB_MAX_NESTING_LEVEL) :
|
||||
colr (colr_),
|
||||
depend_data (depend_data_),
|
||||
max_ops ((int) hb_clamp ((uint64_t) colr_length * HB_SANITIZE_MAX_OPS_FACTOR,
|
||||
(uint64_t) HB_SANITIZE_MAX_OPS_MIN,
|
||||
(uint64_t) HB_SANITIZE_MAX_OPS_MAX)),
|
||||
nesting_level_left (nesting_level_left_),
|
||||
source_gid (0)
|
||||
{}
|
||||
@@ -2601,7 +2623,7 @@ struct COLR
|
||||
void depend (hb_depend_data_builder_t *depend_data) const
|
||||
{
|
||||
if (!has_data ()) return;
|
||||
colr->depend (depend_data);
|
||||
colr->depend (depend_data, colr.get_length ());
|
||||
}
|
||||
|
||||
void closure_glyphs (const hb_set_t *glyphs,
|
||||
@@ -2665,14 +2687,18 @@ struct COLR
|
||||
mutable hb_atomic_t<hb_colr_scratch_t *> cached_scratch;
|
||||
};
|
||||
|
||||
void depend (hb_depend_data_builder_t *depend_data) const
|
||||
void depend (hb_depend_data_builder_t *depend_data, unsigned table_length) const
|
||||
{
|
||||
hb_colrv1_depend_context_t c (this, table_length, depend_data);
|
||||
|
||||
// v0
|
||||
hb_array_t<const BaseGlyphRecord> baseGlyphs = (this+baseGlyphsZ).as_array (numBaseGlyphs);
|
||||
for (const BaseGlyphRecord &record : baseGlyphs)
|
||||
{
|
||||
auto glyph_layers = (this+layersZ).as_array (numLayers).sub_array (record.firstLayerIdx,
|
||||
record.numLayers);
|
||||
if (unlikely (!c.spend_ops (glyph_layers.length)))
|
||||
return;
|
||||
for (const LayerRecord layer : glyph_layers)
|
||||
depend_data->add_depend(record.glyphId, HB_OT_TAG_COLR, layer.glyphId);
|
||||
}
|
||||
@@ -2681,11 +2707,13 @@ struct COLR
|
||||
return;
|
||||
|
||||
// v1
|
||||
hb_colrv1_depend_context_t c (this, depend_data);
|
||||
const BaseGlyphList &baseglyph_paintrecords = this+baseGlyphList;
|
||||
|
||||
for (const BaseGlyphPaintRecord &baseglyph_paintrecord: baseglyph_paintrecords.iter ())
|
||||
{
|
||||
if (unlikely (!depend_data->successful))
|
||||
return;
|
||||
|
||||
c.source_gid = baseglyph_paintrecord.glyphId;
|
||||
c.visited_paint.clear ();
|
||||
|
||||
|
||||
@@ -39,6 +39,10 @@ namespace OT {
|
||||
HB_INTERNAL void PaintColrLayers::dependv1 (hb_colrv1_depend_context_t* c) const
|
||||
{
|
||||
const LayerList &paint_offset_lists = c->get_colr_table ()->get_layerList ();
|
||||
/* Charge the whole layer run: a layer resolving to the null or an
|
||||
* unknown-format paint is dropped by Paint::dispatch() before it reaches
|
||||
* the context, so without this the loop can still spin uncharged. */
|
||||
if (unlikely (!c->spend_ops (numLayers))) return;
|
||||
for (unsigned i = firstLayerIndex; i < firstLayerIndex + numLayers; i++)
|
||||
{
|
||||
const Paint &paint = paint_offset_lists.get_paint (i);
|
||||
|
||||
@@ -353,6 +353,86 @@ test_depend_colr (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static hb_bool_t
|
||||
colr_depend_succeeds (const unsigned char *colr, unsigned int length, unsigned int num_glyphs)
|
||||
{
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
hb_blob_t *blob = hb_blob_create_or_fail ((const char *) colr, length, HB_MEMORY_MODE_DUPLICATE, NULL, NULL);
|
||||
hb_face_builder_add_table (face, HB_TAG ('C','O','L','R'), blob);
|
||||
hb_blob_destroy (blob);
|
||||
hb_face_set_glyph_count (face, num_glyphs);
|
||||
|
||||
hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face);
|
||||
hb_bool_t ret = depend != NULL;
|
||||
|
||||
hb_subset_depend_destroy (depend);
|
||||
hb_face_destroy (face);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Test that COLR work repeated for every base glyph is bounded */
|
||||
static void
|
||||
test_depend_colr_budget (void)
|
||||
{
|
||||
{
|
||||
/* COLRv0: every base glyph uses all of the layers. */
|
||||
enum { NUM_GLYPHS = 2048, LAYERS = 14 + NUM_GLYPHS * 6 };
|
||||
static unsigned char colr[LAYERS + NUM_GLYPHS * 4];
|
||||
colr[7] = 14;
|
||||
colr[10] = LAYERS >> 8;
|
||||
colr[11] = LAYERS & 0xFF;
|
||||
colr[12] = NUM_GLYPHS >> 8;
|
||||
for (unsigned i = 0; i < NUM_GLYPHS; i++)
|
||||
{
|
||||
unsigned char *record = colr + 14 + i * 6;
|
||||
record[0] = i >> 8;
|
||||
record[1] = i & 0xFF;
|
||||
record[4] = NUM_GLYPHS >> 8;
|
||||
}
|
||||
|
||||
colr[3] = 1;
|
||||
g_assert_true (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
colr[2] = NUM_GLYPHS >> 8;
|
||||
colr[3] = 0;
|
||||
g_assert_false (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
}
|
||||
|
||||
{
|
||||
/* COLRv1: 255 PaintColrLayers that each list all 255 of them as layers,
|
||||
* and every base glyph uses the first one. */
|
||||
enum { NUM_GLYPHS = 64, NUM_PAINTS = 255,
|
||||
BASE_GLYPHS = 34,
|
||||
LAYERS = BASE_GLYPHS + 4 + NUM_GLYPHS * 6,
|
||||
PAINTS = LAYERS + 4 + NUM_PAINTS * 4 };
|
||||
static unsigned char colr[PAINTS + NUM_PAINTS * 6];
|
||||
colr[1] = 1;
|
||||
colr[17] = BASE_GLYPHS;
|
||||
colr[20] = LAYERS >> 8;
|
||||
colr[21] = LAYERS & 0xFF;
|
||||
for (unsigned i = 0; i < NUM_GLYPHS; i++)
|
||||
{
|
||||
unsigned char *record = colr + BASE_GLYPHS + 4 + i * 6;
|
||||
record[1] = i;
|
||||
record[4] = (PAINTS - BASE_GLYPHS) >> 8;
|
||||
record[5] = (PAINTS - BASE_GLYPHS) & 0xFF;
|
||||
}
|
||||
colr[LAYERS + 3] = NUM_PAINTS;
|
||||
for (unsigned i = 0; i < NUM_PAINTS; i++)
|
||||
{
|
||||
unsigned offset = PAINTS - LAYERS + i * 6;
|
||||
colr[LAYERS + 4 + i * 4 + 2] = offset >> 8;
|
||||
colr[LAYERS + 4 + i * 4 + 3] = offset & 0xFF;
|
||||
colr[PAINTS + i * 6] = 1;
|
||||
colr[PAINTS + i * 6 + 1] = NUM_PAINTS;
|
||||
}
|
||||
|
||||
colr[BASE_GLYPHS + 3] = 1;
|
||||
g_assert_true (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
colr[BASE_GLYPHS + 3] = NUM_GLYPHS;
|
||||
g_assert_false (colr_depend_succeeds (colr, sizeof (colr), NUM_GLYPHS));
|
||||
}
|
||||
}
|
||||
|
||||
/* Test MATH (mathematical variants) dependency extraction */
|
||||
static void
|
||||
test_depend_math (void)
|
||||
@@ -631,6 +711,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_depend_glyf);
|
||||
hb_test_add (test_depend_cff);
|
||||
hb_test_add (test_depend_colr);
|
||||
hb_test_add (test_depend_colr_budget);
|
||||
hb_test_add (test_depend_math);
|
||||
hb_test_add (test_depend_varc);
|
||||
hb_test_add (test_depend_gsub_formats);
|
||||
|
||||
Reference in new issue
Block a user