Skip to content
Memory corruption Disclosed report Advanced CVE-2018-12393

Unsafe use of CheckedInt (possible buffer overflow) in ScriptLoader::ConvertToUTF16

The code multiplies a checked integer by the size of a character type without re-checking for overflow, so a large input could cause a smaller buffer to be allocated and lead to an out-of-bounds write. An attacker might exploit this to cause memory corruption, though the report notes that caller checks may prevent it.

r2 Follow

Originally disclosed on Mozilla

  • Fixed in 10 days
  • Paid in 3 months
  • Disclosed after 10 days
Text
User Agent: Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0
Build ID: 20100101

Steps to reproduce:

The issue is in ScriptLoader::ConvertToUTF16 (https://searchfox.org/mozilla-central/source/dom/script/ScriptLoader.cpp) :

  CheckedInt<size_t> unicodeLength =
    unicodeDecoder->MaxUTF16BufferLength(aLength);
  if (!unicodeLength.isValid()) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  aBufOut =
    static_cast<char16_t*>(js_malloc(unicodeLength.value() * sizeof(char16_t)));
  if (!aBufOut) {
    aLengthOut = 0;
    return NS_ERROR_OUT_OF_MEMORY;
  }

At line 2826, unicodeLength is calculated and sanitized against an integer overflow via the CheckedInt class. However just a few lines below, the raw value of unicodeLength (unicodeLength.value()) is multiplied by 2 (=sizeof(char16_t)) and thus can cause an overflow! In such case js_malloc will allocate a smaller buffer than required, will all the usual arbitrary out-of-bounds write consequences.

It's not clear whether any checks in the (multiple) callers sanitize away the overflow window, however the fact that this code uses the CheckedInt class strongly suggests that either it has overflowed in the past, or it has a potential for the overflow.

I checked the crash stats and they do contain quite a few bugs around this code, but maybe not all of them due to this specific issue.

The original report as published on Mozilla's Bugzilla, by r2. 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 the code fails to re-validate the result of multiplying a checked integer by the size of char16_t, allowing an integer overflow that leads to an undersized allocation.

Impact

This could lead to an out-of-bounds write and memory corruption, but the report notes that checks in callers might prevent the overflow from being reachable.

What the attacker needed

Attacker controls input length; caller checks may block

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 about a program that calculates how much memory to reserve for text. It checks one number for overflow but then multiplies it by two without checking again, so the actual memory needed could be much larger than what is reserved. If that happens, writing the text can spill past the reserved area and corrupt other data.

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 r2

View all

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