Skip to content
Memory corruption Disclosed report Advanced

Security bug caused by lack of validation of clipboard data length in nsClipboard::GetNativeDataOffClipboard()

When handling CF_DIBV5 image data from the clipboard, nsClipboard::GetNativeDataOffClipboard() failed to pass the actual data length to GetEncodedImageStream(), so the converter could not tell whether the buffer was long enough. This let it read (and, combined with other bugs, write) beyond the bounds of the clipboard buffer.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 23 months
  • Paid in 38 days
  • Disclosed after 23 months

Created attachment 8858367 bug_833_poc_1.htm

When handling image data (CF_DIBV5), nsClipboard::GetNativeDataOffClipboard() (widget\windows\nsClipboard.cpp) does not pass the actual length of the clipboard data to the handler function nsImageFromClipboard ::GetEncodedImageStream() (widget\windows\nsImageClipboard.cpp).

This omission means that GetEncodedImageStream() cannot determine whether the clipboard data it has been passed is actually as long as it expects. This failure, in turn, can cause GetEncodedImageStream() to read and/or write beyond bounds.

The bug is in line 479:

409: nsresult nsClipboard::GetNativeDataOffClipboard(IDataObject * aDataObject, UINT aIndex, UINT aFormat, const char * aMIMEImageFormat, void ** aData, uint32_t * aLen) ... 470: case CF_DIBV5: 471: if (aMIMEImageFormat) 472: { 473: uint32_t allocLen = 0; 474: unsigned char * clipboardData; 475: if (NS_SUCCEEDED(GetGlobalData(stm.hGlobal, (void **)&clipboardData, &allocLen))) 476: { 477: nsImageFromClipboard converter; 478: nsIInputStream * inputStream; 479: converter.GetEncodedImageStream(clipboardData, aMIMEImageFormat, &inputStream); // addrefs for us, don't release 480: if ( inputStream ) { 481: *aData = inputStream; 482: aLen = sizeof(nsIInputStream); 483: result = NS_OK; 484: } 485: } 486: } break;

This bug is related to several other bugs in clipboard handling, which are described together at https://bugzilla.mozilla.org/show_bug.cgi?id=1353927 .

Attached is a POC that exploits this bug to cause GetEncodedImageStream() to read beyond bounds. When combined with other bugs (see https://bugzilla.mozilla.org/show_bug.cgi?id=1353927 ), this bug can also cause writes beyond bounds.

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 471, 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 fires, step through line 475 and notice that the actual length of the clipboard data (|allocLen|) is 4. Now trace into nsImageFromClipboard ::GetEncodedImageStream() and watch that function read |bmiHeader.biWidth| (offset 4, length 4) and |bmiHeader.biHeight| (offset 8, length 4) from beyond the buffer's end.

The POC has been tested on Win7 SP1 x64, but probably works on all Windows versions.

The original report as published on Mozilla's Bugzilla, by q1. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 caller obtained the clipboard buffer and its length but called the image converter without passing the length, so the converter parsed header fields without any bounds check.

Impact

The proof of concept causes an out-of-bounds read, and when combined with other clipboard bugs it can also cause out-of-bounds writes; it was tested on Windows 7 but probably affects all Windows versions.

What the attacker needed

Windows host; victim pastes crafted clipboard image; attacker-supplied page or program

The fix

The converter should be given the actual clipboard data length and validate the header fields against it before reading.

How it was found

  1. Create a Win32 GUI project in Visual Studio and replace the About() function with the proof-of-concept code.
  2. Build it, start Firefox, attach the debugger and set a breakpoint on the clipboard handling line.
  3. Load the proof-of-concept web page, run the code, choose Help/About and press OK.
  4. Click the paste area and press Ctrl+V so the breakpoint fires.
  5. Step over the data retrieval and observe the reported clipboard length is 4.
  6. Trace into the image converter and watch it read the width and height fields from beyond the buffer's 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 that copies image data from the clipboard must know how big that data is before reading it. Here the size was known but not handed on, so the image reader trusted a size it never checked and read past the end of the buffer. Reading past a buffer can crash the program or leak memory contents.

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.

More by q1

View all

71 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.