Skip to content
Memory corruption Disclosed report Advanced

Write beyond bounds in nsClipboard::GetGlobalData()

The clipboard data handler in Firefox on Windows computed a buffer size by adding three to a value taken from the clipboard, without checking for overflow, so a crafted clipboard entry could make the allocation far too small. A subsequent copy then wrote attacker-controlled data past the end of the heap buffer, corrupting memory and in some runs causing a timer thread to call through a register holding bytes from the attacker's string.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 26 days
  • Paid in 32 days
  • Disclosed after 26 days

nsClipboard::GetGlobalData() (widget\windows\nsClipboard.cpp) can cause an integer overflow, which causes underallocation of a buffer followed by a write beyond bounds of attacker-supplied data onto the heap. Sometimes the data causes FF to jump to an attacker-provided address (see below):

The bug is on line 291, which doesn't check for overflow:

   279: nsresult nsClipboard::GetGlobalData(HGLOBAL aHGBL, void** aData,
   280:                                     uint32_t* aLen) {
   281:   // Allocate a new memory buffer and copy the data from global memory.
   282:   // Recall that win98 allocates to nearest DWORD boundary. As a safety
   283:   // precaution, allocate an extra 3 bytes (but don't report them in |aLen|!)
   284:   // and null them out to ensure that all of our NS_strlen calls will succeed.
   285:   // NS_strlen operates on char16_t, so we need 3 NUL bytes to ensure it finds
   286:   // a full NUL char16_t when |*aLen| is odd.
   287:   nsresult result = NS_ERROR_FAILURE;
   288:   if (aHGBL != nullptr) {
   289:     LPSTR lpStr = (LPSTR)GlobalLock(aHGBL);
   290:     DWORD allocSize = GlobalSize(aHGBL);
   291:     char* data = static_cast<char*>(malloc(allocSize + 3));
   292:     if (data) {
   293:       memcpy(data, lpStr, allocSize);
   294:       data[allocSize] = data[allocSize + 1] = data[allocSize + 2] =
   295:           '\0';  // null terminate for safety
   ...
   324:   return result;
   325: }

Attached is a POC that triggers the overwrite.

Use it by starting FF Win64 debug on a machine with >= 16GB RAM, disabling e10s, and restarting FF. Now attach a debugger and set a BP on line 291. Load the POC, select the text "select this text and ctrl/c...", and type ctrl/c. FF will freeze for several seconds. When it becomes responsive again, click in the text "...and click, then ctrl/v here." and type ctrl/v. When the BP triggers, notice that |allocSize| == 0xfffffffe . Now go to disassembly view, step the next few instructions, and watch the overflow. Continue and watch line 293 spew many instances of the attacker-provided string "EvilCode" over the heap.

The heap corruption readily causes other threads to do incorrect things, as you can witness by, e.g., loading www.washingtonpost.com in a tab before using the POC in a different tab. In a few tries, I got a timer thread to attempt to call through register |rdx|, which contains the value 0069007600450065 ("eEvi"), clearly from the repeated JS string "EvilCode" in the POC:

         NSCAP_ADDREF(this, mRawPtr);
     000007FED3936EA1 48 8B 44 24 38       mov         rax,qword ptr [rsp+38h]  
     000007FED3936EA6 48 8B 08             mov         rcx,qword ptr [rax]  
     000007FED3936EA9 48 8B 11             mov         rdx,qword ptr [rcx]  
   > 000007FED3936EAC FF 52 08             call        qword ptr [rdx+8]  
     000007FED3936EAF 89 44 24 2C          mov         dword ptr [rsp+2Ch],eax  
       }
   rcx = 0000000000A58900

   0x0000000000A58900  65 00 45 00 76 00 69 00 6c 00 43 00 6f  e.E.v.i.l.C.o
   0x0000000000A5890D  00 64 00 65 00 45 00 76 00 69 00 6c 00  .d.e.E.v.i.l.
   >	xul.dll!nsCOMPtr<nsIEventTarget>::nsCOMPtr(const nsCOMPtr<nsIEventTarget> & aSmartPtr={...}) Line 474	C++
 	   xul.dll!TimerThread::PostTimerEvent(already_AddRefed<nsTimerImpl> aTimerRef={...}) Line 734	C++
 	   xul.dll!TimerThread::Run() Line 423	C++
 	   xul.dll!nsThread::ProcessNextEvent(bool aMayWait=false, bool * aResult=0x00000000024bf2cf) Line 1157	C++
 	   xul.dll!NS_ProcessNextEvent(nsIThread * aThread=0x0000000000a59500, bool aMayWait=false) Line 468	C++
 	   xul.dll!mozilla::ipc::MessagePumpForNonMainThreads::Run(base::MessagePump::Delegate * aDelegate=0x0000000000a472e0) Line 303	C++
 	   xul.dll!MessageLoop::RunInternal() Line 315	C++
 	   xul.dll!MessageLoop::RunHandler() Line 308	C++
 	   xul.dll!MessageLoop::Run() Line 289	C++
 	   xul.dll!nsThread::ThreadFunc(void * aArg=0x00000000002ee310) Line 452	C++
 	   nss3.dll!_PR_NativeRunThread(void * arg=0x0000000000a61c00) Line 419	C
 	   nss3.dll!pr_root(void * arg=0x0000000000a61c00) Line 137	C
 	   ucrtbase.dll!000007fee9d3be1d()	Unknown
 	   00000000467b012b()	Unknown

This bug apparently was introduced in https://bugzilla.mozilla.org/show_bug.cgi?id=1513201 , but I don't have access to that report.

The original report as published on Mozilla's Bugzilla, by mozillabugs. 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 size calculation added three to the clipboard buffer size without an overflow check, so a size near the maximum wrapped to a small value and the later copy overran the allocation.

Impact

Heap corruption could make other threads misbehave, and in a few attempts a timer thread tried to call through a register containing bytes from the attacker's string, which could lead to code execution.

What the attacker needed

Windows Firefox build; 64-bit debug build; at least 16GB RAM; e10s disabled

How it was found

  1. Start a 64-bit debug build of Firefox on a machine with at least 16GB of RAM, disable e10s and restart.
  2. Attach a debugger and set a breakpoint on the allocation line.
  3. Load the proof of concept, select the text prompting a copy, and press the copy shortcut.
  4. Wait for Firefox to become responsive again, click into the paste target text and press the paste shortcut.
  5. At the breakpoint, observe the reported allocation size is near the maximum value, then step through and watch the copy write the attacker string across the heap.

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 copy something, the browser asks Windows for the data and makes a copy of it in its own memory. The code worked out how much memory to reserve by adding a small number to a size reported by Windows, but never checked whether that addition wrapped around to a tiny number. If it did, the browser reserved almost nothing and then copied a large amount of data into that tiny space, spilling over into neighbouring memory and letting the copied text influence what the program does next.

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 mozillabugs

View all

41 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.