From f840e4d018dae3256bb94b3cdbdd0d1a47e41b6a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:00:28 -0600 Subject: [PATCH] [subset] Clamp PaintColrLayers layer range to LayerList in COLRv1 closure PaintColrLayers::closurev1() recorded firstLayerIndex .. firstLayerIndex + numLayers - 1 as retained layers without checking the range against LayerList.len (PaintColrLayers::sanitize is a plain check_struct), and _colr_closure() then creates one colrv1_layers map entry per retained index. A crafted font whose leaf paints point past the end of the LayerList could therefore add up to 255 entries each for layers that do not exist, driving subset-plan memory to roughly 1200x the font size (585 MiB for a 512 KB font). Clamp the range to the LayerList at the one place the retained-layer set is built, before add_layer_indices() and for the dispatch loop alike; this also removes the firstLayerIndex + numLayers wrap-around in the loop bound. Readers (paint_glyph, dependv1) never record indices and already tolerate out-of-range ones via LayerList::get_paint() returning Null, so they are left alone. A PaintColrLayers whose range lies past the source LayerList now has no mapping and subset writes 0xFFFFFFFF; both that and a renumbered index past the new LayerList are out of range and draw nothing. Adds a regression test that builds such a font inline and pins the output; it fails without the fix. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ROQFK0RA. Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/colrv1-closure.hh | 8 +++- test/api/test-subset-colr.c | 61 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/OT/Color/COLR/colrv1-closure.hh b/src/OT/Color/COLR/colrv1-closure.hh index f94ce070c..24ef3bda5 100644 --- a/src/OT/Color/COLR/colrv1-closure.hh +++ b/src/OT/Color/COLR/colrv1-closure.hh @@ -38,9 +38,13 @@ namespace OT { HB_INTERNAL void PaintColrLayers::closurev1 (hb_colrv1_closure_context_t* c) const { - c->add_layer_indices (firstLayerIndex, numLayers); const LayerList &paint_offset_lists = c->get_colr_table ()->get_layerList (); - for (unsigned i = firstLayerIndex; i < firstLayerIndex + numLayers; i++) + /* Only retain layers that exist in the LayerList; firstLayerIndex is not + * checked against it at sanitize time. */ + unsigned first = hb_min ((unsigned) firstLayerIndex, (unsigned) paint_offset_lists.len); + unsigned count = hb_min ((unsigned) numLayers, paint_offset_lists.len - first); + c->add_layer_indices (first, count); + for (unsigned i = first; i < first + count; i++) { const Paint &paint = paint_offset_lists.get_paint (i); paint.dispatch (c); diff --git a/test/api/test-subset-colr.c b/test/api/test-subset-colr.c index feb9767f2..b22c28ccb 100644 --- a/test/api/test-subset-colr.c +++ b/test/api/test-subset-colr.c @@ -587,6 +587,66 @@ test_subset_colr_var_overflow (void) hb_face_destroy (face); } +/* A PaintColrLayers whose layer range lies past the end of the LayerList + * must not make the subsetter retain (and allocate mappings for) layers + * that do not exist. */ +static void +test_subset_colr_layers_out_of_range (void) +{ + const uint8_t maxp_data[6] = { + 0x00, 0x00, 0x50, 0x00, + 0x00, 0x02 /* numGlyphs */ + }; + const uint8_t colr_data[73] = { + /* header: version 1, no COLRv0 records, BaseGlyphList @34, LayerList @50 */ + 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x32, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + /* BaseGlyphList: glyph 1 -> PaintColrLayers (numLayers 2, firstLayerIndex 0) */ + 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0A, + 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, + /* LayerList: PaintSolid, then PaintColrLayers (numLayers 255, firstLayerIndex 0xFFFFFF00) */ + 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x11, + 0x02, 0x00, 0x00, 0x40, 0x00, + 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + }; + + hb_blob_t *colr = hb_blob_create ((const char *) colr_data, sizeof (colr_data), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_blob_t *maxp = hb_blob_create ((const char *) maxp_data, sizeof (maxp_data), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_t *builder = hb_face_builder_create (); + hb_face_builder_add_table (builder, HB_TAG ('C','O','L','R'), colr); + hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), maxp); + hb_blob_t *face_blob = hb_face_reference_blob (builder); + hb_face_t *face = hb_face_create (face_blob, 0); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_glyph_set (input), 1); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + + hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('C','O','L','R')); + unsigned length; + const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length); + g_assert_cmpuint (length, >=, 34); + unsigned list = read_uint_be (data + 18, 4); + g_assert_cmpuint (list + 12, <=, length); + /* Only the two layers that exist are kept. */ + g_assert_cmpuint (read_uint_be (data + list, 4), ==, 2); + unsigned paint = list + read_uint_be (data + list + 8, 4); + g_assert_cmpuint (paint + 6, <=, length); + g_assert_cmpuint (data[paint], ==, 1); + g_assert_cmpuint (data[paint + 1], ==, 255); + /* The out-of-range range was not retained, so it has no mapping. */ + g_assert_cmpuint (read_uint_be (data + paint + 2, 4), ==, 0xFFFFFFFFu); + hb_blob_destroy (blob); + + hb_face_destroy (subset); + hb_face_destroy (face); + hb_blob_destroy (face_blob); + hb_face_destroy (builder); + hb_blob_destroy (maxp); + hb_blob_destroy (colr); +} + int main (int argc, char **argv) { @@ -603,6 +663,7 @@ main (int argc, char **argv) hb_test_add (test_subset_colr_affine_values); hb_test_add (test_subset_colr_sentinel_map_without_variations); hb_test_add (test_subset_colr_var_overflow); + hb_test_add (test_subset_colr_layers_out_of_range); return hb_test_run(); }