Closed Bug 2054351 Opened 4 months ago Closed 4 months ago

Flat-selection slot-parent confusion causes a heap-buffer-overflow read

Categories

(Core :: DOM: Selection, defect)

defect

Tracking

()

RESOLVED FIXED
155 Branch
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)

Attached file repro.html —

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."
Flags: sec-bounty?
Group: firefox-core-security → dom-core-security
Component: Security → DOM: Selection
Keywords: testcase
Product: Firefox → Core

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.

Status: UNCONFIRMED → NEW
Ever confirmed: true
Keywords: sec-high

Hi Masayuki, can you please take a look at this? Thank you.

Flags: needinfo?(masayuki)

Oops, sorry, I couldn't catch the needinfo. I'll take a look.

Assignee: nobody → masayuki
Status: NEW → ASSIGNED
Flags: needinfo?(masayuki)
Attached file asan-log.txt —

Full log as requested.

Flags: needinfo?(twsmith)

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.

Err, maybe wrong. The containers of spellchecker range boundaries should be Text, so, they are never shadow host.

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

Severity: -- → S2
Keywords: regression
Regressed by: 2014342
OS: Unspecified → All
Hardware: Unspecified → All
Regressed by: 2014622
No longer regressed by: 2014342
Regressed by: 2012637
No longer regressed by: 2014622

It's caused by changing GetAssignedSlot() to HTMLSlotElement::FromNode() of this patch.

Attached file (secure) —

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.

Set release status flags based on info from the regressing bug 2012637

Pushed by masayuki@d-toybox.com: https://github.com/mozilla-firefox/firefox/commit/555e545b7e1e https://hg.mozilla.org/integration/autoland/rev/a9a8d4ec8dab Make `Selection::StyledRanges::ReorderRangesIfNecessary` compare range boundaries in the flat tree r=jjaschke
Attached file (secure) —

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

Attachment #9615080 - Flags: approval-mozilla-beta?

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 Selection to 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
Group: dom-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 4 months ago
Resolution: --- → FIXED
Target Milestone: --- → 155 Branch

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.

Attachment #9615080 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
QA Whiteboard: [sec] [uplift] [qa-triage-done-c155/b154]
Flags: sec-bounty? → sec-bounty+
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: