Write and read beyond bounds in nsPNGEncoder::WriteCallback()
The PNG encoder's WriteCallback function has an integer overflow when doubling the buffer size, and it also fails to check that the doubled size is sufficient for the incoming data. An attacker could cause a write beyond the bounds of the allocated buffer by providing a specially crafted image, potentially leading to memory corruption.
Originally disclosed on Mozilla
- Fixed in 8 months
- Paid in 27 days
- Disclosed after 8 months
Created attachment 8946950
bug_1036_poc_4.htm
nsPNGEncoder::WriteCallback() (image\encoders\png\nsPNGEncoder.cpp) can experience an integer overflow. When it does, it causes compressed data (which can be derived from a script-provided image) to be written beyond the end of its output buffer. WriteCallback() also unsafely assumes that doubling the existing buffer size always provides sufficient room to copy the input data. WriteCallback() also contains an addition that is overflow-safe only by coincidence.
The first bug is on line 712, which doesn't check for overflow. The second bug is that line 712 isn't followed by code to determine whether |that->mImageBufferSize >= that->mImageBufferUsed + size| [1]. The third bug is that the addition on line 706 is only coincidentally safe. [2]
Attached is a POC that demonstrates the overflow and write beyond bounds.
697: void // static
698: nsPNGEncoder::WriteCallback(png_structp png, png_bytep data,
699: png_size_t size)
700: {
701: nsPNGEncoder* that = static_cast<nsPNGEncoder*>(png_get_io_ptr(png));
702: if (!that->mImageBuffer) {
703: return;
704: }
705:
706: if (that->mImageBufferUsed + size > that->mImageBufferSize) {
707: // When we're reallocing the buffer we need to take the lock to ensure
708: // that nobody is trying to read from the buffer we are destroying
709: ReentrantMonitorAutoEnter autoEnter(that->mReentrantMonitor);
710:
711: // expand buffer, just double each time
712: that->mImageBufferSize *= 2;
713: uint8_t* newBuf = (uint8_t*)realloc(that->mImageBuffer,
714: that->mImageBufferSize);
715: if (!newBuf) {
716: // can't resize, just zero (this will keep us from writing more)
717: free(that->mImageBuffer);
718: that->mImageBuffer = nullptr;
719: that->mImageBufferSize = 0;
720: that->mImageBufferUsed = 0;
721: return;
722: }
723: that->mImageBuffer = newBuf;
724: }
725: memcpy(&that->mImageBuffer[that->mImageBufferUsed], data, size);
726: that->mImageBufferUsed += size;
727: that->NotifyListener();
}
Use the POC by starting FF x64 on a machine with >= 16 GB of memory. Disable D2D (if on Windows) by setting gfx.direct2d.disable to |true|. Enable large-surface support (see https://bugzilla.mozilla.org/show_bug.cgi?id=1282074 ) by setting gfx.max-alloc-size == 2147483647 (0x7fffffff). Restart FF.
Now attach a debugger to FF and set a BP on line 712, above. Load the POC and wait for the BP at which the value of |that->mImageBufferSize| is 0x80000000. Now step line 712, observe the overflow, and step through line 725, which copies compressed image data [3] at a huge offset from the beginning of the 0-length "buffer" that lines 713-14 just allocated.
[1] At present, this doubling is coincidentally safe because the maximum |size| passed into WriteCallback() is determined by |PNG_ZBUF_SIZE| in media/libpng/pnglibconf.h , which happens to be 8192, which is the same size as the initial size of |mImageBuffer| that is set by nsPNGEncoder::StartImageEncode(). If libpng were changed to use a larger buffer size, say 10k, the first doubling (from 8k to 16k) would leave only 8k of allocated space to contain potentially as much as 10k of data, causing a write beyond bounds. Since libpng is published by a non-Mozilla provider, and nothing in nsPNGEncoder appears to check |PNG_ZBUF_SIZE|, this bug could occur. This bug would be worse than the overflow, since it could be invoked with a much-smaller image.
[2] Because |size| isa |size_t|, C++ promotes |that->mImageBufferUsed| to a size_t before adding it to |size|.
[3] The POC generates random data that is generally uncompressible, and thus causes libpng to generate a compressed data stream that is sufficiently-long to cause the overflow.
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 doubled without checking for integer overflow, and the code does not verify that the new size is large enough to hold the incoming data.
Impact
An attacker could cause a write beyond the bounds of the allocated buffer, potentially leading to memory corruption and possibly arbitrary code execution.
What the attacker needed
Firefox 64-bit; At least 16 GB of memory; gfx.direct2d.disable set to true; gfx.max-alloc-size set to 2147483647
How it was found
- Start Firefox 64-bit on a machine with at least 16 GB of memory.
- Disable Direct2D by setting gfx.direct2d.disable to true.
- Enable large-surface support by setting gfx.max-alloc-size to 2147483647.
- Restart Firefox.
- Attach a debugger and set a breakpoint on line 712.
- Load the proof-of-concept file and wait for the breakpoint when mImageBufferSize is 0x80000000.
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.
This report describes a bug in Firefox's PNG encoder. When the encoder needs more space to store compressed image data, it doubles the size of its buffer. If the buffer size becomes very large, doubling it can overflow and wrap around to a small number, so the program thinks it has a tiny buffer but then tries to copy a lot of data into it, writing past the end. This can crash the browser or potentially allow an attacker to run code.
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.