Use of uninitialized objects / use after free causes memory corruption via DataTransfer::FillInExternalCustomTypes()
DataTransfer::FillInExternalCustomTypes() does not check the return status of stream reads, so a defective input stream leaves stack variables uninitialised or pointing to freed memory. An attacker could craft a malicious drag-and-drop object to cause memory corruption, leading to crashes or potentially arbitrary code execution.
- Fixed in 11 days
- Paid in 15 days
- Disclosed after 11 days
Created attachment 8848294
bug_735_poc_1.htm
DataTransfer::FillInExternalCustomTypes() (dom\events\DataTransfer.cpp) doesn't check status when reading from an input stream. If the stream is defective, such that insufficient or no data is read from it, FillInExternalCustomTypes() uses the resulting partially- or un-initialized stack variables to create various objects, which are then used by other code.
The stream can be derived from untrusted data.
In several tests, this bug causes memory corruption, resulting in access-violation crashes from attempts to read and/or write memory to which the executing code has no valid access.
The bugs are in all the calls to |stream -> Read*()| and also on the call to SetInputStream()}:
1491: void
1492: DataTransfer::FillInExternalCustomTypes(nsIVariant* aData, uint32_t aIndex,
1493: nsIPrincipal* aPrincipal)
1494: {
1495: char* chrs;
1496: uint32_t len = 0;
1497: nsresult rv = aData->GetAsStringWithSize(&len, &chrs);
1498: if (NS_FAILED(rv)) {
1499: return;
1500: }
1501:
1502: nsAutoCString str;
1503: str.Adopt(chrs, len);
1504:
1505: nsCOMPtr<nsIInputStream> stringStream;
1506: NS_NewCStringInputStream(getter_AddRefs(stringStream), str);
1507:
1508: nsCOMPtr<nsIBinaryInputStream> stream =
1509: do_CreateInstance("@mozilla.org/binaryinputstream;1");
1510: if (!stream) {
1511: return;
1512: }
1513:
1514: stream->SetInputStream(stringStream);
1515:
1516: uint32_t type;
1517: do {
1518: stream->Read32(&type);
1519: if (type == eCustomClipboardTypeId_String) {
1520: uint32_t formatLength;
1521: stream->Read32(&formatLength);
1522: char* formatBytes;
1523: stream->ReadBytes(formatLength, &formatBytes);
1524: nsAutoString format;
1525: format.Adopt(reinterpret_cast<char16_t*>(formatBytes),
1526: formatLength / sizeof(char16_t));
1527:
1528: uint32_t dataLength;
1529: stream->Read32(&dataLength);
1530: char* dataBytes;
1531: stream->ReadBytes(dataLength, &dataBytes);
1532: nsAutoString data;
1533: data.Adopt(reinterpret_cast<char16_t*>(dataBytes),
1534: dataLength / sizeof(char16_t));
1535:
1536: RefPtr<nsVariantCC> variant = new nsVariantCC();
1537: variant->SetAsAString(data);
1538:
1539: SetDataWithPrincipal(format, variant, aIndex, aPrincipal);
1540: }
1541: } while (type != eCustomClipboardTypeId_None);
1542: }
Attached is a POC, derived from the POC for https://bugzilla.mozilla.org/show_bug.cgi?id=1347748 , that creates a defective (in this case, truncated) input stream.
The POC uses lots of memory, so try it on a system with >= 16 GB.
Use it by opening the POC in two instances of FF and attaching the debugger to FF 1. Set a BP on DataTransfer::FillInExternalCustomTypes(), drag the text "Drag this text..." from FF 2 onto the text "...and drop it here!" in FF 1. Wait a minute or three for the BP to fire. On the first iteration of the loop, the code reads |formatLength| == 0x1a, and the correct |formatBytes| "hacker/hacker". It then reads the bogus |dataLength| 0x2000, but |ReadBytes()| on line 1531 gets only 0x62 bytes from the stream, then returns failure. Line 1533 "adopts" the resulting string (which might or might not be OK, depending upon what trash was in the uninitialized pointer |dataBytes|.
On the second iteration, the stream is at EOF, so none of the Read() functions return any data. |formatLength| is, thus, unchanged (at 0x1a), but |formatBytes| points to freed memory. Lines 1525-26 "adopt" this garbage into |format|. A similar thing then happens with |data|.
You can now remove the BP and proceed, and (if your memory contents are appropriate) the POC might display an alert box with a string beginning "xxxxxxx" followed by trash read from beyond bounds. Or FF might crash if your memory contents are otherwise. To get more interesting crashes, launch a WebGL video in a different tab in FF 1 before dropping the text.
An attacker could create a more-defective input stream and easily convince a user to drop the object from which it is derived onto FF. She could then use a JS drop handler (deHandler() in the POC) to read the data, and then AJAX it to a remote server.
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
The code fails to check the return value of stream read operations, so on failure it uses uninitialised or freed memory.
Impact
The bug causes memory corruption, resulting in access-violation crashes; an attacker could potentially exploit it for code execution, though the report only demonstrates crashes and out-of-bounds reads.
What the attacker needed
Victim drags and drops a malicious object; attacker can craft a defective stream; Firefox browser
The fix
The code should check the return status of all stream read operations and handle failures gracefully.
How it was found
- Open the POC in two Firefox instances and attach a debugger to the first.
- Set a breakpoint on DataTransfer::FillInExternalCustomTypes().
- Drag the text from the second instance onto the drop target in the first.
- Wait for the breakpoint to fire and observe the stream reads.
- Remove the breakpoint and continue execution to see the crash or alert.
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 reads data from a stream, it should check whether the read succeeded. Here, the code ignores failures, so it uses garbage values left on the stack. An attacker can supply a broken stream via drag-and-drop to trigger memory corruption, which can crash the browser or possibly let them run 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.