Flat-selection slot-parent confusion causes a heap-buffer-overflow read
Categories
(Core :: DOM: Selection, defect)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox-esr115 | --- | unaffected |
| firefox-esr140 | --- | unaffected |
| firefox-esr153 | --- | unaffected |
| firefox153 | --- | unaffected |
| firefox154 | + | fixed |
| firefox155 | + | fixed |
People
(Reporter: deayzl, Assigned: masayuki)
References
(Regression)
Details
(4 keywords, Whiteboard: [client-bounty-form])
Attachments
(2 files, 2 external files)
Root Cause Analysis
nsContentUtils::CompareChildNodes<TreeKind::FlatForSelection> obtains the
assigned slots for two child references:
// dom/base/nsContentUtils.cpp:3723
HTMLSlotElement* const slot1 = aChild1->GetAssignedSlot<aKind>();
HTMLSlotElement* const slot2 = aChild2->GetAssignedSlot<aKind>();
if (slot1 || slot2) {
if (slot1 == slot2) {
const auto* slot = HTMLSlotElement::FromNode(aParent);
MOZ_ASSERT(slot);
for (nsINode* node : slot->AssignedNodes()) {
// ...
}
}
}
The outer condition plus slot1 == slot2 proves that slot1 and slot2 are
the same non-null HTMLSlotElement. It does not prove that aParent is that
slot.
The same-container fast path in ComparePoints can call
CompareClosestCommonAncestorChildren with a range-boundary container as
aParent. After range mutation, that container can be the ordinary shadow host
while aChild1 and aChild2 are light-DOM children assigned to the same slot.
An invariant trap used only during reduction repeatedly recorded the condition
as parent=host, with two distinct assigned child IDs.
HTMLSlotElement::FromNode(aParent) is therefore allowed to fail its tag
check. The only guard is MOZ_ASSERT(slot), which is absent in this optimized
ASan build. The following member access treats storage at the slot-only
mAssignedNodes offset as a FastFrontRemovableArray. In the recorded run,
that offset is immediately beyond the smaller ordinary HTMLElement object,
so nsTArray::Hdr() performs the reported 8-byte OOB read.
Reproduction Steps
tested firefox commit: 0ae9827c4d7bc8b28ccbfa58324ded73b68dccf6
Build firefox with:
mk_add_options MOZ_OBJDIR=/home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser
ac_add_options --enable-project=browser
ac_add_options --enable-address-sanitizer
ac_add_options --disable-install-strip
ac_add_options --disable-jemalloc
ac_add_options --disable-crashreporter
ac_add_options --disable-profiling
# Keep stack use reasonable while retaining useful line information.
ac_add_options --enable-optimize="-O1"
ac_add_options --enable-debug-symbols=-gline-tables-only
# Optional but useful for GC-sensitive SpiderMonkey reproduction work.
ac_add_options --enable-gczeal
Listen and serve the repro.html:
python3 -m http.server 8000 --bind 127.0.0.1
Execute:
firefox http://localhost:8000/repro.html
Result:
console.error: (new TypeError("can't access property \"find\", this._searchProviderInfo is null", "moz-src:///browser/components/search/SearchSERPTelemetry.sys.mjs", 1209))
=================================================================
==4091453==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7346ac00ce40 at pc 0x727695137cd0 bp 0x7ffeaf003950 sp 0x7ffeaf003948
READ of size 8 at 0x7346ac00ce40 thread T0 (Isolated Web Co)
/home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/bin/llvm-symbolizer: error: '[anon:js-executable-memory]': No such file or directory
#0 0x727695137ccf in Hdr /home/slave/firefox-bug-bounty/firefox/xpcom/ds/nsTArray.h:501:51
#1 0x727695137ccf in Elements /home/slave/firefox-bug-bounty/firefox/xpcom/ds/nsTArray.h:1048:48
#2 0x727695137ccf in Span<AutoTArray<RefPtr<nsINode>, 0UL>, void> /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/Span.h:593:19
#3 0x727695137ccf in AsSpan /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/dom/FastFrontRemovableArray.h:27:12
#4 0x727695137ccf in operator Span /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/dom/FastFrontRemovableArray.h:23:43
#5 0x727695137ccf in AssignedNodes /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dist/include/mozilla/dom/HTMLSlotElement.h:53:62
#6 0x727695137ccf in mozilla::Maybe<int> nsContentUtils::CompareChildNodes<(TreeKind)3, void>(nsINode const&, nsIContent const*, nsIContent const*, nsContentUtils::ResizableNodeIndexCache<100ul>*) /home/slave/firefox-bug-bounty/firefox/dom/base/nsContentUtils.cpp:3735:36
...
mozilla::dom::ScriptLoader::ProcessInlineScript(nsIScriptElement*, JS::loader::ScriptKind, nsTSubstring<char16_t> const&) /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dom/script/./../../../firefox/dom/script/ScriptLoader.cpp:1996:10
#32 0x72769c7ce54f in mozilla::dom::ScriptLoader::ProcessScriptElement(nsIScriptElement*, nsTSubstring<char16_t> const&) /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dom/script/./../../../firefox/dom/script/ScriptLoader.cpp:1459:10
#33 0x72769c7cd3ff in mozilla::dom::ScriptElement::MaybeProcessScript(nsTSubstring<char16_t> const&) /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dom/script/./../../../firefox/dom/script/ScriptElement.cpp:311:18
#34 0x72769c7cc961 in mozilla::dom::ScriptElement::MaybeProcessScript(nsCOMPtr<nsIParser>) /home/slave/firefox-bug-bounty/obj-x86_64-pc-linux-gnu-asan-browser/dom/script/./../../../firefox/dom/script/ScriptElement.cpp:182:18
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/slave/firefox-bug-bounty/firefox/xpcom/ds/nsTArray.h:501:51 in Hdr
Shadow bytes around the buggy address:
0x7346ac00cb80: 00 00 00 00 00 00 00 00 00 00 fa fa fa fa fa fa
0x7346ac00cc00: fa fa fd fd fd fd fd fd fd fd fd fd fd fd fd fd
0x7346ac00cc80: fd fd fd fd fa fa fa fa fa fa fa fa 00 00 00 00
0x7346ac00cd00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fa fa
0x7346ac00cd80: fa fa fa fa fa fa 00 00 00 00 00 00 00 00 00 00
=>0x7346ac00ce00: 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa fa fa
0x7346ac00ce80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x7346ac00cf00: 00 00 fa fa fa fa fa fa fa fa 00 00 00 00 00 00
0x7346ac00cf80: 00 00 00 00 00 00 00 00 00 00 00 00 fa fa fa fa
0x7346ac00d000: fa fa fa fa 00 00 00 00 00 00 00 00 00 00 00 00
0x7346ac00d080: 00 00 00 00 00 00 fa fa fa fa fa fa fa fa fd fd
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==4091453==ABORTING
[Parent 4091255, IPC I/O Parent] WARNING: process 4091453 exited with status 1: file firefox/ipc/chromium/src/chrome/common/process_watcher_posix_sigchld.cc:155
console.error: AboutHomeStartupCache: "A privileged about content process shut down while cache streams were still en route."
Updated•4 months ago
|
Comment 1•4 months ago
|
||
The rating assigned by our bug rating tool (currently under development) is:
Rating: sec-high
Explanation: Valid memory-safety bug (type confusion -> heap OOB read). In CompareChildNodes<FlatForSelection>, when two light-DOM children are assigned to the same <slot>, slot1==slot2 is non-null but aParent can be the shadow HOST rather than the slot (the code's own comments at lines ~4398-4408/4465-4475 acknowledge GetParentNodeForComparison returns the host for FlatForSelection, pending bug 2014622). HTMLSlotElement::FromNode(aParent) is a checked cast that returns nullptr for a non-slot host; the only guard is MOZ_ASSERT(slot), compiled out in opt/release. The compiler folds the checked-cast+deref into an unchecked cast of aParent, so slot->AssignedNodes() reads mAssignedNodes past the smaller HTMLElement object -> confirmed 8-byte heap-buffer-overflow read, then iterates a garbage array pointer/length. Reachable from unprivileged web content (shadow DOM slots + Range/Selection via inline script) with no unusual interaction, no DevTools, deterministic. Type confusion / memory corruption reachable via normal browsing => sec-high.
NOTE: I was able to reproduce the issue locally using the attached testcase.
Comment 2•4 months ago
|
||
Hi Masayuki, can you please take a look at this? Thank you.
| Assignee | ||
Comment 3•4 months ago
|
||
Oops, sorry, I couldn't catch the needinfo. I'll take a look.
| Assignee | ||
Comment 4•4 months ago
|
||
Hmm, I cannot reproduce this with normal debug build on Windows even the assertion failure.
aParent comes from:
- https://searchfox.org/firefox-main/rev/9631bc624c432f094ca05c276e5a5c922407859f/dom/base/nsContentUtils.cpp#4412
- https://searchfox.org/firefox-main/rev/9631bc624c432f094ca05c276e5a5c922407859f/dom/base/nsContentUtils.cpp#4479
- https://searchfox.org/firefox-main/rev/9631bc624c432f094ca05c276e5a5c922407859f/dom/base/nsContentUtils.cpp#4137
- https://searchfox.org/firefox-main/rev/9631bc624c432f094ca05c276e5a5c922407859f/dom/base/nsContentUtils.cpp#4383
- https://searchfox.org/firefox-main/rev/9631bc624c432f094ca05c276e5a5c922407859f/dom/base/nsContentUtils.cpp#4350
I guess the last one is the caller of nsContentUtils::CompareChildNodes in the crash scenario.
Tyson, could you paste the stack more? I'd like to know where considers aParent and what one calls nsContentUtils::ComparePoints.
| Assignee | ||
Comment 6•4 months ago
|
||
Thank you. It seems that the Selection might be the spellchecker selection and the slowness of the asan build may be required to run the spellchecker.
| Assignee | ||
Comment 7•4 months ago
|
||
Err, maybe wrong. The containers of spellchecker range boundaries should be Text, so, they are never shadow host.
| Assignee | ||
Comment 8•4 months ago
|
||
Okay, I wrote a patch. However, I've not succeeded to write the minimized test. It's really hard to get the last moment within asan build because there are a lot of related things but it's really slow and I'm busy on some bugs...
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Comment 9•4 months ago
|
||
It's caused by changing GetAssignedSlot() to HTMLSlotElement::FromNode() of this patch.
| Assignee | ||
Comment 10•4 months ago
|
||
When Selection compares range boundaries, it always uses
TreeKind::FlatForSelection. Therefore, this drops the template
parameters of CompareToRangeStart and CompareToRangeEnd.
Then, Selection::StyledRanges::ReorderRangesIfNecessary calls
nsContentUtils::ComparePoints with range boundaries in
TreeKind::DOM. So, this mismatch causes the crash with unexpected
situation.
However, this patch does not strict nsContentUtils::ComparePoints
because it requires a big patch. Therefore, this adds error handling
and assertions into the related methods.
In a follow up, I think we should make RangeBoundaryBase split for
TreeKind::DOM and TreeKind::FlatForSelection. Then, we can prevent
misuse of wrong tree kind boundaries.
Comment 11•4 months ago
|
||
Set release status flags based on info from the regressing bug 2012637
Comment 12•4 months ago
|
||
| Assignee | ||
Comment 13•4 months ago
|
||
When Selection compares range boundaries, it always uses
TreeKind::FlatForSelection. Therefore, this drops the template
parameters of CompareToRangeStart and CompareToRangeEnd.
Then, Selection::StyledRanges::ReorderRangesIfNecessary calls
nsContentUtils::ComparePoints with range boundaries in
TreeKind::DOM. So, this mismatch causes the crash with unexpected
situation.
However, this patch does not strict nsContentUtils::ComparePoints
because it requires a big patch. Therefore, this adds error handling
and assertions into the related methods.
In a follow up, I think we should make RangeBoundaryBase split for
TreeKind::DOM and TreeKind::FlatForSelection. Then, we can prevent
misuse of wrong tree kind boundaries.
Original Revision: https://phabricator.services.mozilla.com/D313392
Updated•4 months ago
|
Comment 14•4 months ago
|
||
firefox-beta Uplift Approval Request
- User impact if declined/Reason for urgency: This is a UAF crash.
- Code covered by automated testing?: no
- 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 fixes the bad root caller in
Selectionto use right range boundaries to compare. I think it's simple change even though the changed lines are not a few.
And also adds a check into the root caller in nsContentUtils side too. That will return "disconnected" in the case to avoid the same crash with maybe remaining bad callers. This is completely a simple change.
- String changes made/needed?: no
- Is Android affected?: yes
Updated•4 months ago
|
Comment 15•4 months ago
|
||
Comment 16•4 months ago
|
||
This is an automated comment. To help with triage and priority, we are experimenting with an automated exploitation analysis.
A proof-of-concept was attempted, but controllable exploitation could not be demonstrated. This is not proof that it is unexploitable; the result is inconclusive, but it does not seem straightforward. We attempted a type confusion, triggered in a web content process. The confusion was reproduced deterministically, after which size and offset control and heap grooming were attempted; the misread stayed pinned to a fixed-size read at a fixed offset past a fixed-geometry allocation with no downstream write path, and the instrumented allocator halts at that read, so how it would continue over production heap adjacency could not be observed.
If this analysis is demonstrably incorrect, please needinfo :tjr.
Updated•4 months ago
|
Updated•4 months ago
|
Comment 17•4 months ago
|
||
| uplift | ||
Updated•4 months ago
|
Updated•4 months ago
|
Updated•1 month ago
|
Description
•