Skip to content

PluginTARGA: bound RLE decode by image rows (CVE-2024-28581) - #215

Merged
danoli3 merged 1 commit into
danoli3:masterfrom
w4ffl35:fix/targa-rle-row-bounds
Oct 27, 2026
Merged

danoli3 merged 1 commit into
danoli3:masterfrom
w4ffl35:fix/targa-rle-row-bounds

Conversation

@w4ffl35

@w4ffl35 w4ffl35 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Root cause (CVE-2024-28581)

loadRLE<N> checks each packet once against dib_end assuming a flat address model, but pixel rows wrap at line_size while the image only has height rows. An over-long RLE packet whose flat span still fits dib_end (e.g. exact-fit, where padding makes pitch > line_size) wraps past the last row mid-packet and overflows the heap on the next _assignPixel write. ASan pre-fix: heap-buffer-overflow ... in _assignPixel<8> via loadRLE<8>.

Fix

  • Stop the decode with the established FI_MSG_ERROR_CORRUPTED path once pixels run past the last row (y >= height, checked at the top of each pixel iteration so exact-fit packets still exit cleanly, returning the completed bitmap).
  • Same-path hardening: 16/32-bit cache pixels are loaded with memcpy (the RLE byte cache is file-offset aligned, not 2/4-aligned; UBSan misaligned-load fired even on valid 32-bit RLE files), and short IOCache refills at EOF are zero-filled so stale heap bytes never surface in decoded pixels.

Verification (ASan+UBSan, serial build)

  • RED pre-fix / GREEN post-fix: Ruanxingzhi/vul-report freeimage-r1909 cases 10024/10412 (WRITE overflow), 10470 (misaligned WORD load), 10421/10955/10800 (READ variants), plus a crafted 66-byte guard-isolating TGA whose flat span exactly fits dib_end.
  • No regressions: valid 8/16/24/32-bit RLE TGAs (incl. flip flags, header-only mode, exact-fit packets) decode pixel-identically with zero sanitizer findings.
  • Related CVE-2025-70968 (TARGA UAF) verified already-fixed on master (`loadRLE(FIBITMAP*&)" + NULL-safe flips); its PoC (flip flags set) returns clean NULL. No change needed for it.

PoC repos used as reproducers only, never as patch sources.

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

loadRLE's per-packet dib_end check assumes a flat address model, but
pixel rows wrap at line_size while the image only has height rows. An
over-long RLE packet whose flat span fits dib_end can still wrap past
the last row, overflowing the heap in _assignPixel (WRITE, all depths).

Stop the decode with a corruption notice once pixels run past the last
row; the check sits at the top of the pixel loop so packets ending
exactly on the last pixel still exit cleanly.

Also harden the same path: load 16/32-bit cache pixels with memcpy
(the RLE byte cache is file-offset aligned, not 2/4-aligned; UBSan
misaligned-load on crafted and valid files), and zero-fill short
IOCache refills at EOF so stale heap bytes never surface in pixels.

RED (ASan+UBSan, pre-fix) -> GREEN (post-fix) verified with
Ruanxingzhi/vul-report freeimage-r1909 cases 10024/10412/10470 plus a
guard-isolating crafted TGA; valid 8/16/24/32-bit RLE TGAs still
decode pixel-identically.

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 via email

Copy link
Copy Markdown
Owner

@danoli3

danoli3 commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Reviewed against current master. This one looks good to me.

  • Root cause is correctly identified. The per-packet dib_end check treats the image as flat memory. But x wraps at line_size (unpadded), while rows are pitch apart (4-byte padded). So the real write span of a packet is longer than the flat estimate, and a packet that passes the check can still advance y to height. At that point line_bits = FreeImage_GetScanLine(dib, height) is one past the end, and the next _assignPixel writes out of bounds. A y >= height check at the top of each pixel iteration is the minimal, correct guard. It also handles a packet that ends exactly on the last pixel, because the check comes before the write and not after the wrap. Exiting through the existing FI_MSG_ERROR_CORRUPTED + return keeps the same partial-bitmap behaviour as the existing check.
  • Replacing the misaligned loads with memcpy in _assignPixel<16>/<32> is correct. val points into the byte cache at an offset controlled by the file. The destination side (bits) stays aligned, because x steps by pixel_size from a 4-byte-aligned scanline, so the existing WORD store in <16> is fine. Compilers turn these memcpys into the same single load, so there's no performance cost.
  • Zero-filling short IOCache refills removes stale heap bytes from decoded output on truncated files. Previously the cache tail kept data from the last refill or from fresh malloc. The loop can't spin forever, because the row guard bounds it.

Nits, non-blocking:

  • The save path at ~L1169 (*(reinterpret_cast<unsigned*>(packet)) = *(reinterpret_cast<unsigned*>(pixel))) has the same misaligned-access pattern. It isn't driven by the file, so it's out of scope here, but it could be swept up later.
  • The PR also mentions CVE-2025-70968 as already fixed on master. Please record that in issue Multiple existing vulnerabilities #35 so the status list stays current.

Verdict: approve once CI is green.

@danoli3
danoli3 merged commit 6f24892 into danoli3:master Oct 27, 2026
73 checks passed
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