From 4e215a7ec80a50111f42ae5d06abf26cc8faa61b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 14:44:32 -0600 Subject: [PATCH] [cairo] Don't leak the hb_font_t created per scaled font hb_cairo_init_scaled_font() must hand exactly one reference of the hb_font_t to the cairo_scaled_font_t. For a face-based font face (hb_cairo_font_face_create_for_face()) it creates the font with a reference count of 1, but then still called hb_font_reference() before attaching it, so the creating reference was never released. Every finalized scaled font therefore leaked an hb_font_t, pinning its hb_face_t, the font blob and the accelerators until the process exits. Cairo finalizes scaled fonts during normal operation once its cache fills, so the leak accumulates as content selects distinct sizes, transforms or font options. Take the extra reference only in the branch where the font is the shared one owned by the cairo font face (the font-based constructor), and transfer the freshly created font as-is. The set_user_data result is now checked and the font released on failure, matching the budget/color-cache idiom a few lines below and the font-face constructors in the same file. A callback installed with hb_cairo_font_face_set_font_init_func() that returns a different font keeps its historical behavior: the returned font is referenced rather than assumed to transfer ownership, so no in-tree contract changes and no caller can turn into a use-after-free. hb-view uses the font-based constructor and was unaffected. A regression test in test/api/test-cairo.c registers a face destroy notifier, draws through a face-based font face, flushes cairo's scaled-font cache and asserts the face was destroyed; it fails on the unpatched library and passes with the fix. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BAUN9JN4. Co-Authored-By: Claude Opus 4.8 --- src/hb-cairo.cc | 28 +++++++++++++++++++++------ test/api/test-cairo.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 5076e54cb..681229ca5 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -577,7 +577,11 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, hb_font_t *font = (hb_font_t *) cairo_font_face_get_user_data (font_face, &hb_cairo_font_user_data_key); - if (!font) + if (font) + /* Shared font owned by the cairo font face (the font-based + * constructor). Take our own reference to hand to the scaled font. */ + hb_font_reference (font); + else { hb_face_t *face = (hb_face_t *) cairo_font_face_get_user_data (font_face, &hb_cairo_face_user_data_key); @@ -622,16 +626,28 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, { void *user_data = cairo_font_face_get_user_data (font_face, &hb_cairo_font_init_user_data_user_data_key); - font = init_func (font, scaled_font, user_data); + hb_font_t *new_font = init_func (font, scaled_font, user_data); + /* Preserve the historical ownership behavior for a callback that + * returns a different font: reference it, rather than assuming it + * transfers ownership. The font we created is the callback's to + * release, as before. */ + if (new_font != font) + hb_font_reference (new_font); + font = new_font; } hb_font_make_immutable (font); } - cairo_scaled_font_set_user_data (scaled_font, - &hb_cairo_font_user_data_key, - (void *) hb_font_reference (font), - hb_cairo_font_destroy); + /* The scaled font takes over our one reference to font. */ + if (unlikely (CAIRO_STATUS_SUCCESS != cairo_scaled_font_set_user_data (scaled_font, + &hb_cairo_font_user_data_key, + (void *) font, + hb_cairo_font_destroy))) + { + hb_font_destroy (font); + return CAIRO_STATUS_NO_MEMORY; + } hb_cairo_budget_t *budget = (hb_cairo_budget_t *) hb_malloc (sizeof (hb_cairo_budget_t)); if (unlikely (!budget)) diff --git a/test/api/test-cairo.c b/test/api/test-cairo.c index b4dd079cb..8074531d2 100644 --- a/test/api/test-cairo.c +++ b/test/api/test-cairo.c @@ -108,12 +108,56 @@ test_hb_cairo_color_glyph_cycle (void) hb_face_destroy (face); } +static void +face_destroyed (void *user_data) +{ + *(hb_bool_t *) user_data = 1; +} + +static void +test_hb_cairo_face_lifecycle (void) +{ +#ifdef HB_TEST_CAIRO_TSAN + g_test_skip ("Cairo user-font rendering has a pre-existing lock-order inversion under TSan"); + return; +#endif + + static hb_user_data_key_t key; + hb_bool_t destroyed = 0; + + hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf"); + hb_face_set_user_data (face, &key, &destroyed, face_destroyed, 1); + cairo_font_face_t *cairo_face = hb_cairo_font_face_create_for_face (face); + hb_face_destroy (face); + + cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 64, 64); + cairo_t *cr = cairo_create (surface); + + cairo_set_font_face (cr, cairo_face); + cairo_set_font_size (cr, 30); + + cairo_glyph_t glyph = {1, 0, 0}; + cairo_show_glyphs (cr, &glyph, 1); + + g_assert_cmpint (cairo_status (cr), ==, CAIRO_STATUS_SUCCESS); + + cairo_destroy (cr); + cairo_surface_destroy (surface); + cairo_font_face_destroy (cairo_face); + + /* Cairo keeps unreferenced scaled fonts cached; flush them so the + * hb_font_t created for the scaled font, and with it the face, go away. */ + cairo_debug_reset_static_data (); + g_assert_true (destroyed); +} + int main (int argc, char **argv) { hb_test_init (&argc, &argv); hb_test_add (test_hb_cairo_budget); hb_test_add (test_hb_cairo_color_glyph_cycle); + hb_test_add (test_hb_cairo_face_lifecycle); return hb_test_run (); }