Fix integer-overflow heap out-of-bounds write in RenderSDF#202
Merged
Conversation
A crafted font could produce a glyph whose buffered dimensions, multiplied together in 32-bit arithmetic, overflowed and wrapped to a small value. The resulting bitmap buffer was undersized while the render loop still wrote one byte per pixel, corrupting the heap (CWE-190 -> CWE-787) and crashing the process from a single font render. Cap glyph dimensions at 256px and drop oversized glyphs before allocating; the render size is fixed at 24px so legitimate glyphs stay well under this bound. Also widen the bitmap size and write index to size_t as a backstop against 32-bit overflow. Add a regression test that renders an overflow-triggering font in a child process and asserts it completes without crashing and drops the glyph. Co-authored-by: Cursor <cursoragent@cursor.com>
Make the conversions of font-derived values explicit and bounded, resolving the integer-related compiler warnings: - Clamp glyph.left/top to the same +/-256px bound as the dimension cap; at the fixed 24px render size the position never approaches the int32 limits, and this keeps the double->int32_t conversion well defined. - Widen FreeType metrics straight to the double fields instead of narrowing through int, which truncated font-controlled values before the range checks in glyphs.cpp. - Make the width/height, segment-box, buffered-dimension and SDF-value conversions explicit casts. Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A crafted font could trigger a heap out-of-bounds write in
RenderSDF(include/mapbox/glyph_foundry_impl.hpp).RenderSDFcomputed the SDF bitmap allocation size asbuffered_width * buffered_heightin 32-bitunsigned int. Glyph dimensions are derived from the uploaded font's outline bounding box with no upper bound, so a glyph large enough to make that product exceed 2³² wrapped to a small value. The buffer was then under-allocated while the render loop still wrote one byte per pixel, corrupting the heap (CWE-190 → CWE-787) and crashing the process. A 680-byte font is enough to drive FreeType to a 65536×65536 buffered glyph, wrapping the size to 0.Fix
size_tas a backstop against any 32-bit overflow.glyph.left/glyph.topto the same ±256px bound, keeping thedouble→int32_tconversion well defined.int(which truncated font-controlled values before the range checks inglyphs.cpp), and make the remaining width/height, segment-box, buffered-dimension, and SDF-value conversions explicit casts.Test plan
test/overflow.test.js+test/fixtures/evil.ttf: renders the overflow-triggering font in a child process and asserts it completes without crashing and the oversized glyph is dropped. Confirmed it crashes withSIGBUSbefore the fix and passes after.npm test— full suite passes (424 assertions), with byte-identical output on the existing font fixtures, confirming the changes are behavior-preserving for legitimate fonts.Releaserebuild produces no integer-conversion warnings inglyph_foundry_impl.hpp.