LibRaw x3f_thumb_loader heap-based buffer overflow vulnerability
The thumbnail extraction routine in LibRaw multiplies attacker-controlled X3F thumbnail dimensions using 32-bit arithmetic, so the size can wrap around and a much smaller heap buffer is allocated than the loop later writes into. A crafted X3F file could therefore corrupt the heap and potentially lead to code execution.
Originally disclosed on Cisco Talos
A heap-based buffer overflow vulnerability exists in the x3fthumbloader functionality of LibRaw Commit d20315b. A specially crafted malicious file can lead to a heap buffer overflow.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The buffer size is computed as columns * rows * 3 in 32-bit unsigned arithmetic, so the product wraps around and malloc receives a truncated size while the copy loop still uses the original dimensions.
Impact
The heap overflow can corrupt memory and potentially lead to code execution, though only builds compiled with X3F support enabled are affected.
What the attacker needed
Target built with -DUSE_X3FTOOLS; victim opens a crafted X3F file
The fix
The size calculation should be performed in a wider integer type with an overflow check before allocating, and the vendor released a patch.
How it was found
- Craft an X3F file whose thumbnail section header declares very large column and row values.
- Choose a small row_stride so the source bounds check is satisfied with little pixel data.
- Open the file in an application that calls unpack_thumb() on it.
- The size calculation wraps, a small buffer is allocated, and the copy loop writes past its end.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A program reads picture files and works out how much memory to reserve for a small preview image by multiplying the width and height from the file. Because the numbers are multiplied in a limited 32-bit space, a huge width and height wrap around to a small number, so too little memory is reserved. The program then copies far more data than fits, spilling over into neighbouring memory and corrupting it.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.