Repository navigation
Description
Hi,
While fuzzing libschrift I found that init_font() accepts a font whose head.unitsPerEm is zero and never validates it. Every public entry point then divides by it, and in glyph_bbox() the resulting non-finite doubles are converted to int with (int) floor(...) / (int) ceil(...). A 320-byte font reproduces it against unmodified v0.10.2 (8e533fd0).
Summary
init_font() reads the field and stores it without a check (schrift.c:542, v0.10.2):
font->unitsPerEm = getu16(font, head + 18);Four public entry points divide by it:
/* schrift.c:252 sft_lmetrics() */
factor = sft->yScale / sft->font->unitsPerEm;
/* schrift.c:269 sft_gmetrics() */
double xScale = sft->xScale / sft->font->unitsPerEm;
/* schrift.c:355-356 sft_kerning() */
kerning->xShift = kerning->xShift / sft->font->unitsPerEm * sft->xScale;
kerning->yShift = kerning->yShift / sft->font->unitsPerEm * sft->yScale;
/* schrift.c:378-386 sft_render() */
transform[0] = sft->xScale / sft->font->unitsPerEm;glyph_bbox() performs the same division and immediately converts the non-finite result to int (schrift.c:997-1002):
xScale = sft->xScale / sft->font->unitsPerEm;
yScale = sft->yScale / sft->font->unitsPerEm;
box[0] = (int) floor(box[0] * xScale + sft->xOffset);
box[1] = (int) floor(box[1] * yScale + sft->yOffset);
box[2] = (int) ceil (box[2] * xScale + sft->xOffset);
box[3] = (int) ceil (box[3] * yScale + sft->yOffset);Root cause: unitsPerEm is a file-controlled 16-bit field that the loader does not reject when it is zero. With a non-zero caller scale the division yields +/-inf (and NaN when the numerator is 0.0), and converting a non-finite double to int is undefined behaviour. The same non-finite scale propagates into every metric and into the render transform.
Build the reproducer
driver.c loads the font and calls sft_gmetrics() for A-Z; schrift.c / schrift.h are the unmodified v0.10.2 sources. This build disables the unrelated signed-overflow check so that the output contains only the float-to-int conversions:
clang -O1 -g -std=c99 -pedantic -D_POSIX_C_SOURCE=1 \
-fsanitize=address,undefined -fno-sanitize=signed-integer-overflow \
-I. driver.c schrift.c -o repro_fcast -lmPOC (tested on the unmodified source)
A 320-byte TrueType font with head.unitsPerEm = 0, one outlined glyph, and a cmap that maps A to it (7 tables: cmap, glyf, head, hhea, hmtx, loca, maxp). Rebuild it from base64:
base64 -d > poc-unitsPerEm0.ttf <<'EOF'
AAEAAAAHAAAAAAAAY21hcAAAAAAAAAB8AAAALGdseWYAAAAAAAAAqAAAAChoZWFkAAAAAAAAANAAAAA2aGhlYQAAAAAAAAEIAAAAJGhtdHgAAAAAAAABLAAAAARsb2NhAAAAAAAAATAAAAAGbWF4cAAAAAAAAAE4AAAABgAAAAEAAwABAAAADAAEACAAAAAEAAQAAQACAEH//wAAAEH////AAAAAAAAAAAAAAAABAAAAAAAKAAoAAwAAAQEBAQAAAAoAAP/2AAAAAAAKAAAAAAABAAAAAAAAAAAAAF8PPPUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAKAAAAAAAAAAAAAAAAAAAAAAMg/zgAAAPoA+gAAAAAAAAAAAAAAAAAAAAAAAAAAAABAlgAAAAAAAIAEwAAAABQAAACAAA=
EOFsha256sum poc-unitsPerEm0.ttf
# must print: 9b60a83ec9cd206cf6b919205c278d9b15916a10b53a951edf89b13ac2c5e411
ls -l poc-unitsPerEm0.ttf # 320 bytesRun:
ASAN_OPTIONS=detect_leaks=0:abort_on_error=0:symbolize=1 \
UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=0 \
./repro_fcast poc-unitsPerEm0.ttfTrigger result
Verified on unmodified libschrift v0.10.2 (8e533fd0), clang 14, -fsanitize=address,undefined -fno-sanitize=signed-integer-overflow:
src/schrift.c:999:11: runtime error: -nan is outside the range of representable values of type 'int'
#0 0x55e46fadb4b2 in glyph_bbox ./src/schrift.c:999:11
#1 0x55e46fad9196 in sft_gmetrics ./src/schrift.c:284:6
#2 0x55e46fad6365 in main ./driver.c:32:7
#3 0x7fc1d9427d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#4 0x7fc1d9427e3f in __libc_start_main csu/../csu/libc-start.c:392:3
#5 0x55e46fa183b4 in _start (./repro_fcast+0x243b4) (BuildId: 2e4140065548dba35eccd47b749153eefa28c3fc)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/schrift.c:999:11 in
src/schrift.c:1000:11: runtime error: -nan is outside the range of representable values of type 'int'
#0 0x55e46fadb4d4 in glyph_bbox ./src/schrift.c:1000:11
#1 0x55e46fad9196 in sft_gmetrics ./src/schrift.c:284:6
#2 0x55e46fad6365 in main ./driver.c:32:7
#3 0x7fc1d9427d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#4 0x7fc1d9427e3f in __libc_start_main csu/../csu/libc-start.c:392:3
#5 0x55e46fa183b4 in _start (./repro_fcast+0x243b4) (BuildId: 2e4140065548dba35eccd47b749153eefa28c3fc)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/schrift.c:1000:11 in
src/schrift.c:1001:11: runtime error: inf is outside the range of representable values of type 'int'
#0 0x55e46fadb4f6 in glyph_bbox ./src/schrift.c:1001:11
#1 0x55e46fad9196 in sft_gmetrics ./src/schrift.c:284:6
#2 0x55e46fad6365 in main ./driver.c:32:7
#3 0x7fc1d9427d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#4 0x7fc1d9427e3f in __libc_start_main csu/../csu/libc-start.c:392:3
#5 0x55e46fa183b4 in _start (./repro_fcast+0x243b4) (BuildId: 2e4140065548dba35eccd47b749153eefa28c3fc)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/schrift.c:1001:11 in
src/schrift.c:1002:11: runtime error: inf is outside the range of representable values of type 'int'
#0 0x55e46fadb518 in glyph_bbox ./src/schrift.c:1002:11
#1 0x55e46fad9196 in sft_gmetrics ./src/schrift.c:284:6
#2 0x55e46fad6365 in main ./driver.c:32:7
#3 0x7fc1d9427d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#4 0x7fc1d9427e3f in __libc_start_main csu/../csu/libc-start.c:392:3
#5 0x55e46fa183b4 in _start (./repro_fcast+0x243b4) (BuildId: 2e4140065548dba35eccd47b749153eefa28c3fc)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/schrift.c:1002:11 in
[repro] glyph 1 measured
[repro] done
glyph_bbox() is reached from sft_gmetrics() (schrift.c:284). The same non-finite scale is produced at the other three call sites, so sft_lmetrics(), sft_kerning() and sft_render() inherit the defect: any application that calls them on a font with unitsPerEm == 0 performs the same division by zero.
Impact
A downloaded .ttf or .otf is the only attacker input. Every API that converts a font unit into user space divides by this unvalidated value, and in glyph_bbox() the result is converted straight to int, which is undefined for inf and NaN. The metrics the caller receives (minWidth, minHeight, leftSideBearing, yOffset, advanceWidth) and the render transform are all derived from the non-finite scale, so the values used for layout and for buffer sizing are undefined. In a build that traps undefined behaviour the process aborts on the font; otherwise the compiler is entitled to assume the conversion can never receive a non-finite value.
Suggested fix
Reject the divisor when the font is loaded:
/* init_font(), after reading head */
font->unitsPerEm = getu16(font, head + 18);
if (font->unitsPerEm == 0)
return -1;Independently, make the conversion total in glyph_bbox(): compute the scaled coordinate in double, reject non-finite values (if (!isfinite(v)) return -1;) and clamp to the int range before the cast.
Activity
1820893135-pixel commentedon Oct 24, 2026
Fixed in #49.
init_font()now rejects a font withhead.unitsPerEm == 0, andglyph_bbox()computes the scaled coordinates indouble, rejects non-finite values, and clamps the rest to theintrange before converting. Verified: the 320-byte font is rejected at load (no sanitizer report), and metrics/kerning/rendered bitmaps for three real fonts are byte-identical to the unmodified library.