mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 06:21:16 +01:00
[subset/CBDT] Bound image bytes copied out of the source table
IndexSubtableRecord::add_new_subtable() bounds-checks each retained glyph's image block against the source CBDT table, but nothing requires the blocks of different glyphs (or strikes) to be distinct and nothing limits the total copied. A format 3 offsetArray of 0, 65535, 0, 65535, ... makes every even glyph copy the same 64 KB, so a ~186 KB font makes one hb_subset_or_fail() peak at ~3.8 GB and emit a ~1.97 GB font. The existing source_blob->length * 256 cap covers only the serializer buffer holding the new CBLC, not the CBDT side buffers. Charge every block copied out of the source CBDT to a byte budget that is proportional to the source table and shared across strikes, and fail the strike when it runs out; the existing subset_size_table() path then reverts that strike and continues. The budget lives in cbdt_dedup_context_t, which CBLC::subset already shares across every strike, and is set to cbdt_length * HB_SUBSET_CBDT_MAX_COPY_FACTOR, a new limit (64) in hb-limits.hh. The charge is taken before the copy into the per-strike buffer, so strikes that end up sharing data are bounded as well, and it covers index formats 1/2/3 and overlapping records, not only non-monotonic format 3 arrays. The budget defaults to unlimited, so the direct users of the struct (test-cbdt-cache) are unaffected; a well-formed font copies each byte at most once per strike, so only a font whose strikes reference the table more than 64 times over loses bitmaps. This is distinct from the integer-overflow hardening of _copy_data_to_cbdt() in PR #6485: that bounds the final append to cbdt_prime, this bounds the overlap amplification before the copy. Adds test_subset_cbdt_overlapping_images, which builds the format 3 shape above and asserts the output CBDT stays within the factor (524292 > 65536 on the unpatched tree). The api and subset suites pass and the reproducer drops from 3767 MiB to 17 MiB. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-C39OXNU0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
8986600d59
commit
51fae4098a
3 files changed
+83
No files matched your search
@@ -105,6 +105,11 @@ struct cbdt_dedup_context_t
|
||||
hb_hashmap_t<uint64_t, unsigned int> map;
|
||||
hb_vector_t<entry_t> entries;
|
||||
hb_vector_t<unsigned int> lengths; /* Concatenated per-entry lengths. */
|
||||
|
||||
/* Bytes that may still be copied out of the source CBDT table, shared
|
||||
* across strikes. Blocks of different glyphs may overlap or repeat, so
|
||||
* checking each against the table length does not bound their sum. */
|
||||
int64_t budget = hb_int_max (int64_t);
|
||||
};
|
||||
|
||||
struct cblc_bitmap_size_subset_context_t
|
||||
@@ -448,6 +453,9 @@ struct IndexSubtableRecord
|
||||
if (unlikely (offset > bitmap_size_context->cbdt_length ||
|
||||
bitmap_size_context->cbdt_length - offset < length))
|
||||
return_trace (false);
|
||||
if (unlikely (bitmap_size_context->dedup &&
|
||||
!hb_budget_spend (bitmap_size_context->dedup->budget, length)))
|
||||
return_trace (false);
|
||||
if (unlikely (!c->serializer->check_success (
|
||||
_copy_data_to_cbdt (&data,
|
||||
bitmap_size_context->cbdt + offset,
|
||||
@@ -1075,6 +1083,7 @@ CBLC::subset (hb_subset_context_t *c) const
|
||||
_copy_data_to_cbdt (&cbdt_prime, cbdt, CBDT::min_size);
|
||||
|
||||
cbdt_dedup_context_t dedup;
|
||||
dedup.budget = (int64_t) cbdt_length * HB_SUBSET_CBDT_MAX_COPY_FACTOR;
|
||||
for (const BitmapSizeTable& table : + sizeTables.iter ())
|
||||
subset_size_table (c, table, (const char *) cbdt, cbdt_length, cblc_prime, &cbdt_prime, &dedup);
|
||||
|
||||
|
||||
@@ -120,6 +120,14 @@
|
||||
#define HB_GPU_DRAW_MAX_CURVES 65536
|
||||
#endif
|
||||
|
||||
/* Bytes the CBDT subsetter may copy out of the source CBDT table, as a
|
||||
* multiple of that table's length. Image blocks of different glyphs or
|
||||
* strikes may overlap or repeat, so bounds-checking each block against the
|
||||
* table does not bound their sum. */
|
||||
#ifndef HB_SUBSET_CBDT_MAX_COPY_FACTOR
|
||||
#define HB_SUBSET_CBDT_MAX_COPY_FACTOR 64
|
||||
#endif
|
||||
|
||||
/* Total bytes of sub-blobs (gradient color stops, clip outlines) in one
|
||||
* hb_gpu_paint_t walk; exceeding it makes the paint unsupported. */
|
||||
#ifndef HB_GPU_PAINT_MAX_SUB_BYTES
|
||||
|
||||
@@ -135,6 +135,71 @@ test_subset_cbdt_index_format_3 (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_cbdt_overlapping_images (void)
|
||||
{
|
||||
/* One format 3 subtable whose offsetArray alternates 0, CBDT_LENGTH, so
|
||||
* every even glyph claims the whole CBDT table as its image and every odd
|
||||
* glyph has none. Each block is in bounds, but copying them all would
|
||||
* blow the output up to NUM_GLYPHS / 2 times the source table. */
|
||||
enum { NUM_GLYPHS = 1024, CBDT_LENGTH = 1024 };
|
||||
static const char MAXP_data[6] =
|
||||
"\x00\x00\x50\x00" /* version */
|
||||
"\x04\x00" /* numGlyphs */
|
||||
;
|
||||
static const char CBDT_data[CBDT_LENGTH] =
|
||||
"\x00\x03\x00\x00" /* version */
|
||||
;
|
||||
static char CBLC_data[8 + 48 + 8 + 8 + 2 * (NUM_GLYPHS + 1)] =
|
||||
"\x00\x03\x00\x00" /* version */
|
||||
"\x00\x00\x00\x01" /* numSizes */
|
||||
/* BitmapSize */
|
||||
"\x00\x00\x00\x38" /* indexSubtableArrayOffset */
|
||||
"\x00\x00\x00\x00" /* indexTablesSize */
|
||||
"\x00\x00\x00\x01" /* numberOfIndexSubtables */
|
||||
"\x00\x00\x00\x00" /* colorRef */
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" /* hori */
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" /* vert */
|
||||
"\x00\x00" /* startGlyphIndex */
|
||||
"\x03\xFF" /* endGlyphIndex */
|
||||
"\x20\x20\x20\x01" /* ppemX, ppemY, bitDepth, flags */
|
||||
/* IndexSubtableRecord */
|
||||
"\x00\x00" /* firstGlyphIndex */
|
||||
"\x03\xFF" /* lastGlyphIndex */
|
||||
"\x00\x00\x00\x08" /* offsetToSubtable */
|
||||
/* IndexSubtableFormat3 */
|
||||
"\x00\x03" /* indexFormat */
|
||||
"\x00\x11" /* imageFormat */
|
||||
"\x00\x00\x00\x00" /* imageDataOffset */
|
||||
;
|
||||
char *offsets = CBLC_data + 8 + 48 + 8 + 8;
|
||||
for (unsigned i = 1; i <= NUM_GLYPHS; i += 2)
|
||||
{
|
||||
offsets[2 * i] = CBDT_LENGTH >> 8;
|
||||
offsets[2 * i + 1] = CBDT_LENGTH & 0xFF;
|
||||
}
|
||||
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
HB_FACE_ADD_TABLE (face, "maxp", MAXP_data);
|
||||
HB_FACE_ADD_TABLE (face, "CBLC", CBLC_data);
|
||||
HB_FACE_ADD_TABLE (face, "CBDT", CBDT_data);
|
||||
|
||||
hb_set_t *glyphs = hb_set_create ();
|
||||
hb_set_add_range (glyphs, 0, NUM_GLYPHS - 1);
|
||||
hb_face_t *face_subset = hb_subset_test_create_subset (
|
||||
face, hb_subset_test_create_input_from_glyphs (glyphs));
|
||||
hb_set_destroy (glyphs);
|
||||
|
||||
/* The subsetter copies at most HB_SUBSET_CBDT_MAX_COPY_FACTOR times the
|
||||
* source table; here that drops the strike instead of emitting 512 KiB. */
|
||||
hb_blob_t *cbdt = hb_face_reference_table (face_subset, HB_TAG ('C','B','D','T'));
|
||||
g_assert_cmpuint (hb_blob_get_length (cbdt), <=, 64 * CBDT_LENGTH);
|
||||
hb_blob_destroy (cbdt);
|
||||
|
||||
hb_face_destroy (face_subset);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
// TODO: add support/tests for index formats 2,4,5 (image formats are treated as
|
||||
// opaque blobs when subsetting so don't need to be tested separately).
|
||||
// TODO: add a test that keeps no codepoints.
|
||||
@@ -153,6 +218,7 @@ main (int argc, char **argv)
|
||||
// Can use FontTools after https://github.com/fonttools/fonttools/issues/1817
|
||||
// is resolved.
|
||||
hb_test_add (test_subset_cbdt_index_format_3);
|
||||
hb_test_add (test_subset_cbdt_overlapping_images);
|
||||
|
||||
return hb_test_run();
|
||||
}
|
||||
Reference in new issue
Block a user