[ft] Preserve face and blob ownership through loader lifetimes (#6475)

Keep the memory-backed FT_Face blob on the hb_face_t even when the
OpenType sanitizer discards its copy. Otherwise the destroy callback
can release the FT_Face while the face or font still uses it. Return
the empty object on allocation failure before accessing that pointer.

Keep a blob loader's input bytes on the FT_Face rather than the
hb_face_t, since retained table blobs can outlive the latter. Keep the
library reference on the input blob and release it outside the static
FreeType mutex. Initialize file-loader ownership before transferring
references as well.

Add regressions for destroy-callback timing, referenced font lifetime,
and table blobs retained past the source face. All 293 tests pass,
including all eight FreeType subtests under ASan and UBSan. A separate
sanitized ownership probe passes 154 allocation-failure cases.

Discovered by Anthropic's OSS Scanner, as vulnerabilities
ANT-2026-FC0F75Y2 and ANT-2026-O2E5WAA5.

Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-10-30 14:49:21 -06:00
1 parent 703509a9d9
commit ca538ba0d1
2 files changed
+173 -33

No files matched your search

+61 -33
View File
@@ -1210,6 +1210,14 @@ _hb_ft_get_table_tags (const hb_face_t *face HB_UNUSED,
return population;
}
static hb_user_data_key_t ft_face_blob_key = {0};
static void
_destroy_blob (void *p)
{
hb_blob_destroy ((hb_blob_t *) p);
}
/**
* hb_ft_face_create:
@@ -1244,14 +1252,28 @@ hb_ft_face_create (FT_Face ft_face,
if (!ft_face->stream->read) {
hb_blob_t *blob;
blob = hb_blob_create ((const char *) ft_face->stream->base,
(unsigned int) ft_face->stream->size,
HB_MEMORY_MODE_READONLY,
ft_face, destroy);
blob = hb_blob_create_or_fail ((const char *) ft_face->stream->base,
(unsigned int) ft_face->stream->size,
HB_MEMORY_MODE_READONLY,
ft_face, destroy);
if (unlikely (!blob))
return hb_face_get_empty ();
face = hb_face_create (blob, ft_face->face_index);
hb_blob_destroy (blob);
/* hb_face_create() does not hold on to the blob if it fails to sanitize.
* Hook the blob to the face, such that @destroy is not called before the
* face is destroyed. */
if (unlikely (!hb_face_set_user_data (face, &ft_face_blob_key, blob, _destroy_blob, true)))
{
hb_blob_destroy (blob);
hb_face_destroy (face);
return hb_face_get_empty ();
}
} else {
face = hb_face_create_for_tables (_hb_ft_reference_table, ft_face, destroy);
if (unlikely (hb_face_is_immutable (face)))
return face;
hb_face_set_get_table_tags_func (face, _hb_ft_get_table_tags, ft_face, nullptr);
}
@@ -1373,10 +1395,13 @@ hb_ft_font_create (FT_Face ft_face,
hb_face_t *face;
face = hb_ft_face_create (ft_face, destroy);
/* If face creation failed, @destroy has already been called. */
if (unlikely (hb_face_is_immutable (face)))
return hb_font_get_empty ();
font = hb_font_create (face);
hb_face_destroy (face);
_hb_ft_font_set_funcs (font, ft_face, false);
hb_ft_font_changed (font);
hb_face_destroy (face);
return font;
}
@@ -1686,25 +1711,35 @@ hb_ft_face_create_from_file_or_fail (const char *file_name,
return nullptr;
}
ft_face->generic.data = ft_library;
ft_face->generic.finalizer = finalize_ft_library;
FT_Reference_Face (ft_face);
hb_face_t *face = hb_ft_face_create (ft_face, _hb_ft_face_destroy_static);
_hb_ft_face_destroy_static (ft_face);
ft_face->generic.data = ft_library;
ft_face->generic.finalizer = finalize_ft_library;
if (hb_face_is_immutable (face))
return nullptr;
return face;
}
static hb_user_data_key_t ft_blob_key = {0};
static void
_release_blob (void *arg)
{
FT_Face ft_face = (FT_Face) arg;
hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
}
static void
_destroy_blob (void *p)
_hb_ft_face_destroy_static_with_blob (void *data)
{
hb_blob_destroy ((hb_blob_t *) p);
FT_Face ft_face = (FT_Face) data;
/* See _hb_ft_font_destroy() for why the extra blob reference. */
hb_blob_t *blob = hb_blob_reference ((hb_blob_t *) ft_face->generic.data);
_hb_ft_face_destroy_static (ft_face);
hb_blob_destroy (blob);
}
/**
@@ -1750,35 +1785,28 @@ hb_ft_face_create_from_blob_or_fail (hb_blob_t *blob,
return nullptr;
}
FT_Reference_Face (ft_face);
hb_face_t *face = hb_ft_face_create (ft_face, _hb_ft_face_destroy_static);
_hb_ft_face_destroy_static (ft_face);
// Table blobs can outlive the hb_face_t, so keep the input on the FT_Face.
ft_face->generic.data = hb_blob_reference (blob);
ft_face->generic.finalizer = _release_blob;
ft_face->generic.data = ft_library;
ft_face->generic.finalizer = finalize_ft_library;
// And the FT_Library on the blob, as in hb_ft_font_set_funcs().
if (unlikely (!hb_blob_set_user_data (blob, &ft_library_key, ft_library, destroy_ft_library, true)))
{
_hb_ft_face_destroy_static_with_blob (ft_face);
destroy_ft_library (ft_library);
return nullptr;
}
FT_Reference_Face (ft_face);
hb_face_t *face = hb_ft_face_create (ft_face, _hb_ft_face_destroy_static_with_blob);
_hb_ft_face_destroy_static_with_blob (ft_face);
if (hb_face_is_immutable (face))
return nullptr;
// Hook the blob to the hb_face_t, since FT_Face still needs it.
hb_blob_reference (blob);
if (!hb_face_set_user_data (face, &ft_blob_key, blob, _destroy_blob, true))
{
hb_blob_destroy (blob);
hb_face_destroy (face);
return nullptr;
}
return face;
}
static void
_release_blob (void *arg)
{
FT_Face ft_face = (FT_Face) arg;
hb_blob_destroy ((hb_blob_t *) ft_face->generic.data);
}
/**
* hb_ft_font_set_funcs:
* @font: #hb_font_t to work upon
+112
View File
@@ -315,6 +315,116 @@ test_native_ft_create_invalid_blob_no_leak (void)
}
}
static unsigned int ft_face_destroy_count;
static void
count_ft_face_destroy (void *data HB_UNUSED)
{
ft_face_destroy_count++;
}
static void
test_native_ft_create_unsanitizable_font (void)
{
/* A font that FreeType opens but HarfBuzz fails to sanitize: numTables
* claims more table records than the file has room for. hb_face_create()
* drops the blob then, but the destroy callback handed to hb_ft_face_create()
* must still not run before the hb_face_t / hb_font_t is destroyed. */
init_freetype ();
#if GLIB_CHECK_VERSION(2,37,2)
char *path = g_test_build_filename (G_TEST_DIST, "fonts/Roboto-Regular.abc.ttf", NULL);
#else
char *path = g_strdup ("fonts/Roboto-Regular.abc.ttf");
#endif
gchar *contents;
gsize length;
GError *error = NULL;
g_assert_true (g_file_get_contents (path, &contents, &length, &error));
g_free (path);
contents[4] = contents[5] = (gchar) 0xFF;
FT_Face ft_face;
g_assert_cmpint (FT_New_Memory_Face (ft_library, (const FT_Byte *) contents, length, 0, &ft_face), ==, 0);
g_assert_cmpint (FT_Set_Char_Size (ft_face, 2000, 1000, 0, 0), ==, 0);
ft_face_destroy_count = 0;
hb_face_t *face = hb_ft_face_create (ft_face, count_ft_face_destroy);
g_assert_cmpuint (ft_face_destroy_count, ==, 0);
hb_face_destroy (face);
g_assert_cmpuint (ft_face_destroy_count, ==, 1);
ft_face_destroy_count = 0;
hb_font_t *font = hb_ft_font_create (ft_face, count_ft_face_destroy);
g_assert_cmpuint (ft_face_destroy_count, ==, 0);
hb_font_destroy (font);
g_assert_cmpuint (ft_face_destroy_count, ==, 1);
/* The font holds its own FT_Face reference; releasing ours must be fine. */
font = hb_ft_font_create_referenced (ft_face);
FT_Done_Face (ft_face);
hb_font_extents_t extents;
hb_codepoint_t glyph;
g_assert_true (hb_font_get_h_extents (font, &extents));
g_assert_true (hb_font_get_nominal_glyph (font, 'a', &glyph));
hb_font_destroy (font);
g_free (contents);
cleanup_freetype ();
}
static unsigned int ft_blob_destroy_count;
static void
count_ft_blob_destroy (void *data)
{
ft_blob_destroy_count++;
g_free (data);
}
static void
test_native_ft_create_from_blob_table_outlives_face (void)
{
/* Table blobs of a face created by hb_ft_face_create_from_blob_or_fail()
* point into the caller's blob data; they must keep that data alive on
* their own, after both the face and the caller's blob reference are gone. */
#if GLIB_CHECK_VERSION(2,37,2)
char *path = g_test_build_filename (G_TEST_DIST, "fonts/Roboto-Regular.abc.ttf", NULL);
#else
char *path = g_strdup ("fonts/Roboto-Regular.abc.ttf");
#endif
gchar *contents;
gsize length;
GError *error = NULL;
g_assert_true (g_file_get_contents (path, &contents, &length, &error));
g_free (path);
ft_blob_destroy_count = 0;
hb_blob_t *blob = hb_blob_create (contents, length, HB_MEMORY_MODE_READONLY,
contents, count_ft_blob_destroy);
hb_face_t *face = hb_ft_face_create_from_blob_or_fail (blob, 0);
g_assert_nonnull (face);
hb_blob_destroy (blob);
hb_blob_t *head = hb_face_reference_table (face, HB_TAG ('h','e','a','d'));
hb_face_destroy (face);
g_assert_cmpuint (ft_blob_destroy_count, ==, 0);
unsigned int head_length;
const char *head_data = hb_blob_get_data (head, &head_length);
g_assert_cmpuint (head_length, ==, 54);
/* head.magicNumber */
g_assert_cmpmem (head_data + 12, 4, "\x5F\x0F\x3C\xF5", 4);
hb_blob_destroy (head);
g_assert_cmpuint (ft_blob_destroy_count, ==, 1);
}
static gpointer
create_static_ft_faces (gpointer data)
{
@@ -370,6 +480,8 @@ main (int argc, char **argv)
hb_test_add (test_native_ft_set_funcs_stream_face_no_deadlock);
hb_test_add (test_native_ft_glyph_name_zero_size_probe);
hb_test_add (test_native_ft_create_invalid_blob_no_leak);
hb_test_add (test_native_ft_create_unsanitizable_font);
hb_test_add (test_native_ft_create_from_blob_table_outlives_face);
hb_test_add (test_static_ft_library_multithreaded);
return hb_test_run ();