Skip to content
Memory corruption Disclosed report Advanced

Read and write beyond bounds in nsImageFromClipboard::InvertRows()

The InvertRows function in nsImageFromClipboard does not handle images with fewer than one row of data, causing an integer underflow that leads to out-of-bounds read and write. An attacker could exploit this by crafting a malicious image and convincing a user to paste it, potentially causing memory corruption.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 23 months
  • Paid in 38 days
  • Disclosed after 23 months
Text
Created attachment 8858391
bug_836_poc_1.cpp

nsImageFromClipboard::InvertRows() (widget\windows\nsImageClipboard.cpp) does not properly handle the case in which the input image contains < 1 row of data. In that case, line 282 (below) underflows, precipitating a read and write beyond bounds.

Attached is a POC that demonstrates this bug. In the POC, the bitmap dimensions are 32768 w * 43691 h * 24 bpp and |bv5SizeImage| == 0x8000, which means that |aSizeOfBuffer| == |bv5SizeImage| == 0x8000, and |aNumBytesPerRow| == 0x18000, so...

   272: void
   273: nsImageFromClipboard::InvertRows(unsigned char * aInitialBuffer, uint32_t aSizeOfBuffer, uint32_t aNumBytesPerRow)
   274: {
   275:   if (!aNumBytesPerRow) 
   276:     return; 
   277: 
   278:   uint32_t numRows = aSizeOfBuffer / aNumBytesPerRow;

...line 278 calculates |numRows| == 0...

   279:   unsigned char * row = new unsigned char[aNumBytesPerRow];
   280: 
   281:   uint32_t currentRow = 0;
   282:   uint32_t lastRow = (numRows - 1) * aNumBytesPerRow;

...and |lastRow| becomes huge, causing the loop to read and write beyond bounds.

   283:   while (currentRow < lastRow)
   284:   {
   285:     // store the current row into a temporary buffer
   286:     memcpy(row, &aInitialBuffer[currentRow], aNumBytesPerRow);
   287:     memcpy(&aInitialBuffer[currentRow], &aInitialBuffer[lastRow], aNumBytesPerRow);
   288:     memcpy(&aInitialBuffer[lastRow], row, aNumBytesPerRow);
   289:     lastRow -= aNumBytesPerRow;
   290:     currentRow += aNumBytesPerRow;
   291:   }
   292: 
   293:   delete[] row;
   294: }

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 275, 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, step through the code as above. Notice, in particular, that line 282 sets |lastRow| to |0xfffe8000|, and that the loop attempts to access/accesses memory (depending on your memory layout, the access might or might not be successful) far off the end of |aInitialBuffer|.

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

Impact

The out-of-bounds read and write could lead to memory corruption, potentially allowing an attacker to execute arbitrary code or cause a denial of service.

What the attacker needed

Victim pastes a crafted image; attacker can deliver a malicious image

How it was found

  1. Create a Win32 GUI project in Visual Studio and replace the About() function with the POC code.
  2. Build the project and start Firefox, attaching the debugger with a breakpoint on line 275.
  3. Load the POC web page and run the POC code, then select help/about and click OK.
  4. Click on 'Paste an image here!' and press Ctrl+V.
  5. When the breakpoint triggers, step through the code and observe that line 282 sets lastRow to 0xfffe8000 and the loop accesses memory far beyond 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 you paste an image into a program, the program processes the image data. If the image is malformed such that it has zero rows, a calculation goes wrong and the program tries to access memory outside the image buffer. This can crash the program or allow an attacker to run malicious 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.

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.