Heap-use-after-free in StyleSheet::RuleAdded via adoptedStyleSheets proxy
A heap-use-after-free occurs when the adoptedStyleSheets proxy is used after its backing ShadowRoot has been freed by the cycle collector. An attacker could trigger a crash and potentially control the freed memory via heap spraying.
Originally disclosed on Mozilla
- Fixed in 4 days
- Paid in 11 days
- Disclosed after 4 days
Created attachment 9584813
PoC.html
== Summary ==
A heap-use-after-free exists when the adoptedStyleSheets ObservableArray proxy is used after its backing ShadowRoot has been freed by the cycle collector. The proxy retains a raw void* to the freed ShadowRoot in its reserved slot (OBSERVABLE_ARRAY_DOM_INTERFACE_SLOT). Using proxy.push() calls OnSetAdoptedStyleSheets on the freed object, which stores a dangling DocumentOrShadowRoot* in StyleSheet::mAdopters. A subsequent sheet modification (insertRule/replaceSync) triggers the NOTIFY macro, which iterates mAdopters and dereferences the freed ShadowRoot pointer.
A minimal PoC (poc_adopted_uaf_minimal.html) is attached to this report.
== Steps to Reproduce ==
1. Open the attached PoC (poc_adopted_uaf_minimal.html) in an ASan-enabled Firefox Nightly build
2. Wait ~60 seconds for cycle collection pressure to trigger
Or run from CLI:
ASAN_OPTIONS="detect_leaks=0:halt_on_error=0:print_stacktrace=1:quarantine_size_mb=256" \
MOZ_DISABLE_CONTENT_SANDBOX=1 \
./firefox --headless --no-remote --profile /tmp/poc_profile \
poc_adopted_uaf_minimal.html
== Actual Results ==
ASan reports heap-use-after-free:
==PID==ERROR: AddressSanitizer: heap-use-after-free on address 0x...
READ of size 8 at 0x... thread T0 (file:// Content)
#0 AsNode DocumentOrShadowRoot.h:66
#1 mozilla::StyleSheet::RuleAdded StyleSheet.cpp:739
#2 InsertRuleInternal StyleSheet.cpp:1427
#3 mozilla::StyleSheet::InsertRule StyleSheet.cpp:548
#4 CSSStyleSheet_Binding::insertRule CSSStyleSheetBinding.cpp:718
freed by:
#0 free
#1 nsIContent::Destroy() FragmentOrElement.cpp:131
#2 SnowWhiteKiller::Visit nsCycleCollector.cpp:2702
previously allocated by:
#0 malloc
#1 nsNodeInfoManager::Allocate nsNodeInfoManager.cpp:283
#2 Element::AttachShadowWithoutNameChecks Element.cpp:1537
== Expected Results ==
No crash. The proxy should detect that the ShadowRoot has been freed and refuse the operation.
== Root Cause ==
1. The ObservableArray proxy for adoptedStyleSheets stores a raw void* to DocumentOrShadowRoot in OBSERVABLE_ARRAY_DOM_INTERFACE_SLOT.
2. The proxy's slot is only invalidated during GC finalize, leaving it stale when the backing ShadowRoot is reclaimed via cycle collection (CC unlink) instead.
3. StyleSheet::mAdopters is nsTArray<DocumentOrShadowRoot*> — raw pointers, not RefPtr. AddAdopter() appends without preventing the object from being freed.
Attack chain:
(a) proxy = sr.adoptedStyleSheets → proxy holds void* to ShadowRoot
(b) host.remove(); sr = null → ShadowRoot becomes garbage
(c) CC runs → SnowWhiteKiller frees the ShadowRoot
(d) proxy.push(sheet) → OnSetAdoptedStyleSheets on freed memory
→ AddAdopter stores dangling ptr in mAdopters
(e) sheet.insertRule(...) → RuleAdded → NOTIFY macro
→ iterates mAdopters
→ adopter->AsNode() dereferences freed memory → UAF
== Security Impact ==
- Web-accessible from content process (no special permissions required)
- The freed ShadowRoot is a 264-byte heap allocation
- The UAF dereferences a vtable-like pointer (AsNode), which could be controlled via heap spraying
- Deterministic trigger with sufficient GC/CC pressure
== Tested Version ==
Firefox Nightly 152.0a1 (ASan build)
BuildID: 9b901a2393dbc3f35c75b3f7fa4e2a7529df7367
== Attachments ==
1. PoC.html
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
The ObservableArray proxy stores a raw pointer to the ShadowRoot that is not invalidated when the ShadowRoot is freed by cycle collection, and StyleSheet::mAdopters holds raw pointers without keeping the object alive.
Impact
The use-after-free dereferences a vtable-like pointer, which could be controlled via heap spraying, leading to a crash and potentially code execution.
What the attacker needed
Victim visits a malicious page; ASan build for detection
The fix
The proxy should detect that the ShadowRoot has been freed and refuse the operation.
How it was found
- Open the PoC in an ASan-enabled Firefox Nightly build.
- Wait about 60 seconds for cycle collection pressure to trigger.
- Observe the ASan heap-use-after-free report.
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 web page can keep a reference to a style-sheet list after the element that owns it has been removed and cleaned up by the browser. Using that stale reference later makes the browser read freed memory, which can crash it or let an attacker take control.
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.