Write beyond bounds caused by lack of validation of clipboard image header (and overflow) in nsImageFromClipboard ::GetEncodedImageStream()
The clipboard image handler in Firefox did not validate the bitmap header fields (width, height, header size) and multiplied width by height without checking for integer overflow. An attacker could craft clipboard image data that overflows the size calculation, causing a small buffer to be allocated and then written past its bounds.
- Fixed in 23 months
- Paid in 38 days
- Disclosed after 23 months
Created attachment 8858381 bug_834_poc_1.cpp
nsImageFromClipboard::GetEncodedImageStream() (widget\windows\nsImageClipboard.cpp) does not validate its input clipboard image header. Additionally, it does not check for overflow when computing a buffer size. These bugs can cause a read and/or write beyond bounds.
The bugs are between lines 230 and 235 (lack of validation of |bmiHeader.biWidth| and |bmiHeader.biHeight|), on line 235 (unguarded multiplication that can overflow), and on line 240 (lack of validation of |header->bmiHeader.biSize|):
219: nsresult 220: nsImageFromClipboard::GetEncodedImageStream (unsigned char * aClipboardData, const char * aMIMEFormat, nsIInputStream** aInputStream ) 221: { 222: NS_ENSURE_ARG_POINTER (aInputStream); 223: NS_ENSURE_ARG_POINTER (aMIMEFormat); 224: nsresult rv; 225: aInputStream = nullptr; 226: 227: // pull the size information out of the BITMAPINFO header and 228: // initialize the image 229: BITMAPINFO header = (BITMAPINFO ) aClipboardData; 230: int32_t width = header->bmiHeader.biWidth; 231: int32_t height = header->bmiHeader.biHeight; 232: // neg. heights mean the Y axis is inverted and we don't handle that case 233: NS_ENSURE_TRUE(height > 0, NS_ERROR_FAILURE); 234: 235: unsigned char * rgbData = new unsigned char[width * height * 3 / RGB */]; 236: 237: if (rgbData) { 238: BYTE * pGlobal = (BYTE *) aClipboardData; 239: // Convert the clipboard image into RGB packed pixel data 240: rv = ConvertColorBitMap((unsigned char ) (pGlobal + header->bmiHeader.biSize), header, rgbData); 241: // if that succeeded, encode the bitmap as aMIMEFormat data. Don't return early or we risk leaking rgbData 242: if (NS_SUCCEEDED(rv)) { 243: nsAutoCString encoderCID(NS_LITERAL_CSTRING("@mozilla.org/image/encoder;2?type=")); 244: 245: // Map image/jpg to image/jpeg (which is how the encoder is registered). 246: if (strcmp(aMIMEFormat, kJPGImageMime) == 0) 247: encoderCID.AppendLiteral("image/jpeg"); 248: else 249: encoderCID.Append(aMIMEFormat); 250: nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(encoderCID.get(), &rv); 251: if (NS_SUCCEEDED(rv)){ 252: rv = encoder->InitFromData(rgbData, 0, width, height, 3 * width / RGB * # pixels in a row */, 253: imgIEncoder::INPUT_FORMAT_RGB, EmptyString()); 254: if (NS_SUCCEEDED(rv)) { 255: encoder.forget(aInputStream); 256: } 257: } 258: } 259: delete [] rgbData; 260: } 261: else 262: rv = NS_ERROR_OUT_OF_MEMORY; 263: 264: return rv; 265: } // GetImage
Attached is a POC that uses these bugs to cause line 235 to overflow, causing an underallocation and write beyond bounds of an attacker-specified amount of attacker-specified data.
Use the POC web page and code segment by creating a new Win32 (GUI-mode) project in Visual Studio. Replace the About() function with the POC code segment. Build the result. Start FF and attach the debugger to it, setting a BP on line 235, above. Load the POC web page. Now run the POC code and select help/about, then OK. Click on "Paste an image here!" and press ctrl/v. When the BP triggers, view the disassembly window to see the multiplication overflow, causing line 235 to allocate 0x8000 bytes for |rgbData| Then trace downstream into nsImageFromClipboard ::ConvertColorBitmap() and watch it write 0x28000 bytes beyond |rgbData|'s bounds.
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 code trusted the width and height values from the clipboard bitmap header and computed the buffer size with an unchecked multiplication that could wrap around.
Impact
An attacker could cause a write beyond the bounds of an allocated buffer with attacker-controlled data, which may lead to memory corruption and potentially code execution.
What the attacker needed
Windows Firefox build; victim pastes crafted image; attacker controls clipboard data
How it was found
- Build a small Windows program that places crafted bitmap data on the clipboard.
- Attach a debugger to Firefox and set a breakpoint on the buffer allocation line.
- Load the proof-of-concept web page and trigger the program's help/about action.
- Click the paste area and press Ctrl+V to paste the crafted image.
- Observe the multiplication overflow and the subsequent out-of-bounds write.
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 you paste an image, Firefox reads a small header that says how wide and tall the image is, then reserves memory for the pixels. The code multiplied width by height without checking whether the result was too big to fit in the number type, so the reservation came out far too small. The program then wrote all the pixel data into that too-small space, corrupting memory beyond 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.