Repository navigation
FreeImageIO: bound _MemoryWriteProc end position (Windows 32-bit long overflow) - #228
Merged
Merged
Conversation
The growth loop adds current_position and required_bytes in a long, which is 32 bits on Windows (LLP64). After a seek to INT_MAX - 7, a 16-byte write is signed overflow there: the wrapped sum skips the growth loop and memcpy writes past the buffer. The #217 guard only caps the size * count total, not this sum. Refuse any write whose end the int stream positions cannot represent before forming the sum. current_position is always in [0, INT_MAX] via _MemorySeekProc, so INT_MAX - current_position cannot overflow. MemWritePositionTest seeks to INT_MAX - 7, expects the 16-byte write to return 0 without moving the position, then checks an ordinary write still works. A Windows-width model of the function (long -> int32_t, -fsanitize=signed-integer-overflow) reports "2147483640 + 16 cannot be represented in type 'int32_t'" on master and returns 0 with the fix.
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.
Follow-up to #217. That PR made the
size * counttotal overflow-safe, but the write growth loop still computescurrent_position + required_bytesin along. On Windowslongis 32 bits, so a seek toINT_MAX - 7followed by a 16-byte write is signed overflow. The wrapped sum skips the growth loop, andmemcpythen writes past the buffer.Fix
_MemoryWriteProcnow returns 0 whenrequired_bytes > INT_MAX - current_position, and it checks this before it adds the two._MemorySeekProckeepscurrent_positionin[0, INT_MAX], so the subtraction itself cannot overflow. Writes whose end fits in anintbehave as before.Test
New
MemWritePositionTest(ctest):INT_MAX - 7, writes 16 bytes, and expects the write to return 0 with the position unchanged.FreeImage_AcquireMemory.The test can only fail before the fix on Windows jobs. On LP64 hosts the old code does not overflow: it grows the buffer to 2 GiB and then returns 0, so the test passes there with or without the fix.
Verification
longreplaced byint32_t, built with-fsanitize=signed-integer-overflow.runtime error: signed integer overflow: 2147483640 + 16 cannot be represented in type 'int32_t'Should land before the next tag.