Fedora Account System
Red Hat Associate
Red Hat Customer
Disclaimer: Community trackers are created by Red Hat Product Security team on a best effort basis. Package maintainers are required to ascertain if the flaw indeed affects their package, before starting the update process. FreeTypeOpenInstance() allocates FTInstanceRec with a bare malloc() and then assigns instance->strike_index only inside an "if (FT_IS_SFNT(face->face))" block. There is no else branch. A non-SFNT face -- any bitmap font that FreeType's own bitmap driver claims, which libXfont2 reaches whenever such a file carries a FreeType renderer suffix such as .pfb -- leaves the field holding raw heap bytes. The field is then passed unchecked to FT_Do_SBit_Metrics() and used directly as a 64-bit array subscript: FT_Bitmap_Size* sz = &ft_face->available_sizes[strike_index]; The 0xFFFFU sentinel check on the previous line is not a bounds check: it filters exactly one value out of 2^64, so a raw heap word almost always passes it. With num_fixed_sizes == 1 the only in-bounds subscript is 0; any other value causes an out-of-bounds read that crashes the X server. A local client can reach this path with ordinary core protocol requests: spray the server heap with ChangeProperty, point the font path at a directory it controls with SetFontPath, then OpenFont. The font file does not need to be crafted -- an unmodified vendor-shipped .pcf font renamed to .pfb is sufficient. CWE-457 (use of uninitialised variable) leading to CWE-125 (out-of-bounds read). Fixed in: libXfont2-2.0.10 Fixes: - https://gitlab.freedesktop.org/xorg/lib/libxfont/-/commit/TBD Acknowledgements: Apex Security Research (apex-security-research) * Patch From 0ccab39c617c328b4b126a033656e9a9ece44772 Mon Sep 17 00:00:00 2001 From: Peter Hutterer <peter.hutterer> Date: Mon, 9 Nov 2026 14:37:01 +1000 Subject: [PATCH libXfont 2/2] FreeType: always initialise strike_index When FreeTypeOpenInstance() allocates FTInstanceRec, the instance->strike_index field is the only one left unset by default. By renaming a BDF file to a FreeType-backed .pfb file we can trick libXfont2 to use the FreeType backend, not its own bitmap backend, resulting in a "non-SFNT face". This leaves the strike_index field unset. Later when we call FT_Do_SBit_Metrics() strike_index is used directly as an array subscript, leaving us with potential OOB reads: FT_Bitmap_Size* sz = &ft_face->available_sizes[strike_index]; This vulnerability was discovered by: Apex Security Research (apex-security-research) CVE-2026-109920 Assisted-by: Claude:claude-opus-4-6 --- Makefile.am | 12 ++ src/FreeType/ftfuncs.c | 6 +- test/data/ft-strike-index/fonts.dir | 2 + test/data/ft-strike-index/strike-test.bdf | 52 ++++++++ test/data/ft-strike-index/strike-test.pfb | 52 ++++++++ test/test-freetype-strike-index.c | 154 ++++++++++++++++++++++ 6 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 test/data/ft-strike-index/fonts.dir create mode 100644 test/data/ft-strike-index/strike-test.bdf create mode 100644 test/data/ft-strike-index/strike-test.pfb create mode 100644 test/test-freetype-strike-index.c diff --git ./Makefile.am ../Makefile.am index 2f116d2889da..75c914a94e35 100644 --- ./Makefile.am +++ ../Makefile.am @@ -177,16 +177,24 @@ check_PROGRAMS += test-fserve-read-glyphs # The test #includes fserve.c directly to access static functions so # we statically link against libXfont2.a. test_fserve_read_glyphs_SOURCES = test/test-fserve-read-glyphs.c $(TEST_UTIL_SRCS) test_fserve_read_glyphs_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/include -I$(top_srcdir)/src/fc test_fserve_read_glyphs_LDFLAGS = -static test_fserve_read_glyphs_LDADD = libXfont2.la $(LTLIBOBJS) endif XFONT_FC +if XFONT_FREETYPE +TESTS += test-freetype-strike-index +check_PROGRAMS += test-freetype-strike-index + +test_freetype_strike_index_SOURCES = test/test-freetype-strike-index.c $(TEST_UTIL_SRCS) +test_freetype_strike_index_LDADD = libXfont2.la $(LTLIBOBJS) +endif XFONT_FREETYPE + # PCF parser security regression tests if HAVE_PYTHON TESTS += test-pcf-repad test-pcf-props test-pcf-bitmapscale \ test-pcf-encoding test-pcf-norepad test-pcf-negskip check_PROGRAMS += test-pcf-repad test-pcf-props test-pcf-bitmapscale \ test-pcf-encoding test-pcf-norepad test-pcf-negskip PCF_TEST_SRCS = test/pcf-test-common.c test/pcf-test-common.h $(TEST_UTIL_SRCS) @@ -274,16 +282,20 @@ endif HAVE_PYTHON EXTRA_DIST += test/gen_evil_pcf_repad.py \ test/gen_evil_pcf_props.py \ test/gen_evil_pcf_bitmapscale.py \ test/gen_evil_pcf_encoding.py \ test/gen_evil_pcf_norepad.py \ test/gen_evil_pcf_negskip.py \ test/gen_evil_snf_nprops_overflow.py +EXTRA_DIST += test/data/ft-strike-index/strike-test.bdf \ + test/data/ft-strike-index/strike-test.pfb \ + test/data/ft-strike-index/fonts.dir + MAINTAINERCLEANFILES = ChangeLog INSTALL .PHONY: ChangeLog INSTALL INSTALL: $(INSTALL_CMD) diff --git ./src/FreeType/ftfuncs.c ../src/FreeType/ftfuncs.c index 0d529a8cee5b..d6117b341c11 100644 --- ./src/FreeType/ftfuncs.c +++ ../src/FreeType/ftfuncs.c @@ -418,16 +418,17 @@ FreeTypeOpenInstance(FTInstancePtr *instance_return, FTFacePtr face, if(instance == NULL) { return AllocError; } instance->refcount = 1; instance->face = face; instance->load_flags = load_flags; + instance->strike_index = 0xFFFFU; instance->spacing = spacing; /* Actual spacing */ instance->pixel_size =0; instance->pixel_width_unit_x =0; instance->pixel_width_unit_y =0; instance->charcellMetrics = NULL; instance->averageWidth = 0; instance->rawAverageWidth = 0; instance->forceConstantMetrics = NULL; @@ -473,18 +474,16 @@ FreeTypeOpenInstance(FTInstancePtr *instance_return, FTFacePtr face, return FTtoXReturnCode(ftrc); } if( FT_IS_SFNT( face->face ) ) { #if 1 FT_F26Dot6 tt_char_width, tt_char_height, tt_dim_x, tt_dim_y; FT_Int nn; - instance->strike_index=0xFFFFU; - tt_char_width = (FT_F26Dot6)(trans->scale*(1<<6) + 0.5); tt_char_height = (FT_F26Dot6)(trans->scale*(1<<6) + 0.5); tt_dim_x = FLOOR64( ( tt_char_width * trans->xres + 36 ) / 72 + 32 ); tt_dim_y = FLOOR64( ( tt_char_height * trans->yres + 36 ) / 72 + 32 ); if ( tt_dim_x && !tt_dim_y ) tt_dim_y = tt_dim_x; @@ -969,17 +968,18 @@ ft_get_very_lazy_bbox( FT_UInt index, } static FT_Error FT_Do_SBit_Metrics( FT_Face ft_face, FT_Size ft_size, FT_ULong strike_index, FT_UShort glyph_index, FT_Glyph_Metrics *metrics_return, int *sbitchk_incomplete_but_exist ) { #if 1 - if ( strike_index != 0xFFFFU && ft_face->available_sizes != NULL ) + if ( strike_index != 0xFFFFU && ft_face->available_sizes != NULL && + strike_index < (FT_ULong) ft_face->num_fixed_sizes ) { FT_Error error; FT_Bitmap_Size* sz = &ft_face->available_sizes[strike_index]; error = FT_Set_Pixel_Sizes( ft_face, sz->x_ppem/64, sz->y_ppem/64 ); if ( !error ) { error = FT_Load_Glyph( ft_face, glyph_index, FT_LOAD_SBITS_ONLY ); diff --git ./test/data/ft-strike-index/fonts.dir ../test/data/ft-strike-index/fonts.dir new file mode 100644 index 000000000000..f0aae020a343 --- /dev/null +++ ../test/data/ft-strike-index/fonts.dir @@ -0,0 +1,2 @@ +1 +strike-test.pfb -misc-striketest-medium-r-normal--0-0-0-0-p-0-iso10646-1 diff --git ./test/data/ft-strike-index/strike-test.bdf ../test/data/ft-strike-index/strike-test.bdf new file mode 100644 index 000000000000..bf31e45d58e8 --- /dev/null +++ ../test/data/ft-strike-index/strike-test.bdf @@ -0,0 +1,52 @@ +STARTFONT 2.1 +FONT -misc-striketest-medium-r-normal--10-100-75-75-p-100-iso10646-1 +SIZE 10 75 75 +FONTBOUNDINGBOX 10 10 0 -2 +STARTPROPERTIES 10 +FONT_ASCENT 8 +FONT_DESCENT 2 +POINT_SIZE 100 +RESOLUTION_X 75 +RESOLUTION_Y 75 +AVERAGE_WIDTH 100 +PIXEL_SIZE 10 +SPACING "P" +CHARSET_REGISTRY "ISO10646" +CHARSET_ENCODING "1" +ENDPROPERTIES +CHARS 2 +STARTCHAR space +ENCODING 32 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +ENDCHAR +STARTCHAR exclam +ENCODING 33 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0C00 +0C00 +0C00 +0C00 +0C00 +0C00 +0000 +0000 +0C00 +0000 +ENDCHAR +ENDFONT diff --git ./test/data/ft-strike-index/strike-test.pfb ../test/data/ft-strike-index/strike-test.pfb new file mode 100644 index 000000000000..bf31e45d58e8 --- /dev/null +++ ../test/data/ft-strike-index/strike-test.pfb @@ -0,0 +1,52 @@ +STARTFONT 2.1 +FONT -misc-striketest-medium-r-normal--10-100-75-75-p-100-iso10646-1 +SIZE 10 75 75 +FONTBOUNDINGBOX 10 10 0 -2 +STARTPROPERTIES 10 +FONT_ASCENT 8 +FONT_DESCENT 2 +POINT_SIZE 100 +RESOLUTION_X 75 +RESOLUTION_Y 75 +AVERAGE_WIDTH 100 +PIXEL_SIZE 10 +SPACING "P" +CHARSET_REGISTRY "ISO10646" +CHARSET_ENCODING "1" +ENDPROPERTIES +CHARS 2 +STARTCHAR space +ENCODING 32 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +ENDCHAR +STARTCHAR exclam +ENCODING 33 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0C00 +0C00 +0C00 +0C00 +0C00 +0C00 +0000 +0000 +0C00 +0000 +ENDCHAR +ENDFONT diff --git ./test/test-freetype-strike-index.c ../test/test-freetype-strike-index.c new file mode 100644 index 000000000000..8b73210254ed --- /dev/null +++ ../test/test-freetype-strike-index.c @@ -0,0 +1,154 @@ +/* + * Test for uninitialised strike_index in FreeTypeOpenInstance(). + * + * FreeTypeOpenInstance() allocates FTInstanceRec with a bare malloc() + * and assigns instance->strike_index only inside "if (FT_IS_SFNT())". + * A non-SFNT face -- any bitmap font that FreeType's bitmap driver + * claims, which libXfont2 reaches whenever such a file carries a + * FreeType renderer suffix such as .pfb -- left the field holding raw + * heap bytes. The field was then passed to FT_Do_SBit_Metrics() and + * used as an array subscript into ft_face->available_sizes[], causing + * an out-of-bounds read. + * + * This test opens a BDF font file via a .pfb filename (which routes it + * through the FreeType backend instead of the bitmap backend). Before + * the fix, this would use an uninitialised strike_index value as an + * array subscript, causing a crash or undefined behaviour. After the + * fix, the sentinel value 0xFFFFU is set unconditionally and the bounds + * check in FT_Do_SBit_Metrics() prevents any out-of-bounds access. + * + * Test font: test/data/ft-strike-index/strike-test.pfb (a BDF font + * with a .pfb extension, static file checked into the repository) + */ + +/* + * Copyright (c) 2026, Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "utils/font-test-utils.h" +#include "src/util/replace.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <X11/fonts/fsmasks.h> + +static int +try_open_font(xfont2_fpe_funcs_rec const **fpe_functions, + FontPathElementPtr fpe, const char *xlfd) +{ + FontPtr pFont = NULL; + char *aliasName = NULL; + int result; + + fsBitmapFormat format = BitmapFormatScanlinePad32 | + BitmapFormatBitOrderLSB | + BitmapFormatByteOrderLSB | + BitmapFormatImageRectMin; + fsBitmapFormatMask fmask = BitmapFormatMaskScanLinePad | + BitmapFormatMaskBit | + BitmapFormatMaskByte | + BitmapFormatMaskImageRectangle; + + result = (*fpe_functions[fpe->type]->open_font) + (NULL, fpe, 0, xlfd, strlen(xlfd), + format, fmask, (XID)1, &pFont, &aliasName, NULL); + + if (result == Successful && pFont) { + (*fpe_functions[fpe->type]->close_font)(fpe, pFont); + } + + return result; +} + +int +main(int argc, char **argv) +{ + xfont2_fpe_funcs_rec const **fpe_functions; + int fpe_function_count; + FontPathElementPtr *fpe_list; + const char *srcdir; + char fontdir[PATH_MAX]; + char abspath[PATH_MAX]; + const char *paths[1]; + int num_fpes = 1; + int result; + + /* The .pfb suffix routes the font through the FreeType backend. + * The font is actually a BDF, so FreeType's bitmap driver parses + * it and produces a non-SFNT face -- the exact path that triggered + * the uninitialised strike_index bug. */ + const char *xlfd = + "-misc-striketest-medium-r-normal--10-100-75-75-p-100-iso10646-1"; + + srcdir = getenv("srcdir"); + if (!srcdir) + srcdir = "."; + + snprintf(fontdir, sizeof(fontdir), "%s/test/data/ft-strike-index", srcdir); + + /* Font path must be absolute */ + if (fontdir[0] != '/') { + if (!realpath(fontdir, abspath)) { + fprintf(stderr, "FAIL: could not resolve path %s\n", fontdir); + return 1; + } + memcpy(fontdir, abspath, sizeof(fontdir)); + } + + fpe_functions = init_font_handlers(&fpe_function_count); + + paths[0] = fontdir; + fpe_list = init_font_paths(paths, &num_fpes); + if (num_fpes != 1) { + fprintf(stderr, "FAIL: could not initialize font path %s\n", fontdir); + return 1; + } + + /* This is the critical call: opening a BDF-as-.pfb font through + * the FreeType backend exercises the non-SFNT path in + * FreeTypeOpenInstance(). Before the fix, strike_index would be + * uninitialised and used as an array subscript in + * FT_Do_SBit_Metrics(), causing undefined behaviour (typically a + * crash). After the fix, the sentinel is set and the bounds check + * prevents any out-of-bounds access. */ + result = try_open_font(fpe_functions, fpe_list[0], xlfd); + + if (result == Successful) { + printf("PASS: BDF-as-.pfb font opened successfully " + "(strike_index properly initialised)\n"); + } else { + /* The font may fail to open for legitimate reasons (e.g. the + * encoding doesn't match), but it must not crash. If we reach + * this point, the process survived -- the uninitialised + * strike_index did not cause a fatal OOB access. */ + printf("PASS: BDF-as-.pfb font open returned %d " + "(no crash from uninitialised strike_index)\n", result); + } + + (*fpe_functions[fpe_list[0]->type]->free_fpe)(fpe_list[0]); + free(fpe_list[0]->name); + free(fpe_list[0]); + free(fpe_list); + + return 0; +} -- 2.56.0 #+begin_src diff --- Makefile.am | 12 ++ src/FreeType/ftfuncs.c | 6 +- test/data/ft-strike-index/fonts.dir | 2 + test/data/ft-strike-index/strike-test.bdf | 52 ++++++++ test/data/ft-strike-index/strike-test.pfb | 52 ++++++++ test/test-freetype-strike-index.c | 154 ++++++++++++++++++++++ 6 files changed, 275 insertions(+), 3 deletions(-) create mode 100644 test/data/ft-strike-index/fonts.dir create mode 100644 test/data/ft-strike-index/strike-test.bdf create mode 100644 test/data/ft-strike-index/strike-test.pfb create mode 100644 test/test-freetype-strike-index.c diff --git ./Makefile.am ../Makefile.am index 2f116d2889da..75c914a94e35 100644 --- ./Makefile.am +++ ../Makefile.am @@ -177,16 +177,24 @@ check_PROGRAMS += test-fserve-read-glyphs # The test #includes fserve.c directly to access static functions so # we statically link against libXfont2.a. test_fserve_read_glyphs_SOURCES = test/test-fserve-read-glyphs.c $(TEST_UTIL_SRCS) test_fserve_read_glyphs_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/include -I$(top_srcdir)/src/fc test_fserve_read_glyphs_LDFLAGS = -static test_fserve_read_glyphs_LDADD = libXfont2.la $(LTLIBOBJS) endif XFONT_FC +if XFONT_FREETYPE +TESTS += test-freetype-strike-index +check_PROGRAMS += test-freetype-strike-index + +test_freetype_strike_index_SOURCES = test/test-freetype-strike-index.c $(TEST_UTIL_SRCS) +test_freetype_strike_index_LDADD = libXfont2.la $(LTLIBOBJS) +endif XFONT_FREETYPE + # PCF parser security regression tests if HAVE_PYTHON TESTS += test-pcf-repad test-pcf-props test-pcf-bitmapscale \ test-pcf-encoding test-pcf-norepad test-pcf-negskip check_PROGRAMS += test-pcf-repad test-pcf-props test-pcf-bitmapscale \ test-pcf-encoding test-pcf-norepad test-pcf-negskip PCF_TEST_SRCS = test/pcf-test-common.c test/pcf-test-common.h $(TEST_UTIL_SRCS) @@ -274,16 +282,20 @@ endif HAVE_PYTHON EXTRA_DIST += test/gen_evil_pcf_repad.py \ test/gen_evil_pcf_props.py \ test/gen_evil_pcf_bitmapscale.py \ test/gen_evil_pcf_encoding.py \ test/gen_evil_pcf_norepad.py \ test/gen_evil_pcf_negskip.py \ test/gen_evil_snf_nprops_overflow.py +EXTRA_DIST += test/data/ft-strike-index/strike-test.bdf \ + test/data/ft-strike-index/strike-test.pfb \ + test/data/ft-strike-index/fonts.dir + MAINTAINERCLEANFILES = ChangeLog INSTALL .PHONY: ChangeLog INSTALL INSTALL: $(INSTALL_CMD) diff --git ./src/FreeType/ftfuncs.c ../src/FreeType/ftfuncs.c index 0d529a8cee5b..d6117b341c11 100644 --- ./src/FreeType/ftfuncs.c +++ ../src/FreeType/ftfuncs.c @@ -418,16 +418,17 @@ FreeTypeOpenInstance(FTInstancePtr *instance_return, FTFacePtr face, if(instance == NULL) { return AllocError; } instance->refcount = 1; instance->face = face; instance->load_flags = load_flags; + instance->strike_index = 0xFFFFU; instance->spacing = spacing; /* Actual spacing */ instance->pixel_size =0; instance->pixel_width_unit_x =0; instance->pixel_width_unit_y =0; instance->charcellMetrics = NULL; instance->averageWidth = 0; instance->rawAverageWidth = 0; instance->forceConstantMetrics = NULL; @@ -473,18 +474,16 @@ FreeTypeOpenInstance(FTInstancePtr *instance_return, FTFacePtr face, return FTtoXReturnCode(ftrc); } if( FT_IS_SFNT( face->face ) ) { #if 1 FT_F26Dot6 tt_char_width, tt_char_height, tt_dim_x, tt_dim_y; FT_Int nn; - instance->strike_index=0xFFFFU; - tt_char_width = (FT_F26Dot6)(trans->scale*(1<<6) + 0.5); tt_char_height = (FT_F26Dot6)(trans->scale*(1<<6) + 0.5); tt_dim_x = FLOOR64( ( tt_char_width * trans->xres + 36 ) / 72 + 32 ); tt_dim_y = FLOOR64( ( tt_char_height * trans->yres + 36 ) / 72 + 32 ); if ( tt_dim_x && !tt_dim_y ) tt_dim_y = tt_dim_x; @@ -969,17 +968,18 @@ ft_get_very_lazy_bbox( FT_UInt index, } static FT_Error FT_Do_SBit_Metrics( FT_Face ft_face, FT_Size ft_size, FT_ULong strike_index, FT_UShort glyph_index, FT_Glyph_Metrics *metrics_return, int *sbitchk_incomplete_but_exist ) { #if 1 - if ( strike_index != 0xFFFFU && ft_face->available_sizes != NULL ) + if ( strike_index != 0xFFFFU && ft_face->available_sizes != NULL && + strike_index < (FT_ULong) ft_face->num_fixed_sizes ) { FT_Error error; FT_Bitmap_Size* sz = &ft_face->available_sizes[strike_index]; error = FT_Set_Pixel_Sizes( ft_face, sz->x_ppem/64, sz->y_ppem/64 ); if ( !error ) { error = FT_Load_Glyph( ft_face, glyph_index, FT_LOAD_SBITS_ONLY ); diff --git ./test/data/ft-strike-index/fonts.dir ../test/data/ft-strike-index/fonts.dir new file mode 100644 index 000000000000..f0aae020a343 --- /dev/null +++ ../test/data/ft-strike-index/fonts.dir @@ -0,0 +1,2 @@ +1 +strike-test.pfb -misc-striketest-medium-r-normal--0-0-0-0-p-0-iso10646-1 diff --git ./test/data/ft-strike-index/strike-test.bdf ../test/data/ft-strike-index/strike-test.bdf new file mode 100644 index 000000000000..bf31e45d58e8 --- /dev/null +++ ../test/data/ft-strike-index/strike-test.bdf @@ -0,0 +1,52 @@ +STARTFONT 2.1 +FONT -misc-striketest-medium-r-normal--10-100-75-75-p-100-iso10646-1 +SIZE 10 75 75 +FONTBOUNDINGBOX 10 10 0 -2 +STARTPROPERTIES 10 +FONT_ASCENT 8 +FONT_DESCENT 2 +POINT_SIZE 100 +RESOLUTION_X 75 +RESOLUTION_Y 75 +AVERAGE_WIDTH 100 +PIXEL_SIZE 10 +SPACING "P" +CHARSET_REGISTRY "ISO10646" +CHARSET_ENCODING "1" +ENDPROPERTIES +CHARS 2 +STARTCHAR space +ENCODING 32 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +ENDCHAR +STARTCHAR exclam +ENCODING 33 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0C00 +0C00 +0C00 +0C00 +0C00 +0C00 +0000 +0000 +0C00 +0000 +ENDCHAR +ENDFONT diff --git ./test/data/ft-strike-index/strike-test.pfb ../test/data/ft-strike-index/strike-test.pfb new file mode 100644 index 000000000000..bf31e45d58e8 --- /dev/null +++ ../test/data/ft-strike-index/strike-test.pfb @@ -0,0 +1,52 @@ +STARTFONT 2.1 +FONT -misc-striketest-medium-r-normal--10-100-75-75-p-100-iso10646-1 +SIZE 10 75 75 +FONTBOUNDINGBOX 10 10 0 -2 +STARTPROPERTIES 10 +FONT_ASCENT 8 +FONT_DESCENT 2 +POINT_SIZE 100 +RESOLUTION_X 75 +RESOLUTION_Y 75 +AVERAGE_WIDTH 100 +PIXEL_SIZE 10 +SPACING "P" +CHARSET_REGISTRY "ISO10646" +CHARSET_ENCODING "1" +ENDPROPERTIES +CHARS 2 +STARTCHAR space +ENCODING 32 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +ENDCHAR +STARTCHAR exclam +ENCODING 33 +SWIDTH 720 0 +DWIDTH 10 0 +BBX 10 10 0 -2 +BITMAP +0C00 +0C00 +0C00 +0C00 +0C00 +0C00 +0000 +0000 +0C00 +0000 +ENDCHAR +ENDFONT diff --git ./test/test-freetype-strike-index.c ../test/test-freetype-strike-index.c new file mode 100644 index 000000000000..8b73210254ed --- /dev/null +++ ../test/test-freetype-strike-index.c @@ -0,0 +1,154 @@ +/* + * Test for uninitialised strike_index in FreeTypeOpenInstance(). + * + * FreeTypeOpenInstance() allocates FTInstanceRec with a bare malloc() + * and assigns instance->strike_index only inside "if (FT_IS_SFNT())". + * A non-SFNT face -- any bitmap font that FreeType's bitmap driver + * claims, which libXfont2 reaches whenever such a file carries a + * FreeType renderer suffix such as .pfb -- left the field holding raw + * heap bytes. The field was then passed to FT_Do_SBit_Metrics() and + * used as an array subscript into ft_face->available_sizes[], causing + * an out-of-bounds read. + * + * This test opens a BDF font file via a .pfb filename (which routes it + * through the FreeType backend instead of the bitmap backend). Before + * the fix, this would use an uninitialised strike_index value as an + * array subscript, causing a crash or undefined behaviour. After the + * fix, the sentinel value 0xFFFFU is set unconditionally and the bounds + * check in FT_Do_SBit_Metrics() prevents any out-of-bounds access. + * + * Test font: test/data/ft-strike-index/strike-test.pfb (a BDF font + * with a .pfb extension, static file checked into the repository) + */ + +/* + * Copyright (c) 2026, Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "utils/font-test-utils.h" +#include "src/util/replace.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <X11/fonts/fsmasks.h> + +static int +try_open_font(xfont2_fpe_funcs_rec const **fpe_functions, + FontPathElementPtr fpe, const char *xlfd) +{ + FontPtr pFont = NULL; + char *aliasName = NULL; + int result; + + fsBitmapFormat format = BitmapFormatScanlinePad32 | + BitmapFormatBitOrderLSB | + BitmapFormatByteOrderLSB | + BitmapFormatImageRectMin; + fsBitmapFormatMask fmask = BitmapFormatMaskScanLinePad | + BitmapFormatMaskBit | + BitmapFormatMaskByte | + BitmapFormatMaskImageRectangle; + + result = (*fpe_functions[fpe->type]->open_font) + (NULL, fpe, 0, xlfd, strlen(xlfd), + format, fmask, (XID)1, &pFont, &aliasName, NULL); + + if (result == Successful && pFont) { + (*fpe_functions[fpe->type]->close_font)(fpe, pFont); + } + + return result; +} + +int +main(int argc, char **argv) +{ + xfont2_fpe_funcs_rec const **fpe_functions; + int fpe_function_count; + FontPathElementPtr *fpe_list; + const char *srcdir; + char fontdir[PATH_MAX]; + char abspath[PATH_MAX]; + const char *paths[1]; + int num_fpes = 1; + int result; + + /* The .pfb suffix routes the font through the FreeType backend. + * The font is actually a BDF, so FreeType's bitmap driver parses + * it and produces a non-SFNT face -- the exact path that triggered + * the uninitialised strike_index bug. */ + const char *xlfd = + "-misc-striketest-medium-r-normal--10-100-75-75-p-100-iso10646-1"; + + srcdir = getenv("srcdir"); + if (!srcdir) + srcdir = "."; + + snprintf(fontdir, sizeof(fontdir), "%s/test/data/ft-strike-index", srcdir); + + /* Font path must be absolute */ + if (fontdir[0] != '/') { + if (!realpath(fontdir, abspath)) { + fprintf(stderr, "FAIL: could not resolve path %s\n", fontdir); + return 1; + } + memcpy(fontdir, abspath, sizeof(fontdir)); + } + + fpe_functions = init_font_handlers(&fpe_function_count); + + paths[0] = fontdir; + fpe_list = init_font_paths(paths, &num_fpes); + if (num_fpes != 1) { + fprintf(stderr, "FAIL: could not initialize font path %s\n", fontdir); + return 1; + } + + /* This is the critical call: opening a BDF-as-.pfb font through + * the FreeType backend exercises the non-SFNT path in + * FreeTypeOpenInstance(). Before the fix, strike_index would be + * uninitialised and used as an array subscript in + * FT_Do_SBit_Metrics(), causing undefined behaviour (typically a + * crash). After the fix, the sentinel is set and the bounds check + * prevents any out-of-bounds access. */ + result = try_open_font(fpe_functions, fpe_list[0], xlfd); + + if (result == Successful) { + printf("PASS: BDF-as-.pfb font opened successfully " + "(strike_index properly initialised)\n"); + } else { + /* The font may fail to open for legitimate reasons (e.g. the + * encoding doesn't match), but it must not crash. If we reach + * this point, the process survived -- the uninitialised + * strike_index did not cause a fatal OOB access. */ + printf("PASS: BDF-as-.pfb font open returned %d " + "(no crash from uninitialised strike_index)\n", result); + } + + (*fpe_functions[fpe_list[0]->type]->free_fpe)(fpe_list[0]); + free(fpe_list[0]->name); + free(fpe_list[0]); + free(fpe_list); + + return 0; +} -- 2.56.0 #+end_src