Repository navigation
FreeImageIO: overflow-safe _MemoryReadProc total (CVE-2023-47992) - #217
Conversation
|
Reviewed against current The direction is right. On master, Blocking: the fix only works where const int remaining_bytes = mem_header->file_length - mem_header->current_position;
if (remaining_bytes > 0) {
const size_t remaining = (size_t)remaining_bytes;
if (count <= remaining / size) {
const size_t required_bytes = (size_t)size * count; // cannot overflow: <= remaining
memcpy(buffer, (char*)mem_header->data + mem_header->current_position, required_bytes);
mem_header->current_position += (int)required_bytes;
return count;
}
memcpy(buffer, (char*)mem_header->data + mem_header->current_position, remaining);
mem_header->current_position = mem_header->file_length;
return (unsigned)(remaining / size);
}
return 0;A Minor points:
Verdict: good change. Please switch to the division-based (or |
…-2023-47992 class) size/count come straight from file-driven callers (e.g. height * pitch): the int product introduced by the 3.19 rewrite wraps for huge inputs, defeating the bounds check and handing memcpy a giant length (same bug class as CVE-2023-47992, whose 3.18.0 code was a per-item loop with no size * count product). A size_t product is still not enough: it wraps exactly like before on ILP32 targets (MinGW32, Win32 MSVC, armv7, wasm32), so compare with division instead, which does not depend on the width of size_t. Full/short/zero semantics are unchanged; totals in [2^31, 2^32) now do a proper short read instead of returning 0. A crafted size=0x40000001/count=4 against a 64-byte stream claimed 4 items pre-fix and returns 0 post-fix on both 64-bit hosts and 32-bit logic. AI assistance disclosure: this patch was authored with AI assistance (agentic coding CLI) and verified with AddressSanitizer/UBSan red/green runs before submission.
_MemoryWriteProc computed (long)(size * count): the product wraps in 32-bit unsigned before the cast, so a huge total becomes tiny and the call claims items that were never written. Lower risk than the read side (write sizes come from the encoder, not the file), but the same class. Compute the product in 64 bits and refuse totals above 0x7FFFFFFF, which the int stream positions (max 2 GiB data_length) cannot represent. Zero-size behavior is unchanged. AI assistance disclosure: this patch was authored with AI assistance (agentic coding CLI) and verified with AddressSanitizer/UBSan red/green runs before submission.
ab4f414 to
23ea250
Compare
|
Revision pushed addressing all four points: (1) division-based read check, verbatim in structure including the short path returning |
|
Thank you merged |
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 danoli3#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.
Root cause (CVE-2023-47992)
_MemoryReadProccomputedrequired_bytes = (int)size * countin int arithmetic. With file-drivensize/count(e.g.height * pitch) the product overflows, defeating the bounds check and handingmemcpya giant length (original 3.18.0 impact: heap OOB read + write). On current master the copy is already bounded by the remaining stream bytes, but the wrapped total still over-claims:size=0x40000001,count=4against a 64-byte stream returned 4 pre-fix.Fix
Compute the total in
size_twith byte-identical full/short/zero semantics for all previously-safe inputs (samecount/remaining/size/0returns, same copy bounds, same position updates). The crafted case now takes the short path and returns 0; no sanitizer fires.Verification (ASan+UBSan, serial build)
AI assistance disclosure: this patch was authored with AI assistance (agentic coding CLI) and verified with AddressSanitizer/UBSan red/green runs before submission.