When -1 Becomes a Heap Write: A libtiff TIFFReadAndRealloc() Vulnerability
A libtiff function added a failed read callback's negative return value into an internal byte counter before validating it, so the error path computed an invalid memset that wrote one byte before the raw-data heap allocation and one byte past its end. An attacker who can make a stream-backed TIFF read fail with a negative result could corrupt heap memory in the decoding process.
A stream read failed and returned -1. Instead of rejecting that error value, libtiff fed it straight into an internal byte counter.
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 write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
The read callback's return value was added to the internal byte counter before being checked for a negative error value, so the error path performed pointer arithmetic and a memset with an invalid offset and length.
Impact
The confirmed impact is a heap out-of-bounds write and heap memory corruption in stream-backed libtiff decoding; the author does not claim remote code execution, and tested GDAL and ImageMagick paths converted natural syscall failures into clean errors, only reproducing the overflow when the callback itself was forced to return a negative value.
What the attacker needed
Attacker-controlled TIFF data; app uses custom or stream-backed callbacks; read callback returns a negative value
The fix
The upstream fix validates a negative read result before it can influence offsets, pointers or lengths, and after the correction the same condition produced a clean error instead of an out-of-bounds write.
How it was found
- Build the vulnerable libtiff revision with AddressSanitizer and UndefinedBehaviorSanitizer.
- Open a stream-backed TIFF source through TIFFClientOpen().
- Let the background thread send a minimal TIFF header with strip data expected later in the stream.
- Reset the local TCP connection before the requested strip bytes arrive.
- Allow the callback's real read() call to return -1 with ECONNRESET.
- Observe the negative result reach TIFFReadAndRealloc() during encoded strip loading.
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 write-up has not been written yet. The Original tab links to the source.
A program that reads a TIFF image can be given a custom way of fetching the bytes, for example from a network stream. When that fetch fails, the fetching routine reports the failure as a negative number. libtiff added that negative number to a running total before checking it, so a later memory-clearing step used a wrong starting point and length, writing outside the memory it had reserved. That kind of out-of-bounds write is called heap corruption.
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.