mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-29 09:34:05 +01:00
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 <noreply@anthropic.com>
164 lines
5.3 KiB
C
164 lines
5.3 KiB
C
/*
|
|
* Copyright © 2026 Behdad Esfahbod
|
|
*
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
*
|
|
* Permission is hereby granted, without written agreement and without
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
* software and its documentation for any purpose, provided that the
|
|
* above copyright notice and the following two paragraphs appear in
|
|
* all copies of this software.
|
|
*
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
* DAMAGE.
|
|
*
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
*/
|
|
|
|
#include "hb-test.h"
|
|
|
|
#include <hb-cairo.h>
|
|
|
|
#if defined(__has_feature)
|
|
#if __has_feature(thread_sanitizer)
|
|
#define HB_TEST_CAIRO_TSAN 1
|
|
#endif
|
|
#endif
|
|
#if defined(__SANITIZE_THREAD__)
|
|
#define HB_TEST_CAIRO_TSAN 1
|
|
#endif
|
|
|
|
static void
|
|
test_hb_cairo_budget (void)
|
|
{
|
|
#ifdef HB_TEST_CAIRO_TSAN
|
|
/* Cairo's user-font rendering takes two internal mutexes in both orders.
|
|
* The same workload reports this lock-order inversion against unmodified
|
|
* HarfBuzz; keep race detection enabled rather than suppressing Cairo
|
|
* deadlock reports process-wide. */
|
|
g_test_skip ("Cairo user-font rendering has a pre-existing lock-order inversion under TSan");
|
|
return;
|
|
#endif
|
|
|
|
hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-4886959609413632");
|
|
cairo_font_face_t *cairo_face = hb_cairo_font_face_create_for_face (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 = {0, 0, 0};
|
|
cairo_show_glyphs (cr, &glyph, 1);
|
|
|
|
g_assert_cmpint (cairo_status (cr), ==, CAIRO_STATUS_SUCCESS);
|
|
g_assert_cmpint (cairo_surface_status (surface), ==, CAIRO_STATUS_SUCCESS);
|
|
|
|
cairo_destroy (cr);
|
|
cairo_surface_destroy (surface);
|
|
cairo_font_face_destroy (cairo_face);
|
|
hb_face_destroy (face);
|
|
}
|
|
|
|
static void
|
|
test_hb_cairo_color_glyph_cycle (void)
|
|
{
|
|
#ifdef HB_TEST_CAIRO_TSAN
|
|
g_test_skip ("Cairo user-font rendering has a pre-existing lock-order inversion under TSan");
|
|
return;
|
|
#endif
|
|
|
|
/* Two COLRv1 glyphs that paint each other with PaintColrGlyph. The
|
|
* color_glyph callback used to show the nested glyph through cairo,
|
|
* re-entering hb_font_paint_glyph() without the cycle and depth
|
|
* guards of the paint walk in progress. */
|
|
hb_face_t *face = hb_test_open_font_file ("fonts/test_glyphs-glyf_colr_1.ttf");
|
|
hb_font_t *font = hb_font_create (face);
|
|
cairo_font_face_t *cairo_face = hb_cairo_font_face_create_for_font (font);
|
|
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);
|
|
|
|
/* paintcolrglyph_cycle_first, paintcolrglyph_cycle_second */
|
|
for (hb_codepoint_t u = 0xF1100; u <= 0xF1101; u++)
|
|
{
|
|
hb_codepoint_t gid;
|
|
g_assert_true (hb_font_get_nominal_glyph (font, u, &gid));
|
|
|
|
cairo_glyph_t glyph = {gid, 0, 30};
|
|
cairo_show_glyphs (cr, &glyph, 1);
|
|
}
|
|
|
|
g_assert_cmpint (cairo_status (cr), ==, CAIRO_STATUS_SUCCESS);
|
|
g_assert_cmpint (cairo_surface_status (surface), ==, CAIRO_STATUS_SUCCESS);
|
|
|
|
cairo_destroy (cr);
|
|
cairo_surface_destroy (surface);
|
|
cairo_font_face_destroy (cairo_face);
|
|
hb_font_destroy (font);
|
|
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 ();
|
|
}
|