Write and read beyond bounds caused by nsJPEGEncoder::emptyOutputBuffer()
An integer overflow in the JPEG encoder's buffer-growth routine lets compressed image data be written far past the end of the allocated output buffer. A script could then read memory beyond the buffer through the resulting Blob, or crash the browser while copying it.
Originally disclosed on Mozilla
- Fixed in 2.2 years
- Paid in 3 months
- Disclosed after 2.2 years
Created attachment 8964037
bug_1091_poc_3.htm
nsJPEGEncoder::emptyOutputBuffer (image\encoders\jpeg\nsJPEGEncoder.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 far beyond the end of its output buffer.
The bug is on line 433, which doesn't check for overflow.
This bug is similar to https://bugzilla.mozilla.org/show_bug.cgi?id=1434490 .
Attached is a POC that demonstrates the overflow and write beyond bounds.
If FF survives long enough to execute the lambda function in the |e.toBlob()| call in the POC, the script will then be able to read data from beyond bounds by reading the Blob, since that data comes from memory pointed to by |nsJPEGEncoder::mImageBuffer|. Often, however, FF dies reading beyond bounds while attempting to copy the buffer into the Blob, usually in NS_CopySegmentToBuffer().
420: boolean // static
421: nsJPEGEncoder::emptyOutputBuffer(jpeg_compress_struct* cinfo)
422: {
423: nsJPEGEncoder* that = static_cast<nsJPEGEncoder*>(cinfo->client_data);
424: NS_ASSERTION(that->mImageBuffer, "No buffer to empty!");
425:
426: // When we're reallocing the buffer we need to take the lock to ensure
427: // that nobody is trying to read from the buffer we are destroying
428: ReentrantMonitorAutoEnter autoEnter(that->mReentrantMonitor);
429:
430: that->mImageBufferUsed = that->mImageBufferSize;
431:
432: // expand buffer, just double size each time
433: that->mImageBufferSize *= 2;
434:
435: uint8_t* newBuf = (uint8_t*)realloc(that->mImageBuffer,
436: that->mImageBufferSize);
437: if (!newBuf) {
438: // can't resize, just zero (this will keep us from writing more)
439: free(that->mImageBuffer);
440: that->mImageBuffer = nullptr;
441: that->mImageBufferSize = 0;
442: that->mImageBufferUsed = 0;
443:
444: // This seems to be the only way to do errors through the JPEG library. We
445: // pass an nsresult masquerading as an int, which works because the
446: // setjmp() caller casts it back.
447: longjmp(((encoder_error_mgr*)(cinfo->err))->setjmp_buffer,
448: static_cast<int>(NS_ERROR_OUT_OF_MEMORY));
449: }
450: that->mImageBuffer = newBuf;
451:
452: cinfo->dest->next_output_byte = &that->mImageBuffer[that->mImageBufferUsed];
453: cinfo->dest->free_in_buffer = that->mImageBufferSize - that->mImageBufferUsed;
454: return 1;
455: }
Use the POC by starting FF, disabling D2D (if on Windows) (by setting gfx.direct2d.disable to |true|), and enabling large-surface support (c.f. 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 433, above. Load the POC and wait for the BP at which the value of |that->mImageBufferSize| is 0x80000000. Now step line 433 and observe the overflow and subsequent allocation of a 0-length buffer for |newBuf| (and thus |mImageBuffer|, line 450) and the setting of the next writable buffer location (|next_output_byte|, line 452) to 0x80000000 beyond the end of the buffer.
Now set a BP on encode_one_block_simd() (in media\libjpeg\jchuff.c) and proceed. When you hit the BP, observe the function writing compressed image data into hyperspace at |state->next_output_byte|.
The POC generates random data that is generally uncompressible, and thus causes the JPEG compressor 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
Likely the buffer-size doubling on line 433 is done without an overflow check, so the size wraps to zero and the next write pointer is set far beyond the real allocation.
Impact
The script could read data from beyond the buffer via the Blob, though the browser often crashes instead while copying the buffer.
What the attacker needed
Large-surface support enabled; D2D disabled on Windows; script supplies a large incompressible image
How it was found
- Start Firefox with D2D disabled and large-surface support enabled, then restart.
- Attach a debugger and set a breakpoint on line 433.
- Load the proof-of-concept and wait until the buffer size reaches 0x80000000.
- Step the line to observe the overflow and the zero-length allocation.
- Set a breakpoint in the JPEG block encoder and continue to see data written past the buffer.
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.
When a browser saves an image as JPEG, it keeps growing a memory buffer to hold the compressed result. Here the size counter doubles until it wraps around to zero, so the code thinks it has a huge buffer when it actually has none, and keeps writing image data past the end of memory. That can leak other memory contents back to a web page or crash the browser.
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.