Skip to content
Memory corruption Disclosed report Advanced

heap-use-after-free in SpeechEvent

A speech recognition event object kept only a raw pointer to the SpeechRecognition object that created it. If the JavaScript object was garbage collected before the queued event ran on the main thread, the event dereferenced freed memory, causing a use-after-free.

Kirin Follow

Originally disclosed on Mozilla

  • Fixed in 3 months
  • Paid in 3 months
  • Disclosed after 3 months

Created attachment 9271726 asan.log

heap-use-after-free in SpeechEvent

VERSION

Firefox Nightly Build ( 101.0a1 )

VULNERABILITY DETAILS

I noticed that the SpeechEvent only hold a Raw Pointer to SpeechRecognition (mRecognition) (1):

class SpeechEvent : public Runnable {
 public:
  SpeechEvent(SpeechRecognition* aRecognition,
              SpeechRecognition::EventType aType);

  ~SpeechEvent();

  NS_IMETHOD Run() override;
  ......
  ......

 private:
  SpeechRecognition* mRecognition; // **** 1 ****
  ......
};
} 

SpeechEvent::Run() {
  mRecognition->ProcessEvent(this); // **** 2 ****
  return NS_OK;
}

When a SpeechEvent dispatch to MainThread, the SpeechRecognition can be released in the JavaScript context of MainThread. And when the MainThread completes the current task and trigger SpeechEvent::Run, it will lead to a use-after-free issue in (2).

REPRODUCTION CASE

To support SpeechRecognition, the media.webspeech.recognition.force_enable and media.webspeech.recognition.enable should be enable.

POC:

<html>
<script>
forceGC = () => {
   // To trigger easily => fuzzing.enabled: true
   FuzzingFunctions.garbageCollect();
   FuzzingFunctions.cycleCollect();
}

var recognition = new SpeechRecognition();

recognition.abort(); // or stop()
recognition=null; 
forceGC();
</script>
</html>

ASAN LOG

See asan.log in attachment.

Suggestion

Hold a reference of SpeechRecognition in SpeechEvent to avoid it be released before SpeechEvent::Run.

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 SpeechEvent stored a raw pointer to SpeechRecognition instead of a strong reference, so the object could be destroyed before the queued event executed.

Impact

A page could trigger a use-after-free in the browser process, which may crash the browser and could potentially be exploited for code execution.

What the attacker needed

Speech recognition prefs enabled; victim loads a malicious page

The fix

The event should hold a strong reference to the SpeechRecognition object so it cannot be released before the event runs.

How it was found

  1. Enable the two speech recognition preferences so the feature is available.
  2. Create a SpeechRecognition object in a page script.
  3. Call abort or stop on it, then drop the reference by setting it to null.
  4. Force garbage collection and cycle collection so the object is freed.
  5. The queued SpeechEvent then runs and dereferences the freed object.

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 can queue work to run later, and that work sometimes refers back to an object it did not keep alive. Here the queued speech event held only a memory address, not a real reference, so the browser was free to delete the speech recognition object first. When the event finally ran, it read memory that had already been freed, which is a use-after-free and can crash or be abused.

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.