Closed Bug 1764112 Opened 4 years ago Closed 4 years ago

heap-use-after-free in SpeechEvent

Categories

(Core :: Web Speech, defect)

defect

Tracking

()

RESOLVED FIXED
104 Branch
Tracking Status
firefox-esr91 - disabled
firefox-esr102 --- disabled
firefox99 --- wontfix
firefox100 --- wontfix
firefox101 - wontfix
firefox102 --- wontfix
firefox103 + disabled
firefox104 --- fixed

People

(Reporter: kirin.say, Assigned: smaug)

Details

(Keywords: csectype-uaf, reporter-external, sec-high, Whiteboard: [reporter-external] [client-bounty-form] [verif?])

Attachments

(2 files, 1 external file, 1 obsolete file)

Attached file 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.

Flags: sec-bounty?
Group: firefox-core-security → core-security
Component: Security → Web Speech
Product: Firefox → Core
Group: core-security → dom-core-security
Keywords: csectype-uaf
Assignee: nobody → bugs
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true

Comment on attachment 9271870 [details]
Bug 1764112, ensure there is a reasonable SpeechRecognition object around when handling SpeechEvents, r=eeejay

Security Approval Request

  • How easily could an exploit be constructed based on the patch?: The patch does pinpoint where the issue is.
  • Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?: Yes
  • Which older supported branches are affected by this flaw?: all, but behind a pref
  • If not all supported branches, which bug introduced the flaw?: None
  • Do you have backports for the affected branches?: Yes
  • If not, how different, hard to create, and risky will they be?: the same patch should work everywhere.
  • How likely is this patch to cause regressions; how much testing does it need?: This is disabled by default (so I'm not even sure approval is needed)

https://searchfox.org/mozilla-central/source/dom/webidl/SpeechRecognition.webidl#13
https://searchfox.org/mozilla-central/source/modules/libpref/init/StaticPrefList.yaml#9207,9209

Attachment #9271870 - Flags: sec-approval?

I just noticed a change in the status of this issue.
Thanks for your patch and such a quick CONFIRM and response!

Comment on attachment 9271870 [details]
Bug 1764112, ensure there is a reasonable SpeechRecognition object around when handling SpeechEvents, r=eeejay

Approved to land and uplift

Attachment #9271870 - Flags: sec-approval? → sec-approval+

Comment on attachment 9271870 [details]
Bug 1764112, ensure there is a reasonable SpeechRecognition object around when handling SpeechEvents, r=eeejay

Beta/Release Uplift Approval Request

  • User impact if declined: This code is disabled by default, so this affects only some unusual cases
  • Is this code covered by automated tests?: No
  • Has the fix been verified in Nightly?: No
  • Needs manual test from QE?: No
  • If yes, steps to reproduce:
  • List of other uplifts needed: None
  • Risk to taking this patch: Low
  • Why is the change risky/not risky? (and alternatives if risky): Modifies preffed off code to be safer.
  • String changes made/needed:

ESR Uplift Approval Request

  • If this is not a sec:{high,crit} bug, please state case for ESR consideration:
  • User impact if declined:
  • Fix Landed on Version:
  • Risk to taking this patch: Low
  • Why is the change risky/not risky? (and alternatives if risky):
Attachment #9271870 - Flags: approval-mozilla-esr91?
Attachment #9271870 - Flags: approval-mozilla-beta?

oh, interesting. That is actually a valid issue I guess

Comment on attachment 9271870 [details]
Bug 1764112, ensure there is a reasonable SpeechRecognition object around when handling SpeechEvents, r=eeejay

Declining the nomination given that the patch had to be backed out for causing crashes and that there are no more betas left in 100. I think it is safer to let it ride the trains, but feel free to NI me if there is strong need to get this into an RC build once a patch is ready given that its sec-high.

Attachment #9271870 - Flags: approval-mozilla-beta? → approval-mozilla-beta-

I'm not sure why there's a crash here. It seems like there is a invalid nsCycleCollector (Null Dereference) in the Context of SpeechRecognition::FeedAudioData (in GraphRunner thread):

* thread #49, name = 'GraphRunner', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x000000010b0ee08c XUL`RefPtr<nsCycleCollector>::operator!(this=0x0000000000000000) const at RefPtr.h:311:36
   308 	  // needed separately?  Shouldn't the compiler prefer using the non-deleted
   309 	  // operator bool instead of the deleted operator T*?
   310 	  explicit operator bool() const { return !!mRawPtr; }
-> 311 	  bool operator!() const { return !mRawPtr; }
   312
   313 	  T* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
   314 	    MOZ_ASSERT(mRawPtr != nullptr,
Target 0: (plugin-container) stopped.
(lldb) bt
* thread #49, name = 'GraphRunner', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x000000010b0ee08c XUL`RefPtr<nsCycleCollector>::operator!(this=0x0000000000000000) const at RefPtr.h:311:36
    frame #1: 0x000000010b0edf17 XUL`::NS_CycleCollectorSuspect3(aPtr=0x0000000150ea9400, aCp=0x0000000000000000, aRefCnt=0x0000000150ea9438, aShouldDelete=0x0000000000000000) at nsCycleCollector.cpp:3764:7
    frame #2: 0x000000010f913c8b XUL`mozilla::DOMEventTargetHelper::AddRef() [inlined] unsigned long nsCycleCollectingAutoRefCnt::incr<&(NS_CycleCollectorSuspect3)>(this=0x0000000150ea9438, aOwner=0x0000000150ea9400, aCp=0x0000000000000000) at nsISupportsImpl.h:248:7
    frame #3: 0x000000010f913c3a XUL`mozilla::DOMEventTargetHelper::AddRef() [inlined] unsigned long nsCycleCollectingAutoRefCnt::incr<&(NS_CycleCollectorSuspect3)>(this=0x0000000150ea9438, aOwner=0x0000000150ea9400) at nsISupportsImpl.h:234
    frame #4: 0x000000010f913c1e XUL`mozilla::DOMEventTargetHelper::AddRef(this=0x0000000150ea9400) at DOMEventTargetHelper.cpp:85
    frame #5: 0x0000000110b95875 XUL`mozilla::dom::SpeechRecognition::AddRef(this=0x0000000150ea9400) at SpeechRecognition.cpp:145:1
    frame #6: 0x000000010e6d4f85 XUL`mozilla::RefPtrTraits<mozilla::dom::SpeechRecognition>::AddRef(aPtr=0x0000000150ea9400) at RefPtr.h:49:39
    frame #7: 0x000000010e6d4f25 XUL`RefPtr<mozilla::dom::SpeechRecognition>::ConstRemovingRefPtrTraits<mozilla::dom::SpeechRecognition>::AddRef(aPtr=0x0000000150ea9400) at RefPtr.h:380:35
    frame #8: 0x0000000110ba5a15 XUL`RefPtr<mozilla::dom::SpeechRecognition>::RefPtr(this=0x0000000150f16d18, aRawPtr=0x0000000150ea9400) at RefPtr.h:109:7
    frame #9: 0x0000000110b9679d XUL`RefPtr<mozilla::dom::SpeechRecognition>::RefPtr(this=0x0000000150f16d18, aRawPtr=0x0000000150ea9400) at RefPtr.h:107:54
    frame #10: 0x0000000110b9b7cf XUL`mozilla::dom::SpeechEvent::SpeechEvent(this=0x0000000150f16ce0, aRecognition=0x0000000150ea9400, aType=EVENT_AUDIO_DATA) at SpeechRecognition.cpp:1149:7
    frame #11: 0x0000000110b91473 XUL`mozilla::dom::SpeechEvent::SpeechEvent(this=0x0000000150f16ce0, aRecognition=0x0000000150ea9400, aType=EVENT_AUDIO_DATA) at SpeechRecognition.cpp:1151:21
    frame #12: 0x0000000110b9b5cf XUL`mozilla::dom::SpeechRecognition::FeedAudioData(this=0x0000000150ea9400, aSamples=already_AddRefed<mozilla::SharedBuffer> @ 0x0000700003c023f8, aDuration=480, aProvider=0x0000000150e47160, aTrackRate=48000) at SpeechRecognition.cpp:1101:35
......
......

The crash is clear. We're in a wrong thread.

Flags: needinfo?(bugs)

The severity field is not set for this bug.
:anatal, could you have a look please?

For more information, please visit auto_nag documentation.

Flags: needinfo?(anatal)

Hi, will you update a new patch for this issue recently? Thanks.

Yes I will. ( Obviously this issue doesn't happen unless on changes the pref )

Attachment #9271870 - Flags: approval-mozilla-esr91?

Hello,
Anything update for this issue?

Olli and I talked a bit - we were not sure if sec-high is the right rating for this issue, given the feature is disabled by default.

Security bugs are rated as though the feature is enabled, which can be a bit awkward in cases like this. However, it does mean that sec-high bugs are not always actually high priority bugs to fix.

[Tracking Requested - why for this release]: This feature is disabled by default, so we probably don't need to fix it right away.

Thank you for still working on this vulnerability.

I don't know the detailed rules of rating here. But I suggest you consider that this vulnerability does not require user interaction to trigger. And I have found that some developers and a small number of users will enable this configuration to make the website compatible.(Just a suggestion).

Since vulnerabilities are not easily fixed in imperfect modules(and its patch was back out once), I agree to delay the fix of the issue.
But will you commit a valid patch before the end of the 90-day deadline?

Hsin-Yi, can you answer comment 19?

Flags: needinfo?(htsai)

Let me fix this.

Flags: needinfo?(htsai)
Attachment #9271870 - Attachment is obsolete: true

Comment on attachment 9282336 [details]
Bug 1764112, ensure there is a reasonable SpeechRecognition object around when handling SpeechEvents, r=eeejay

Security Approval Request

  • How easily could an exploit be constructed based on the patch?: The patch does pinpoint where the issue is.
  • Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?: Yes
  • Which older supported branches are affected by this flaw?: all, but behind a pref
  • If not all supported branches, which bug introduced the flaw?: None
  • Do you have backports for the affected branches?: Yes
  • If not, how different, hard to create, and risky will they be?: the same patch should work everywhere.
  • How likely is this patch to cause regressions; how much testing does it need?: This is disabled by default (so I'm not even sure approval is needed)
  • Is Android affected?: Yes
Attachment #9282336 - Flags: sec-approval?

Comment on attachment 9282336 [details]
Bug 1764112, ensure there is a reasonable SpeechRecognition object around when handling SpeechEvents, r=eeejay

Approved to land and uplift

Attachment #9282336 - Flags: sec-approval? → sec-approval+

ensure there is a reasonable SpeechRecognition object around when handling SpeechEvents, r=eeejay
https://hg.mozilla.org/integration/autoland/rev/1bc2769872aa43a9a820ecb0037d60d8816073b1
https://hg.mozilla.org/mozilla-central/rev/1bc2769872aa

Group: dom-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 4 years ago
Resolution: --- → FIXED
Target Milestone: --- → 104 Branch

Since nightly and release are affected, beta will likely be affected too.
For more information, please visit auto_nag documentation.

[Tracking Requested - why for this release]:

Flags: sec-bounty? → sec-bounty+

Are we intending to nominate this for Beta/ESR102/ESR91 uplift? Please do so if yes. It grafts cleanly as-landed.

Flags: needinfo?(anatal) → needinfo?(bugs)

I don't know whether we need this elsewhere. The code is disabled by a pref by default.
Do we have some generic rule about this kind of case?

Flags: needinfo?(bugs) → needinfo?(dveditz)
QA Whiteboard: [post-critsmash-triage]
Flags: qe-verify-

This is a hard question. Normally we'd consider this an unfinished experimental feature and wouldn't back-port it. But on Chrome's stats site it appears to be used quite a lot. This might encourage more people that we expect to turn this feature on and therefore make themselves vulnerable. We are not, unfortunately, gathering our own stats on the use of this feature or pref setting as far as I can see which makes everything a big guess.

In the short term we don't need to worry about ESR-91 (but it doesn't really hurt to fix it if it grafts cleanly)

In the longer term do we need to worry about ESR-102? From the Chrome stats it appears the number of sites using the API is growing. It might just be sites using it as part of a fingerprinting API, but maybe it's really for compatibility with sites that have worked in Chrome for a while. hard to say. The "better safe than sorry" principal says to land this on ESR-102 and beta at least. It might be easier to simply land this on ESR-91 than to have to justify why we think the risk is remote.

Flags: needinfo?(dveditz)

It's too late for Beta 103 at this point, but we could still take it for 91.13/102.2esr next cycle. I thought in general we were being more proactive about backports for preffed-off features these days given some of the recent incidents.

Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: