Skip to content

FreeImageIO: overflow-safe _MemoryReadProc total (CVE-2023-47992) - #217

Merged
danoli3 merged 2 commits into
danoli3:masterfrom
w4ffl35:fix/memio-size-overflow
Oct 27, 2026
Merged

danoli3 merged 2 commits into
danoli3:masterfrom
w4ffl35:fix/memio-size-overflow

Conversation

@w4ffl35

@w4ffl35 w4ffl35 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Root cause (CVE-2023-47992)

_MemoryReadProc computed required_bytes = (int)size * count in int arithmetic. With file-driven size/count (e.g. height * pitch) the product overflows, defeating the bounds check and handing memcpy a 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=4 against a 64-byte stream returned 4 pre-fix.

Fix

Compute the total in size_t with byte-identical full/short/zero semantics for all previously-safe inputs (same count / remaining/size / 0 returns, 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)

  • Direct survivable-call probe: wrap-size returns 0 (was 4), normal 16-byte read returns 16 with intact data, short read returns 8; zero findings.
  • Full BMP/TARGA/XPM file+memory load matrix re-run: unchanged behavior, zero findings.

AI assistance disclosure: this patch was authored with AI assistance (agentic coding CLI) and verified with AddressSanitizer/UBSan red/green runs before submission.

@danoli3

danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Reviewed against current master.

The direction is right. On master, (int)(size) * count is evaluated as unsigned, wraps, and then converts to int. With size=0x40000001, count=4 it becomes 4, takes the full-read path, copies 4 bytes and returns count. Callers then believe they got ~4 GiB. Doing the math wider and the comparison unsigned fixes that. The new behaviour for totals in [2^31, 2^32) is also correct. Master returned 0 for those (the product went negative). Now it does a proper short read of the remaining bytes, which matches fread semantics. current_position += (int)required_bytes is safe because that path is only taken when required_bytes <= remaining_bytes <= INT_MAX.

Blocking: the fix only works where size_t is 64-bit. On ILP32 targets (size_t)size * (size_t)count wraps exactly like before. That covers the MinGW32 jobs in our CI matrix, Win32 MSVC, armv7 Android and wasm32/Emscripten. On those, the PR's own PoC (0x40000001 * 4) still produces 4 and still returns 4. A check based on division doesn't depend on the width of size_t:

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 uint64_t product would also work.

Minor points:

  • The CVE text describes 3.18.0. That version's _MemoryReadProc was a per-item loop and had no size * count product; the int product came in with the 3.19 rewrite. The fix is still worth having, but the commit message should say it fixes the 3.19 regression of the same bug class, not literally the 3.18.0 code.
  • _MemoryWriteProc right below has the same class of bug: (long)(size * count) wraps in 32-bit unsigned before the cast. It's lower risk because write sizes come from the encoder, not the file, but it's worth fixing in the same PR or a follow-up.

Verdict: good change. Please switch to the division-based (or uint64_t) check so 32-bit builds are covered too.

…-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.
@w4ffl35
w4ffl35 force-pushed the fix/memio-size-overflow branch from ab4f414 to 23ea250 Compare October 27, 2026 12:12
@w4ffl35

w4ffl35 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

Revision pushed addressing all four points: (1) division-based read check, verbatim in structure including the short path returning (unsigned)(remaining/size) — the PoC now returns 0 under both host and simulated-ILP32 arithmetic (verified: old product returns 4, new logic 0); no -m32 toolchain here, so 32-bit coverage rests on that simulation plus host ASan/UBSan runs; (2) commit message reworded to the 3.19-regression framing; (3) _MemoryWriteProc same-class fix as a separate commit (64-bit product, totals >2GiB refused, zero-size behavior unchanged); (4) red/green reproducers re-run plus valid-corpus pixel-exact equivalence. Thanks for the precise review — especially the ILP32 catch.

@danoli3
danoli3 merged commit 2896238 into danoli3:master Oct 27, 2026
77 checks passed
@danoli3

danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Thank you merged

haobinnan pushed a commit to haobinnan/FreeImage that referenced this pull request Oct 31, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants