Skip to content
Memory corruption Disclosed report Advanced

Overflow in nsXMLContentSink::AddText causes memory-safety bug

An integer overflow in the XML content sink's text-buffer growth logic can make a reallocation request wrap to a tiny size, after which the code copies text past the end of the allocated buffer. The reporter claims this heap overflow could allow an attacker to run chosen code by corrupting an object's virtual-function table.

q1 Follow

Originally disclosed on Mozilla

  • Fixed in 6.5 years
  • Paid in 3 months
  • Disclosed after 6.5 years
Text
User Agent: Mozilla/5.0 (Windows NT 5.1; rv:36.0) Gecko/20100101 Firefox/36.0
Build ID: 20150305021524

Steps to reproduce:

nsXMLContentSink::AddText can experience an overflow, causing a realloc to a tiny buffer size. The function then writes to unallocated space, potentially permitting the execution of attacker-chosen code, as by modifying an object's virtual-function table:

1436: nsresult
1437: nsXMLContentSink::AddText(const char16_t* aText,
1438:                           int32_t aLength)
1439: {
      ...
1448:   // Copy data from string into our buffer; flush buffer when it fills up
1449:   int32_t offset = 0;
1450:   while (0 != aLength) {
1451:     int32_t amount = mTextSize - mTextLength;
1452:     if (0 == amount) {
1453:       // XSLT wants adjacent textnodes merged.
1454:       if (mConstrainSize && !mXSLTProcessor) {
1455:         nsresult rv = FlushText();
1456:         if (NS_OK != rv) {
1457:           return rv;
1458:         }
1459:
1460:         amount = mTextSize - mTextLength;
1461:       }
1462:       else {
1463:         mTextSize += aLength;
1464:         mText = (char16_t *) PR_REALLOC(mText, sizeof(char16_t) * mTextSize);
1465:         if (nullptr == mText) {
1466:           mTextSize = 0;
1467: 
1468:           return NS_ERROR_OUT_OF_MEMORY;
1469:         }
1470:
1471:         amount = aLength;
1472:       }
1473:     }
1474:     if (amount > aLength) {
1475:       amount = aLength;
1476:     }
1477:     memcpy(&mText[mTextLength], &aText[offset], sizeof(char16_t) * amount);
1478:     mTextLength += amount;
1479:     offset += amount;
1480:     aLength -= amount;
1481:   }

The bug is in line 1463, which can overflow. Line 1464 then allocates a buffer that is too small to contain mTextSize + aLength characters, and line 1477 copies data beyond the buffer's end.

The original report as published on Mozilla's Bugzilla, by q1. 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 an integer overflow when adding the incoming length to the current buffer size, so the reallocation size wraps to a small value while the copy loop still uses the larger length.

Impact

A heap buffer overflow that could permit execution of attacker-chosen code, though the report gives no working exploit and only describes the mechanism.

What the attacker needed

Attacker-supplied XML parsed by the sink

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 keeps a growing text buffer and asks the system for more memory when it fills up. Here the size calculation can wrap around to a very small number, so the program gets a tiny buffer but still copies a lot of text into it, writing past the end. That kind of out-of-bounds write can let an attacker take over the program.

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 q1

View all

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