Overflow and latent write beyond bounds in DataTransfer::GetTransferable()
An integer overflow in DataTransfer::GetTransferable() can truncate transferable data, and if the function wrote an odd number of bytes, it would become exploitable to cause a write beyond bounds.
- Fixed in 14 days
- Paid in 16 days
- Disclosed after 14 days
Created attachment 8847831
bug_734_poc_1.htm
DataTransfer::GetTransferable() (dom\events\DataTransfer.cpp) can cause an integer overflow. Because of the incidental fact that this function only ever writes an even number of bytes to its temporary output stream |stream|, the overflow occurs, but it doesn't cause a write beyond bounds, only a truncation of the transferable data. If this function were modified to write an odd number of bytes to |stream|, this bug would become exploitable.
The bug is in the unguarded addition in lines 1071-72 (see below).
Attached is a POC that illustrates the overflow. Use it by attaching a debugger to FF and setting a BP on line 1071. Drag the text and wait for the BP to trigger. On the 8th BP, observe how |totalCustomLength| overflows.
If GetTransferable() ever wrote an odd number of bytes to |stream|, the overflow would become exploitable by allowing a script to use a series of DataTransfer.setData() calls that cause |totalCustomLength| to total exactly 0xfffffffb when control reaches line 1081. Line 1081 would then increment |totalCustomLength| to 0xffffffff, and line 1088 would then overflow and allocate 0 bytes of nsStringBuffer. But lines 1093-94 would read 0xffffffff bytes of content-provided data into the 0-length nsStringBuffer. (You can simulate this overrun using the debugger. Make sure to skip the debug-only assertions in nsStringBuffer::Alloc()).
951: already_AddRefed<nsITransferable>
952: DataTransfer::GetTransferable(uint32_t aIndex, nsILoadContext* aLoadContext)
953: {
954: if (aIndex >= MozItemCount()) {
955: return nullptr;
956: }
957:
958: const nsTArray<RefPtr<DataTransferItem>>& item = *mItems->MozItemsAt(aIndex);
959: uint32_t count = item.Length();
960: if (!count) {
961: return nullptr;
962: }
...
1008: do {
1009: for (uint32_t f = 0; f < count; f++) {
1010: RefPtr<DataTransferItem> formatitem = item[f];
1011: nsCOMPtr<nsIVariant> variant = formatitem->DataNoSecurityCheck();
1012: if (!variant) { // skip empty items
1013: continue;
1014: }
1015:
1016: nsAutoString type;
1017: formatitem->GetType(type);
1018:
1019: // If the data is of one of the well-known formats, use it directly.
1020: bool isCustomFormat = true;
1021: for (uint32_t f = 0; f < ArrayLength(knownFormats); f++) {
1022: if (type.EqualsASCII(knownFormats[f])) {
1023: isCustomFormat = false;
1024: break;
1025: }
1026: }
1027:
1028: uint32_t lengthInBytes;
1029: nsCOMPtr<nsISupports> convertedData;
1030:
1031: if (handlingCustomFormats) {
1032: if (!ConvertFromVariant(variant, getter_AddRefs(convertedData),
1033: &lengthInBytes)) {
1034: continue;
1035: }
1036:
1037: // When handling custom types, add the data to the stream if this is a
1038: // custom type.
1039: if (isCustomFormat) {
1040: // If it isn't a string, just ignore it. The dataTransfer is cached in
1041: // the drag sesion during drag-and-drop, so non-strings will be
1042: // available when dragging locally.
1043: nsCOMPtr<nsISupportsString> str(do_QueryInterface(convertedData));
1044: if (str) {
1045: nsAutoString data;
1046: str->GetData(data);
1047:
1048: if (!stream) {
1049: // Create a storage stream to write to.
1050: NS_NewStorageStream(1024, UINT32_MAX, getter_AddRefs(storageStream));
1051:
1052: nsCOMPtr<nsIOutputStream> outputStream;
1053: storageStream->GetOutputStream(0, getter_AddRefs(outputStream));
1054:
1055: stream = do_CreateInstance("@mozilla.org/binaryoutputstream;1");
1056: stream->SetOutputStream(outputStream);
1057: }
1058:
1059: int32_t formatLength = type.Length() * sizeof(nsString::char_type);
1060:
1061: stream->Write32(eCustomClipboardTypeId_String);
1062: stream->Write32(formatLength);
1063: stream->WriteBytes((const char *)type.get(),
1064: formatLength);
1065: stream->Write32(lengthInBytes);
1066: stream->WriteBytes((const char *)data.get(), lengthInBytes);
1067:
1068: // The total size of the stream is the format length, the data
1069: // length, two integers to hold the lengths and one integer for the
1070: // string flag.
1071: totalCustomLength +=
1072: formatLength + lengthInBytes + (sizeof(uint32_t) * 3);
1073: }
1074: }
1075: } else if (isCustomFormat && stream) {
1076: // This is the second pass of the loop (handlingCustomFormats is false).
1077: // When encountering the first custom format, append all of the stream
1078: // at this position.
1079:
1080: // Write out a terminator.
1081: totalCustomLength += sizeof(uint32_t);
1082: stream->Write32(eCustomClipboardTypeId_None);
1083:
1084: nsCOMPtr<nsIInputStream> inputStream;
1085: storageStream->NewInputStream(0, getter_AddRefs(inputStream));
1086:
1087: RefPtr<nsStringBuffer> stringBuffer =
1088: nsStringBuffer::Alloc(totalCustomLength + 1);
1089:
1090: // Read the data from the string and add a null-terminator as ToString
1091: // needs it.
1092: uint32_t amountRead;
1093: inputStream->Read(static_cast<char*>(stringBuffer->Data()),
1094: totalCustomLength, &amountRead);
1095: static_cast<char*>(stringBuffer->Data())[amountRead] = 0;
...
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 an unguarded integer addition when accumulating the total length of custom clipboard data, which can overflow a 32-bit unsigned integer.
Impact
Currently the overflow only truncates the transferable data, but if the function wrote an odd number of bytes it could become exploitable to cause a write beyond bounds.
What the attacker needed
Firefox browser; User drags text from a malicious page
How it was found
- Attach a debugger to Firefox and set a breakpoint on line 1071.
- Drag the text from the provided proof-of-concept page.
- Wait for the breakpoint to trigger.
- On the eighth breakpoint hit, observe that totalCustomLength overflows.
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.
This is a bug in Firefox's drag-and-drop code. When you drag text, the browser adds up the sizes of the pieces of data. If the total gets too big, the number wraps around to a small value, which could make the browser allocate a tiny buffer and then copy too much data into it. Right now the bug only causes data to be cut short, but a small change to the code would make it dangerous.
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.