Skip to content

[Bug]libschrift: sft_gmetrics() negates INT_MIN when the scaled glyph box saturates -> signed integer overflow (CWE-190) #48

Open

Description

@1820893135-pixel

Hi,

While fuzzing libschrift I found that sft_gmetrics() negates the bottom edge of the scaled glyph box without checking it. The box comes from glyph_bbox(), whose float-to-int conversions yield INT_MIN when the scale is non-finite, and -INT_MIN is not representable in int. A 320-byte font reproduces it against unmodified v0.10.2 (8e533fd0).

Summary

sft_gmetrics() ends with (schrift.c:288, v0.10.2):

metrics->yOffset = sft->flags & SFT_DOWNWARD_Y ? -bbox[3] : bbox[1];

bbox[3] is produced by glyph_bbox() (schrift.c:1002):

box[3] = (int) ceil (box[3] * yScale + sft->yOffset);

Root cause: yScale is sft->yScale / sft->font->unitsPerEm (schrift.c:997-998), so a font with head.unitsPerEm == 0 makes it non-finite. On x86-64 the cvttsd2si conversion of a non-finite or out-of-range double produces INT_MIN (0x80000000), which is the value the trace below shows. sft_gmetrics() then computes -bbox[3] for the default SFT_DOWNWARD_Y flag, and negating INT_MIN is signed integer overflow, i.e. undefined behaviour executed inside the library's own public entry point.

This is a separate defect from the conversion: fixing the conversion alone does not remove the negation, and fixing the negation alone does not remove the conversion. Both are reachable from the same font.

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 float-cast check so that the output isolates the negation:

clang -O1 -g -std=c99 -pedantic -D_POSIX_C_SOURCE=1 \
      -fsanitize=address,undefined -fno-sanitize=float-cast-overflow \
      -I. driver.c schrift.c -o repro_negate -lm

POC (tested on the unmodified source)

Same 320-byte font as the companion report: head.unitsPerEm = 0, one outlined glyph mapped at A. Rebuild it from base64:

base64 -d > poc-unitsPerEm0.ttf <<'EOF'
AAEAAAAHAAAAAAAAY21hcAAAAAAAAAB8AAAALGdseWYAAAAAAAAAqAAAAChoZWFkAAAAAAAAANAAAAA2aGhlYQAAAAAAAAEIAAAAJGhtdHgAAAAAAAABLAAAAARsb2NhAAAAAAAAATAAAAAGbWF4cAAAAAAAAAE4AAAABgAAAAEAAwABAAAADAAEACAAAAAEAAQAAQACAEH//wAAAEH////AAAAAAAAAAAAAAAABAAAAAAAKAAoAAwAAAQEBAQAAAAoAAP/2AAAAAAAKAAAAAAABAAAAAAAAAAAAAF8PPPUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAKAAAAAAAAAAAAAAAAAAAAAAMg/zgAAAPoA+gAAAAAAAAAAAAAAAAAAAAAAAAAAAABAlgAAAAAAAIAEwAAAABQAAACAAA=
EOF
sha256sum poc-unitsPerEm0.ttf
# must print: 9b60a83ec9cd206cf6b919205c278d9b15916a10b53a951edf89b13ac2c5e411
ls -l poc-unitsPerEm0.ttf     # 320 bytes

Run:

ASAN_OPTIONS=detect_leaks=0:abort_on_error=0:symbolize=1 \
UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=0 \
./repro_negate poc-unitsPerEm0.ttf

Trigger result

Verified on unmodified libschrift v0.10.2 (8e533fd0), clang 14, -fsanitize=address,undefined -fno-sanitize=float-cast-overflow:

src/schrift.c:288:53: runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself
    #0 0x56344d99bdc2 in sft_gmetrics ./src/schrift.c:288:53
    #1 0x56344d998365 in main ./driver.c:32:7
    #2 0x7fb44417ad8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #3 0x7fb44417ae3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #4 0x56344d8da3b4 in _start (./repro_negate+0x253b4) (BuildId: 793c9fa5541dddc3522ea163f022ca2717ee5c7c)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior src/schrift.c:288:53 in 
[repro] glyph 1 measured
[repro] done

The faulting frame is sft_gmetrics() itself (schrift.c:288:53), called directly from the application; glyph_bbox() has already returned INT_MIN as bbox[3].

Image

Impact

SFT_DOWNWARD_Y is the conventional top-left origin used by image-drawing applications, and the value returned in metrics->yOffset is used by the caller as a vertical drawing offset. With this font the negation is undefined, so the compiler is entitled to assume it cannot happen and to optimise the surrounding code under that assumption; the returned yOffset is not a defined value. In a build that traps undefined behaviour the process aborts on the font.

Suggested fix

Reject the non-finite box at its source and make the negation safe:

/* glyph_bbox(): reject the non-finite scaled coordinates before the cast */
double v = box[3] * yScale + sft->yOffset;
if (!isfinite(v)) return -1;
box[3] = (int) ceil(v);

/* sft_gmetrics() */
metrics->yOffset = (sft->flags & SFT_DOWNWARD_Y) ? (int) -(long) bbox[3] : bbox[1];

Activity

  1. 1820893135-pixel commented on Oct 24, 2026

    @1820893135-pixel
    Author

    Fixed in #50: sft_gmetrics() now negates bbox[3] in unsigned arithmetic, so -INT_MIN is well defined. Verified with -fno-sanitize=float-cast-overflow: the 320-byte font no longer reports the negation, and the metrics/rendering of three real fonts are unchanged. The load-time rejection of unitsPerEm == 0 and the total float-to-int conversion are in #49.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions