mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 23:11:04 +01:00
[subset] Bound the glyf bytes a plan may retain through an aliasing loca
glyf::subset() trims (and, with --no-hinting, walks the components of) every retained glyph on every serializer retry. The only thing bounding that work was the implicit assumption that loca is monotonic, so the retained ranges sum to at most the table. loca need not be monotonic, so any number of glyph IDs can alias the same bytes, and the work grows as glyph count times glyph size times retries: a 328 KB font with loca = 0, L, 0, L, ... makes hb-subset spend ~22 s of CPU and then fail, where a same-sized monotonic font takes 0.02 s. The glyph closure in _populate_gids_to_retain() already fetches each retained glyph exactly once, before any table is subset, so charge the untrimmed length there (before the composite operation count can bail out, so every glyph added to the set is paid for) against a budget of HB_SUBSET_GLYF_MAX_BYTES_FACTOR (16) times the glyf length, and fail the plan when it is exceeded. This also makes hb_subset_preprocess() fall back to the source face cheaply. Fonts with a monotonic loca never exceed a factor of one, so their output is unchanged; the one behaviour change is that a specification-violating loca that aliases large glyph bytes more than sixteen tables' worth is now rejected instead of subsetting slowly. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-S5U7MYJ7. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
220ef2ca34
commit
be8103f334
4 files changed
+75
-3
No files matched your search
@@ -641,6 +641,7 @@ struct glyf_accelerator_t
|
||||
}
|
||||
|
||||
unsigned int get_num_glyphs () const { return num_glyphs; }
|
||||
unsigned int get_table_length () const { return glyf_table.get_length (); }
|
||||
hb_blob_t *reference_glyf_table () const
|
||||
{ return hb_blob_reference (glyf_table.get_blob ()); }
|
||||
|
||||
|
||||
@@ -112,6 +112,14 @@
|
||||
#define HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH 64
|
||||
#endif
|
||||
|
||||
/* Source bytes of all the glyphs the subsetter retains, as a multiple of
|
||||
* the glyf table length; exceeding it fails the plan. A monotonic loca
|
||||
* never needs more than one; a loca that aliases the same bytes to many
|
||||
* glyph IDs makes trimming and hint dropping cost that many table scans. */
|
||||
#ifndef HB_SUBSET_GLYF_MAX_BYTES_FACTOR
|
||||
#define HB_SUBSET_GLYF_MAX_BYTES_FACTOR 16
|
||||
#endif
|
||||
|
||||
#ifndef HB_SVG_MAX_PATH_SEGMENTS
|
||||
#define HB_SVG_MAX_PATH_SEGMENTS 262144
|
||||
#endif
|
||||
|
||||
+21
-3
@@ -391,6 +391,7 @@ _glyf_add_gid_and_children (const OT::glyf_accelerator_t &glyf,
|
||||
hb_codepoint_t gid,
|
||||
hb_set_t *gids_to_retain,
|
||||
int operation_count,
|
||||
int64_t &bytes_budget,
|
||||
unsigned depth = 0)
|
||||
{
|
||||
/* Check if is already visited */
|
||||
@@ -398,17 +399,24 @@ _glyf_add_gid_and_children (const OT::glyf_accelerator_t &glyf,
|
||||
|
||||
gids_to_retain->add (gid);
|
||||
|
||||
auto glyph = glyf.glyph_for_gid (gid);
|
||||
|
||||
/* Every retained glyph is walked here and again when glyf is subset.
|
||||
* Charge its untrimmed bytes before the operation count can bail out,
|
||||
* so that a glyph retained past that point is still paid for. */
|
||||
if (unlikely (!hb_budget_spend (bytes_budget, HB_BUDGET_1, glyph.get_bytes ().length)))
|
||||
return operation_count;
|
||||
|
||||
if (unlikely (depth++ > HB_MAX_NESTING_LEVEL)) return operation_count;
|
||||
if (unlikely (--operation_count < 0)) return operation_count;
|
||||
|
||||
auto glyph = glyf.glyph_for_gid (gid);
|
||||
|
||||
for (auto &item : glyph.get_composite_iterator ())
|
||||
operation_count =
|
||||
_glyf_add_gid_and_children (glyf,
|
||||
item.get_gid (glyph.is_extended ()),
|
||||
gids_to_retain,
|
||||
operation_count,
|
||||
bytes_budget,
|
||||
depth);
|
||||
|
||||
return operation_count;
|
||||
@@ -494,9 +502,19 @@ _populate_gids_to_retain (hb_subset_plan_t* plan,
|
||||
/* Populate a full set of glyphs to retain by adding all referenced
|
||||
* composite glyphs. */
|
||||
if (glyf.has_data ())
|
||||
{
|
||||
/* With a monotonic loca the retained glyphs add up to at most the
|
||||
* table; loca need not be monotonic though, and any number of glyph
|
||||
* IDs may alias the same bytes, each of them trimmed separately. */
|
||||
int64_t bytes_budget = (int64_t) glyf.get_table_length () *
|
||||
HB_SUBSET_GLYF_MAX_BYTES_FACTOR;
|
||||
for (hb_codepoint_t gid : cur_glyphset)
|
||||
_glyf_add_gid_and_children (glyf, gid, &plan->_glyphset,
|
||||
cur_glyphset.get_population () * HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH);
|
||||
cur_glyphset.get_population () * HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH,
|
||||
bytes_budget);
|
||||
if (unlikely (!plan->check_success (bytes_budget >= 0)))
|
||||
return;
|
||||
}
|
||||
else
|
||||
plan->_glyphset.union_ (cur_glyphset);
|
||||
#ifndef HB_NO_SUBSET_CFF
|
||||
|
||||
@@ -624,6 +624,50 @@ test_subset_glyf_recompute_default_instance_bounds (void)
|
||||
hb_face_destroy (source);
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_glyf_aliased_loca (void)
|
||||
{
|
||||
/* A loca of 0, 16, 0, 16, ...: every even glyph is the same 16 bytes and
|
||||
* every odd one starts after its end, so reads as empty. */
|
||||
enum { num_glyphs = 101 };
|
||||
static const char maxp_data[] = {0x00, 0x00, 0x50, 0x00, 0x00, num_glyphs};
|
||||
static const char head_data[54] = {
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x5F, 0x0F, 0x3C, (char) 0xF5, 0x00, 0x00, 0x03, (char) 0xE8,
|
||||
};
|
||||
static const char glyf_data[16] = {
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x31,
|
||||
};
|
||||
char loca_data[2 * (num_glyphs + 1)] = {0};
|
||||
for (unsigned i = 1; i <= num_glyphs; i += 2)
|
||||
loca_data[2 * i + 1] = sizeof (glyf_data) / 2;
|
||||
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
|
||||
HB_FACE_ADD_TABLE (face, "head", head_data);
|
||||
HB_FACE_ADD_TABLE (face, "glyf", glyf_data);
|
||||
HB_FACE_ADD_TABLE (face, "loca", loca_data);
|
||||
|
||||
/* A few glyphs sharing the same bytes are each copied out. */
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_glyph_set (input), 0, 4);
|
||||
hb_face_t *subset = hb_subset_test_create_subset (face, input);
|
||||
hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('g','l','y','f'));
|
||||
g_assert_cmpuint (hb_blob_get_length (blob), ==, 2 * sizeof (glyf_data));
|
||||
hb_blob_destroy (blob);
|
||||
hb_face_destroy (subset);
|
||||
|
||||
/* Enough of them to add up to many times the table are rejected. */
|
||||
input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_glyph_set (input), 0, num_glyphs - 1);
|
||||
g_assert_null (hb_subset_plan_create_or_fail (face, input));
|
||||
g_assert_null (hb_subset_or_fail (face, input));
|
||||
hb_subset_input_destroy (input);
|
||||
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -647,6 +691,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_subset_glyf_retain_gids_truncates);
|
||||
hb_test_add (test_subset_glyf_zero_contour_instructions);
|
||||
hb_test_add (test_subset_glyf_recompute_default_instance_bounds);
|
||||
hb_test_add (test_subset_glyf_aliased_loca);
|
||||
|
||||
#ifdef HB_EXPERIMENTAL_API
|
||||
hb_test_add (test_subset_glyf_iftb_requirements);
|
||||
|
||||
Reference in new issue
Block a user