Fix integer wraparound in Buffer bounds checks on 32-bit platforms#193
Merged
garretrieger merged 1 commit intoApr 16, 2026
Merged
Conversation
The typed Read methods (ReadU8, ReadU16, ReadU24, ReadU32, ReadTag, ReadR64) used bounds checks of the form `offset_ + N > length_`. On 32-bit platforms where size_t is 32 bits, if offset_ is close to SIZE_MAX (e.g. 0xFFFFFFFF), the addition `offset_ + N` wraps around to a small value, bypassing the bounds check and allowing out-of-bounds reads from the underlying buffer. Fix by restructuring the checks to `length_ < N || offset_ > length_ - N`, which avoids any arithmetic that could overflow. This is consistent with the existing Read(uint8_t*, size_t) method which already uses the safe `offset_ > length_ - n_bytes` pattern. Also add bounds validation to set_offset() (changing return type from void to bool) to reject offsets beyond the buffer length, and update the sole caller in font.cc to check the return value. This prevents TTC font collection offsets from positioning the buffer past its end.
Contributor
Author
|
@garretrieger Could you review this security fix? It addresses integer wraparound in Buffer bounds checks on 32-bit platforms that allows out-of-bounds reads via crafted TTC fonts. |
garretrieger
approved these changes
Apr 16, 2026
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.
Summary
The typed Read methods in
Buffer(ReadU8,ReadU16,ReadU24,ReadU32,ReadTag,ReadR64) use bounds checks of the formoffset_ + N > length_. On 32-bit platforms wheresize_tis 32 bits, ifoffset_is close toSIZE_MAX(e.g.0xFFFFFFFF), the additionoffset_ + Nwraps around to a small value, bypassing the bounds check and allowing out-of-bounds reads from the underlying buffer.Root cause: unsigned integer wraparound in
offset_ + Nwhenoffset_is nearSIZE_MAXon ILP32 platforms.Fix: Restructure all bounds checks from
offset_ + N > length_tolength_ < N || offset_ > length_ - N, which avoids any arithmetic that could overflow. This pattern is consistent with the existingRead(uint8_t*, size_t)method which already uses the safeoffset_ > length_ - n_bytesform.Additionally,
set_offset()previously accepted any value without validation. This is changed to returnbooland reject offsets beyond the buffer length. The sole caller infont.cc(TTC collection font parsing) now checks the return value.Changes
src/buffer.h: Fix bounds checks inReadU8,ReadU16,ReadU24,ReadU32,ReadTag,ReadR64to use overflow-safe comparisonssrc/buffer.h: Changeset_offset()fromvoidtoboolwith bounds validationsrc/font.cc: Checkset_offset()return value when seeking to TTC font offsetsImpact
An attacker supplying a crafted WOFF2 font could potentially trigger out-of-bounds memory reads on 32-bit platforms (e.g., 32-bit Android, embedded systems, wasm32) by manipulating the buffer offset to near
SIZE_MAX. This could lead to information disclosure or crashes.Test plan
makesize_t)