Write beyond bounds in nsDataObjCollection::GetText()
An integer overflow in nsDataObjCollection::GetText() lets the accumulated buffer size wrap around when many large strings are added via dataTransfer.mozSetDataAt(). If GetText() were called, the code would allocate a small buffer and then copy attacker-controlled data past its end.
- Fixed in 9.1 years
- Paid in 17 months
- Disclosed after 9.1 years
Created attachment 8857225 bug_804_poc_3.htm; use as directed at the bottom of comment 0 and call me in the morning
nsDataObjCollection::GetText() (widget\windows\nsDataObjCollection.cpp) can cause an integer overflow. If it does, it will write beyond the bounds of its |buffer| array.
The bug is that it its |mDataObjects| array, where it accumulates the length of each string into |bufferSize|. But |mDataObjects| can be filled with an arbitrary number of strings (containing arbitrary binary data) by JS code that calls dataTransfer.mozSetDataAt(), with each string being <= 2**28 bytes long (js::MaxStringLength).
At present, I do not know how to cause FF to call nsDataObjCollection::GetText(), though I have attached a POC that populates |mDataObjects| with enough large strings to cause an overflow and write beyond bounds if GetText() were called with the containing nsDataObjCollection.
The POC populates |mDataObjects| with 17 strings. The first 15 are exactly 256MB, the next is 256MB - 16KB, and the last is 32KB. If GetText() were invoked with these strings, the last string would cause the addition on line 265 to compute the quantity
0xffffc001 + 0x8000 -> overflows to 0x4001
and to allocate 0x4001 bytes of memory.
Lines 269-72 would then point |buffer| 0xffffc001-0x4001-1 bytes beyond the allocated buffer's end, and line 273 would copy |0x8000| bytes of data there, overrunning |buffer| by 0x3ffe bytes.
Thus, the attacker could control the amount of data written, as well as the data itself (JS strings can contain arbitrary binary characters, using \uXXXX, where X is a hex digit).
There are 2 overflow bugs in each main section of GetText(): lines 265 and 275, and lines 305 and 315:
232: HRESULT nsDataObjCollection::GetText(LPFORMATETC pFE, LPSTGMEDIUM pSTM) 233: { 234: STGMEDIUM workingmedium; 235: FORMATETC fe = pFE; 236: HGLOBAL hGlobalMemory; 237: HRESULT hr; 238: uint32_t buffersize = 1; 239: uint32_t alloclen = 0; 240: 241: hGlobalMemory = GlobalAlloc(GHND, buffersize); 242: 243: if (pFE->cfFormat == CF_TEXT) { 244: nsAutoCString text; 245: for (uint32_t i = 0; i < mDataObjects.Length(); ++i) { 246: nsDataObj dataObj = mDataObjects.ElementAt(i); 247: hr = dataObj->GetData(&fe, &workingmedium); ... 256: // Now we need to pull out the text 257: char* buffer = (char*)GlobalLock(workingmedium.hGlobal); 258: if (buffer == nullptr) 259: return E_FAIL; 260: text = buffer; 261: GlobalUnlock(workingmedium.hGlobal); 262: ReleaseStgMedium(&workingmedium); 263: // Now put the text into our buffer 264: alloclen = text.Length(); 265: hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen, 266: GHND); 267: if (hGlobalMemory == nullptr) 268: return E_FAIL; 269: buffer = ((char*)GlobalLock(hGlobalMemory) + buffersize); 270: if (!buffer) 271: return E_FAIL; 272: buffer--; // Overwrite the preceding null 273: memcpy(buffer, text.get(), alloclen); 274: GlobalUnlock(hGlobalMemory); 275: buffersize += alloclen; 276: } 277: pSTM->tymed = TYMED_HGLOBAL; 278: pSTM->pUnkForRelease = nullptr; // Caller gets to free the data 279: pSTM->hGlobal = hGlobalMemory; 280: return S_OK; 281: } 282: if (pFE->cfFormat == CF_UNICODETEXT) { 283: buffersize = sizeof(char16_t); 284: nsAutoString text; 285: for (uint32_t i = 0; i < mDataObjects.Length(); ++i) { 286: nsDataObj* dataObj = mDataObjects.ElementAt(i); 287: hr = dataObj->GetData(&fe, &workingmedium); ... 296: // Now we need to pull out the text 297: char16_t* buffer = (char16_t*)GlobalLock(workingmedium.hGlobal); 298: if (buffer == nullptr) 299: return E_FAIL; 300: text = buffer; 301: GlobalUnlock(workingmedium.hGlobal); 302: ReleaseStgMedium(&workingmedium); 303: // Now put the text into our buffer 304: alloclen = text.Length() * sizeof(char16_t); 305: hGlobalMemory = ::GlobalReAlloc(hGlobalMemory, buffersize + alloclen, 306: GHND); 307: if (hGlobalMemory == nullptr) 308: return E_FAIL; 309: buffer = (char16_t*)((char*)GlobalLock(hGlobalMemory) + buffersize); 310: if (!buffer) 311: return E_FAIL; 312: buffer--; // Overwrite the preceding null 313: memcpy(buffer, text.get(), alloclen); 314: GlobalUnlock(hGlobalMemory); 315: buffersize += alloclen; 316: } 317: pSTM->tymed = TYMED_HGLOBAL; 318: pSTM->pUnkForRelease = nullptr; // Caller gets to free the data 319: pSTM->hGlobal = hGlobalMemory; 320: return S_OK; 321: } 322: 323: return E_FAIL; 324: }
Use the POC by loading it into FF, attaching a debugger to FF, and setting a BP on nsNativeDragTarget::Drop(). Select the text "select and drag this text..." and drag and drop it onto the text "...to here!". Wait for the BP to fire, then examine |pData| to verify that it contains 17 objects totalling 0x100004000 bytes. Note that the first 16 objects are represented by clipboard cache files, because they are too large to be cached in memory; those items have |DataStruct::mDataLen| == 0. You can examine them (and their actual sizes) in the running account's temp folder (e.g., c:\users\browser\AppData\Local\temp\clipboardcache-xxxxx). The last object is cached in memory, and has |DataStruct::mDataLen| == 0x8000.
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 accumulator is a 32-bit unsigned integer that wraps when summing many large strings, and the wrapped value is used for allocation while the real lengths are used for copying.
Impact
If GetText() were invoked, an attacker could control both the amount and the content of data written beyond the buffer, potentially leading to memory corruption; the report does not show a path that actually calls it.
What the attacker needed
Windows build of Firefox; ability to trigger GetText() (not shown)
How it was found
- Load the proof-of-concept page in Firefox and attach a debugger.
- Set a breakpoint on nsNativeDragTarget::Drop().
- Select the text and drag it onto the drop target.
- When the breakpoint fires, inspect the data object to confirm 17 items totalling 0x100004000 bytes.
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.
The browser keeps a list of text items when you drag and drop. A function that joins them into one block adds up their sizes in a fixed-width number, which can wrap around to a tiny value. The code then trusts that tiny size and copies much more data than it allocated, corrupting memory.
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.