Skip to content
Memory corruption Disclosed report Advanced

Write beyond bounds caused by conflicting buffer size calculations between nsImageFromClipboard::ConvertColorBitMap() and GetEncodedImageStream()

A buffer size mismatch in Firefox's Windows clipboard image handling lets a crafted clipboard bitmap cause a heap buffer overflow. An attacker who can place such an image on the clipboard could make the code write past the end of an allocated buffer.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 23 months
  • Paid in 5 months
  • Disclosed after 23 months
Text
nsImageFromClipboard::ConvertColorBitMap() (widget\windows\nsImageClipboard.cpp) can write beyond the end of the output buffer provided by nsImageFromClipboard::GetEncodedImageStream().

The bug is that GetEncodedImageStream() calculates its output buffer size using the image header's |bmiHeader.biWidth| and |bmiHeader.biHeight| field (size = width * height * 3), but ConvertColorBitMap() determines how many rows to write by using the image header's |bmiHeader.biSizeImage| field.

If |bmiHeader.biSizeImage| describes a larger image than calculated by GetEncodedImageStream(), ConvertColorBitMap() writes beyond the end of its output buffer.


You can modify the POC attached to https://bugzilla.mozilla.org/show_bug.cgi?id=1356650 to show this bug by modifying the POC to use:

	imagePixelWidth  = 4;
	imagePixelHeight = 16;
	...
	p->bV5SizeImage = 256;

and building/running the POC as described in https://bugzilla.mozilla.org/show_bug.cgi?id=1356650 .

Set a BP on line 235 of nsImageFromClipboard::GetEncodedImageStream(). When the BP triggers, view the disassembly window to see the multiplication in line 235 calculate 0xc0 and allocate that many bytes for the output buffer |rgbData|. Then trace into nsImageFromClipboard::ConvertColorBitmap() and watch it write 0x102 bytes into the output buffer (while also reading that many bytes from the clipboard image data). Thus ConvertColorBitmap() overruns the output buffer by 0x42 bytes.

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 output buffer size is computed from the bitmap width and height while the number of rows written is derived from a different, attacker-controlled size field, so the two calculations disagree.

Impact

A crafted clipboard image causes a heap buffer overflow of about 0x42 bytes, which could corrupt memory; the text does not demonstrate code execution.

What the attacker needed

Attacker-controlled image on clipboard; victim pastes or copies the image in Firefox

How it was found

  1. Modify the existing proof-of-concept to set a small pixel width and height and a larger size-image value.
  2. Build and run the proof-of-concept as described in the referenced bug.
  3. Set a breakpoint on the line that computes the buffer size and observe the smaller allocation.
  4. Step into the colour conversion routine and observe it writing more bytes than were allocated.

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 program copies an image, it first works out how much memory it needs and then fills that memory. Here the two parts of the program disagree about the image's size: one uses the width and height, the other uses a separate size field. If that separate field claims a bigger image, the filling code writes past the end of the memory it was given, which can corrupt other data.

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.