Write beyond bounds in Key::EncodeAsString()
An integer overflow in the Key::EncodeAsString() function of Firefox's IndexedDB code lets a crafted string cause a size calculation to wrap around, so the code writes roughly 4GB of partly attacker-controlled data past the end of its buffer. An attacker could use this to corrupt memory in the browser process.
- Fixed in 48 days
- Paid in 53 days
- Disclosed after 48 days
Created attachment 8875939
bug_897_poc_2.htm
Key::EncodeAsString() (dom\indexedDB\key.cpp) can cause an integer overflow. When it does, it writes ~ 4GB of partially attacker-provided data beyond the end of its |mBuffer| array.
The attached POC demonstrates the overwrite.
The bug is in line 454, which does not check whether the addition overflows. There is also a similar bug on the "+=" in line 447 that might be caused to overflow with similar results,
433: template <typename T>
434: void
435: Key::EncodeAsString(const T* aStart, const T* aEnd, uint8_t aType)
436: {
437: // First measure how long the encoded string will be.
438:
439: // The +2 is for initial 3 and trailing 0. We'll compensate for multi-byte
440: // chars below.
441: uint32_t size = (aEnd - aStart) + 2;
442:
443: const T* start = aStart;
444: const T* end = aEnd;
445: for (const T* iter = start; iter < end; ++iter) {
446: if (*iter > ONE_BYTE_LIMIT) {
447: size += char16_t(*iter) > TWO_BYTE_LIMIT ? 2 : 1;
448: }
449: }
450:
451: // Allocate memory for the new size
452: uint32_t oldLen = mBuffer.Length();
453: char* buffer;
454: if (!mBuffer.GetMutableData(&buffer, oldLen + size)) {
455: return;
456: }
457: buffer += oldLen;
458:
459: // Write type marker
460: *(buffer++) = aType;
461:
462: // Encode string
463: for (const T* iter = start; iter < end; ++iter) {
464: if (*iter <= ONE_BYTE_LIMIT) {
465: *(buffer++) = *iter + ONE_BYTE_ADJUST;
466: }
467: else if (char16_t(*iter) <= TWO_BYTE_LIMIT) {
468: char16_t c = char16_t(*iter) + TWO_BYTE_ADJUST + 0x8000;
469: *(buffer++) = (char)(c >> 8);
470: *(buffer++) = (char)(c & 0xFF);
471: }
472: else {
473: uint32_t c = (uint32_t(*iter) << THREE_BYTE_SHIFT) | 0x00C00000;
474: *(buffer++) = (char)(c >> 16);
475: *(buffer++) = (char)(c >> 8);
476: *(buffer++) = (char)c;
477: }
478: }
479:
480: // Write end marker
481: *(buffer++) = eTerminator;
482:
483: NS_ASSERTION(buffer == mBuffer.EndReading(), "Wrote wrong number of bytes");
484: }
Use the POC by expanding the .7z file into the corresponding ".gz" file [1], and saving it and the .htm file to a webserver. Start 64-bit FF, attach a debugger to it, and set a BP on line 454.
Load the .htm file in FF and wait for a few minutes for it to load.
The BP will trigger twice. Ignore the first hit. On the second hit, step the assembly code for line 454 and watch the quantity |oldLen + size| overflow to 4. Then step into the |for| loop beginning on line 463 and watch it write data determined by the .gz file's contents (interspersed with 0s) far beyond |mBuffer|'s end.
[1] (it is really just a file of bytes of 0x7f)
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 adds lengths and per-character expansion without checking for integer overflow, so the allocation is smaller than the data written.
Impact
The overflow causes a large out-of-bounds write of partly attacker-controlled data, which could lead to memory corruption in the browser.
What the attacker needed
Victim loads a crafted web page; 64-bit Firefox
How it was found
- Expand the provided archive into a .gz file and place it with the .htm file on a web server.
- Start a 64-bit Firefox and attach a debugger, setting a breakpoint on line 454.
- Load the .htm file and wait a few minutes for it to load.
- The breakpoint triggers twice; ignore the first hit and on the second step through the assembly to see the length addition overflow.
- Step into the encoding loop and observe writes far beyond the end of the buffer.
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.
A program often reserves a block of memory and then fills it with data. Here the code works out how big the block should be by adding numbers together, but the addition can wrap around to a tiny value, so the block is far too small for what gets written into it. Writing past the end of a reserved block can crash the browser or let an attacker control nearby 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.