Heap use-after-free in [@ mozilla::net::WebSocketChannelChild::MaybeReleaseIPCObject] due to count==1 race in WebSocketChannelChild::Release()
Categories
(Core :: Networking: WebSockets, defect, P1)
Tracking
()
People
(Reporter: tjr, Assigned: kershaw)
Details
(5 keywords, Whiteboard: [necko-priority-queue][necko-triaged][adv-main155+r][adv-esr153.2+r][adv-esr140.15+r][adv-esr115.40+r])
Attachments
(7 files, 5 external files)
WebSocketChannelChild has a hand-written threadsafe Release() (netwerk/protocol/websocket/WebSocketChannelChild.cpp:29-45) with special handling for an observed refcount of 1: it calls MaybeReleaseIPCObject() and then reads mRefCnt again, even though the decrementing thread no longer owns any reference to the object. When the count reaches 1, the only remaining reference is the IPDL reference taken by AddIPDLReference(); that reference is released on the main thread by NeckoChild::DeallocPWebSocketChild -> ReleaseIPDLReference() -> Release() -> delete this (netwerk/ipc/NeckoChild.cpp:120-124) when the parent's delete reply to SendDeleteSelf is processed. MaybeReleaseIPCObject() (WebSocketChannelChild.cpp:98-118) dereferences this throughout: on a non-main thread it reads mNeckoTarget via NeckoTargetHolder::GetNeckoTarget() (netwerk/ipc/NeckoTargetHolder.cpp:13) and would then AddRef this into a runnable; on the main thread it locks mMutex and calls SendDeleteSelf(). None of this is protected against the concurrent final release.
The object is released from two threads in normal operation: for worker WebSockets, delivered channel events (WrappedWebSocketEvent, ChannelEventQueue CompleteResumeRunnable) hold strong channel references released on the worker thread, while the IPDL reference is released on the main thread. A hostile page plus its own WebSocket server can arrange for a worker-thread release to observe count==1 (entering MaybeReleaseIPCObject with an unowned this) while the main thread independently observes count==1, sends DeleteSelf, receives the parent's delete, and frees the 416-byte WebSocketChannelChild. When the worker thread resumes it reads (and would subsequently write the refcount of) freed memory. Reproduced under ASAN in the WebCOOP+COEP content process with a sleep-only race-widening patch; the crash is a use-after-free read followed by a would-be refcount write and a virtual IPC send on freed memory, i.e. exploitable memory corruption in the content process triggered by remote content.
Build Info
- Branch: main
- Revision: ca6e9493686b3e5ed1cddb8d3a3974068463df71
- Timestamp: 2026-07-29T00:55:15+00:00
Affected Code
File: netwerk/protocol/websocket/WebSocketChannelChild.cpp, line 29-45
NS_IMETHODIMP_(MozExternalRefCountType) WebSocketChannelChild::Release() {
MOZ_ASSERT(0 != mRefCnt, "dup release");
nsrefcnt count = --mRefCnt; // caller now owns ZERO references
NS_LOG_RELEASE(this, mRefCnt, "WebSocketChannelChild");
if (count == 1) {
MaybeReleaseIPCObject(); // uses `this` without owning a reference
return mRefCnt; // reads member of possibly-freed object
}
if (count == 0) {
mRefCnt = 1; /* stabilize */
delete this;
return 0;
}
return count;
}
File: netwerk/protocol/websocket/WebSocketChannelChild.cpp, line 98-118
void WebSocketChannelChild::MaybeReleaseIPCObject() {
if (!NS_IsMainThread()) {
nsCOMPtr<nsIEventTarget> target = GetNeckoTarget(); // UAF READ: this->mNeckoTarget
MOZ_ALWAYS_SUCCEEDS(target->Dispatch(
NewRunnableMethod("WebSocketChannelChild::MaybeReleaseIPCObject", this,
&WebSocketChannelChild::MaybeReleaseIPCObject), // AddRef writes freed refcount
NS_DISPATCH_NORMAL));
return;
}
{
MutexAutoLock lock(mMutex); // uses this->mMutex
if (mIPCState != Opened) {
return;
}
mIPCState = Closing;
}
SendDeleteSelf(); // IPC send on unowned `this`
}
File: netwerk/ipc/NeckoChild.cpp, line 120-124
bool NeckoChild::DeallocPWebSocketChild(PWebSocketChild* child) {
WebSocketChannelChild* p = static_cast<WebSocketChannelChild*>(child);
p->ReleaseIPDLReference(); // main thread: final Release() -> delete this
return true;
}
Release() lets a non-owning thread act on the count==1 observation. The sole remaining (IPDL) reference is released on the main thread when the parent's delete (the reply to SendDeleteSelf) destroys the actor's ActorLifecycleProxy, so everything MaybeReleaseIPCObject() does with this races the true owner of the last reference. The mIPCState mutex cannot close the window because the object containing the mutex is itself freed. The worker-thread releases come from WrappedWebSocketEvent / ChannelEventQueue::CompleteResumeRunnable destructors, which hold strong channel references delivered to the worker thread (confirmed by the reproduced crash stacks).
Exploit Chain
- A hostile page creates a dedicated worker that opens a WebSocket to the attacker's server; the server completes the handshake and floods small text frames in bursts, forever, and never completes the closing handshake, so the parent keeps relaying frames during teardown.
- The page calls worker.terminate() mid-flood. On the worker thread, WebSocketImpl teardown closes the channel and WebSocketImpl::Disconnect() queues the DOM's mChannel reference release to the main thread (NS_ReleaseOnMainThread, dom/websocket/WebSocket.cpp:677).
- The main thread drops the DOM reference while the worker thread still holds queued WrappedWebSocketEvent / CompleteResumeRunnable strong references from the frame-flood backlog.
- The worker drains its event queue; the final release decrements the channel refcount 2->1 and enters MaybeReleaseIPCObject() on the worker thread with zero owned references — the unowned-
thiswindow. - Concurrently, continued RecvOnMessageAvailable traffic on the main thread creates and destroys transient channel references; a main-thread release also observes count==1 and runs the main-thread path: mIPCState = Closing, SendDeleteSelf() to the parent.
- The parent replies with delete; the content-process main thread runs ActorDestroy / ActorLifecycleProxy teardown -> NeckoChild::DeallocPWebSocketChild -> ReleaseIPDLReference() -> Release() 1->0 -> delete this, freeing the 416-byte WebSocketChannelChild while the worker thread is still inside its window.
- The worker thread resumes: GetNeckoTarget() reads mNeckoTarget from the freed object (ASAN: READ of size 8 at offset 264 of the 416-byte region), and NewRunnableMethod(this) would then AddRef — a write into the freed refcount — followed later by a virtual SendDeleteSelf on freed memory; with heap grooming this is an exploitable use-after-free in the content process.
IPC Path
- PWebSocket teardown
- Protocol: PWebSocket
- Parent process: Main
- Child process: Content
- Message flow: Content -> Main: DeleteSelf(); Main -> Content: delete (frees WebSocketChannelChild via DeallocPWebSocketChild while worker thread still uses it)
Steps to Reproduce
- Apply source_patch.diff (three PR_Sleep race-widening calls only: 400 ms in the off-main-thread branch of WebSocketChannelChild::MaybeReleaseIPCObject, 80 ms before SendDeleteSelf on the main-thread branch, 50 ms in the worker-side path of WebSocketImpl::Disconnect) and build the Linux ASAN fuzzing configuration (mozconfig.linux.asan.fuzzing).
- Start the hostile WebSocket server: python3 ws_flood_server.py (listens on 127.0.0.1:8931; floods 32 x 24-byte text frames every 25 ms and ignores client close frames).
- Load testcase.html over HTTP with prefs network.websocket.timeout.close=1 and browser.dom.window.dump.enabled=true. The testcase repeatedly spawns a worker that opens ws://127.0.0.1:8931/ and terminates it 400 ms later.
- The WebCOOP+COEP content process crashes with the heap-use-after-free within a few seconds (reproduced on the first run in this verification).
Security Impact
- Severity: Moderate
- Attacker capability: A remote website (with its own WebSocket server, no compromised process required) can trigger a use-after-free of a 416-byte WebSocketChannelChild in the victim's content process: a read of a freed member, followed by a refcount write into freed memory and a virtual IPC send on the freed object. With heap grooming of the freed 416-byte slot this is a memory-corruption primitive that can lead to arbitrary code execution in the content process.
- Preconditions: Victim visits an attacker-controlled page during normal browsing. The bug is a cross-thread race: the worker thread must be preempted between the refcount decrement in Release() and the member read in MaybeReleaseIPCObject() for the duration of a full child->parent->child IPC round-trip. The deterministic reproduction required inserted sleeps of 50-400 ms; natively the window is a few instructions wide, making each attempt unreliable, though the attacker can retry indefinitely (the testcase respawns a worker every 1.6 s and each frame burst yields many release races) with no penalty on failure. Per the severity rubric, the unreliable-timing constraint drops this one level from the memory-safety default of High.
ASAN Report
==32284==ERROR: AddressSanitizer: heap-use-after-free on address 0x775a36006148 at pc 0x761a0a4d6b0d bp 0x7619e5830950 sp 0x7619e5830948
READ of size 8 at 0x775a36006148 thread T20
#0 0x761a0a4d6b0c in nsCOMPtr<nsISerialEventTarget>::nsCOMPtr(nsCOMPtr<nsISerialEventTarget> const&) /firefox/xpcom/base/nsCOMPtr.h:379:62
#1 0x761a0a4d6b0c in mozilla::net::NeckoTargetHolder::GetNeckoTarget() netwerk/ipc/NeckoTargetHolder.cpp:13:43
#2 0x761a0a3f2e23 in mozilla::net::WebSocketChannelChild::MaybeReleaseIPCObject() netwerk/protocol/websocket/WebSocketChannelChild.cpp:108:39
#3 0x761a0a3f2cde in mozilla::net::WebSocketChannelChild::Release() netwerk/protocol/websocket/WebSocketChannelChild.cpp:37:5
#4 0x761a0a4e36ea in mozilla::RefPtrTraits<nsISupports>::Release(nsISupports*) dist/include/mozilla/RefPtr.h:48:40
#5 0x761a0a4e36ea in nsCOMPtr<nsISupports>::~nsCOMPtr() /firefox/xpcom/base/nsCOMPtr.h:340:7
#6 0x761a0a4e36ea in mozilla::net::ChannelEventQueue::ResumeInternal()::CompleteResumeRunnable::~CompleteResumeRunnable() netwerk/ipc/ChannelEventQueue.cpp:145:49
#8 0x761a0ab76ac4 in mozilla::Runnable::Release() xpcom/threads/nsThreadUtils.cpp:75:1
#11 0x761a15e053e6 in mozilla::dom::(anonymous namespace)::WorkerRunnableDispatcher::~WorkerRunnableDispatcher() dom/websocket/WebSocket.cpp:2793:7
#13 0x761a155d0544 in mozilla::dom::WorkerRunnable::Release() dom/workers/WorkerRunnable.cpp:94:1
#17 0x761a0ab8d949 in nsThread::ProcessNextEvent(bool, bool*) xpcom/threads/nsThread.cpp:1191:13
#19 0x761a155b163a in mozilla::dom::WorkerPrivate::DoRunLoop(JSContext*) dom/workers/WorkerPrivate.cpp:4173:7
#20 0x761a15571b13 in mozilla::dom::workerinternals::(anonymous namespace)::WorkerThreadPrimaryRunnable::Run() dom/workers/RuntimeService.cpp:2408:42
...
0x775a36006148 is located 264 bytes inside of 416-byte region [0x775a36006040,0x775a360061e0)
freed by thread T0 (WebCOOP+COEP Co) here:
#0 0x5ce83caaa916 in __interceptor_free
#1 0x761a0a4ccc75 in mozilla::net::NeckoChild::DeallocPWebSocketChild(mozilla::net::PWebSocketChild*) netwerk/ipc/NeckoChild.cpp:122:6
#2 0x761a0ae41068 in mozilla::ipc::ActorLifecycleProxy::~ActorLifecycleProxy() ipc/glue/ProtocolUtils.cpp:294:11
#3 0x761a152c3acc in mozilla::ipc::ActorLifecycleProxy::Release() dist/include/mozilla/ipc/ProtocolUtils.h:666:3
#7 0x761a152c3acc in mozilla::dom::PContentChild::OnMessageReceived(IPC::Message const&) ipc/ipdl/PContentChild.cpp:8211:5
#8 0x761a0ae1a715 in mozilla::ipc::MessageChannel::DispatchAsyncMessage(...) ipc/glue/MessageChannel.cpp:1797:25
... (main-thread event loop)
previously allocated by thread T0 (WebCOOP+COEP Co) here:
#0 0x5ce83caaabb4 in __interceptor_malloc
#3 0x761a0a8a9609 in already_AddRefed<mozilla::net::WebSocketChannelChild> mozilla::MakeAndAddRef<mozilla::net::WebSocketChannelChild, bool&>(bool&) dist/include/mozilla/RefPtr.h:608:15
#4 0x761a0a8a9609 in mozilla::net::WebSocketChannelConstructor(bool) netwerk/build/nsNetModule.cpp:79:12
#12 0x761a15dfbc9d in mozilla::dom::WebSocketImpl::InitializeConnection(nsIPrincipal*, nsICookieJarSettings*) dom/websocket/WebSocket.cpp:1923:15
#13 0x761a15e040d0 in mozilla::dom::(anonymous namespace)::ConnectRunnable::InitWithWindow(nsPIDOMWindowInner*) dom/websocket/WebSocket.cpp:1229:25
#14 0x761a155d3e00 in mozilla::dom::WorkerMainThreadRunnable::Run() dom/workers/WorkerRunnable.cpp:636:20
...
SUMMARY: AddressSanitizer: heap-use-after-free (/firefox/obj-firefox-asan/dist/bin/libxul.so+0x14683b0c)
| Reporter | ||
Comment 1•4 months ago
|
||
| Reporter | ||
Comment 2•4 months ago
|
||
| Reporter | ||
Comment 3•4 months ago
|
||
| Reporter | ||
Comment 4•4 months ago
|
||
| Reporter | ||
Comment 5•4 months ago
|
||
| Reporter | ||
Comment 6•4 months ago
|
||
| Reporter | ||
Comment 7•4 months ago
|
||
| Reporter | ||
Comment 8•4 months ago
|
||
This bug was filed automatically by an AI-powered security testing tool. The analysis, testcase, and any suggested patches were generated by an AI model and have NOT been verified by a human.
Please review all details carefully before triaging. The issue was confirmed by an independent verifier, but for non-ASAN issues, the verifier has been made less strict. Additionally, the root cause analysis and suggested fix may contain errors.
| Reporter | ||
Updated•4 months ago
|
Updated•3 months ago
|
Comment 9•3 months ago
|
||
The severity field for this bug is set to S3. However, the bug is flagged with the sec-high keyword.
:kershaw, could you consider increasing the severity of this security bug?
For more information, please visit BugBot documentation.
| Assignee | ||
Updated•3 months ago
|
| Assignee | ||
Comment 10•3 months ago
|
||
Use the same approach HttpChannelChild::Release() uses: dispatch off-main-thread
releases through the main thread with DecrementWithLimit<2>, so the count==1
SendDeleteSelf and count==0 delete only run on the main thread where the IPDL
reference is also released.
Updated•3 months ago
|
Comment 11•3 months ago
|
||
:kershaw, please update all release status flags to reflect the affected/unaffected/disabled state correctly. Which branches (beta, release, and/or ESR) are affected by this flaw?
Comment 12•3 months ago
|
||
Comment 13•3 months ago
|
||
| bugherder | ||
Updated•3 months ago
|
Comment 14•3 months ago
|
||
The patch landed in nightly and beta is affected, along with ESR.
:kershaw, is this bug important enough to require an uplift?
- If yes, please nominate the patch for beta and ESR approvals.
- See https://wiki.mozilla.org/Release_Management/Requesting_an_Uplift for documentation on how to request an uplift.
- If no, please set
status-firefox155and the ESR status flag(s) towontfix.
For more information, please visit BugBot documentation.
| Assignee | ||
Updated•3 months ago
|
Comment 15•3 months ago
|
||
firefox-beta Uplift Approval Request
- User impact if declined/Reason for urgency: Possible crash
- Code covered by automated testing?: yes
- Fix verified in Nightly?: yes
- Needs manual QE testing?: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: This patch reuses the same pattern as HttpChannelChild to release WebSocketChannelChild, so it should be safe.
- String changes made/needed?: N/A
- Is Android affected?: yes
| Assignee | ||
Comment 16•3 months ago
|
||
Use the same approach HttpChannelChild::Release() uses: dispatch off-main-thread
releases through the main thread with DecrementWithLimit<2>, so the count==1
SendDeleteSelf and count==0 delete only run on the main thread where the IPDL
reference is also released.
Original Revision: https://phabricator.services.mozilla.com/D319530
Comment 17•3 months ago
|
||
firefox-esr153 Uplift Approval Request
- User impact if declined/Reason for urgency: Possible crash
- Code covered by automated testing?: yes
- Fix verified in Nightly?: yes
- Needs manual QE testing?: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: This patch reuses the same pattern as HttpChannelChild to release WebSocketChannelChild, so it should be safe.
- String changes made/needed?: N/A
- Is Android affected?: yes
| Assignee | ||
Comment 18•3 months ago
|
||
Use the same approach HttpChannelChild::Release() uses: dispatch off-main-thread
releases through the main thread with DecrementWithLimit<2>, so the count==1
SendDeleteSelf and count==0 delete only run on the main thread where the IPDL
reference is also released.
Original Revision: https://phabricator.services.mozilla.com/D319530
Updated•3 months ago
|
Updated•3 months ago
|
Comment 20•3 months ago
|
||
| uplift | ||
Updated•3 months ago
|
Updated•3 months ago
|
Comment 21•3 months ago
|
||
| uplift | ||
Comment 22•3 months ago
|
||
firefox-esr140 Uplift Approval Request
- User impact if declined/Reason for urgency: Possible crash
- Code covered by automated testing?: yes
- Fix verified in Nightly?: yes
- Needs manual QE testing?: no
- Steps to reproduce for manual QE testing: N/A
- Risk associated with taking this patch: low
- Explanation of risk level: This patch reuses the same pattern as HttpChannelChild to release WebSocketChannelChild, so it should be safe.
- String changes made/needed?: N/A
- Is Android affected?: yes
| Assignee | ||
Comment 23•3 months ago
|
||
Use the same approach HttpChannelChild::Release() uses: dispatch off-main-thread
releases through the main thread with DecrementWithLimit<2>, so the count==1
SendDeleteSelf and count==0 delete only run on the main thread where the IPDL
reference is also released.
Original Revision: https://phabricator.services.mozilla.com/D319530
| Assignee | ||
Comment 24•3 months ago
|
||
ESR115 uplift of D319530, hand-rebased onto the older Release()/
MaybeReleaseIPCObject() shape.
Updated•3 months ago
|
Comment 25•3 months ago
|
||
firefox-esr115 Uplift Approval Request
- User impact if declined/Reason for urgency: Possible crash
- Code covered by automated testing?: yes
- Fix verified in Nightly?: yes
- Needs manual QE testing?: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: This patch reuses the same pattern as HttpChannelChild to release WebSocketChannelChild, so it should be safe.
- String changes made/needed?: N/A
- Is Android affected?: yes
Updated•3 months ago
|
Updated•3 months ago
|
Updated•3 months ago
|
Comment 26•3 months ago
|
||
| uplift | ||
Updated•3 months ago
|
Comment 27•3 months ago
|
||
| uplift | ||
Updated•3 months ago
|
Updated•3 months ago
|
Updated•3 months ago
|
Updated•23 days ago
|
Description
•