Closed Bug 590771 (CVE-2010-3766) Opened 14 years ago Closed 14 years ago

nsDOMAttribute MutationObserver Remote Code Execution Vulnerability (ZDI-CAN-898)

Categories

(Core :: DOM: Core & HTML, defect)

defect
Not set
critical

Tracking

()

RESOLVED FIXED
Tracking Status
blocking2.0 --- betaN+
blocking1.9.2 --- .13+
status1.9.2 --- .13-fixed
blocking1.9.1 --- .16+
status1.9.1 --- .16-fixed

People

(Reporter: reed, Assigned: mounir)

References

Details

(4 keywords, Whiteboard: [sg:critical?])

Attachments

(5 files)

Attached file PoC
ZDI-CAN-898: Mozilla Firefox nsDOMAttribute MutationObserver Remote Code Execution Vulnerability

-- CVSS ----------------------------------------------------------------
10, (AV:N/AC:L/Au:N/C:C/I:C/A:C)

-- ABSTRACT ------------------------------------------------------------

TippingPoint has identified a vulnerability affecting the following 
products:

    Mozilla Firefox 3.6.x

-- VULNERABILITY DETAILS -----------------------------------------------

This vulnerability allows remote attackers to execute arbitrary code on
vulnerable installations of Mozilla Firefox. User interaction is
required to exploit this vulnerability in that the target must visit a
malicious page or open a malicious file.

The specific flaw exists within the application's support of an API used
for element traversal. Due to a particular element not implementing
functionality required by the API, a use-after free vulnerability can be
made to occur. This can be used to achieve code execution under the
context of the application.

The issue occurs within the application's support of the NodeIterator
API that is used for DOM Traversal as outlined in the DOM specification
on w3.org. 

In order to support the node iteration, the application needs to keep
track of modifications to the dom tree in order to update the state of
the iterator. This is done explicitly by a pair of methods called
AddMutationObserver/RemoveMutationObserver. Anytime an element is
created, the observer list will be updated so that a node iterator can
adjust for modified elements.

When adding an element, it will need to update the MutationObserver list
as in the following code.

content/base/src/nsNodeIterator.cpp:185
nsNodeIterator::nsNodeIterator(nsINode *aRoot,
                               PRUint32 aWhatToShow,
                               nsIDOMNodeFilter *aFilter,
                               PRBool aExpandEntityReferences) :
    nsTraversal(aRoot, aWhatToShow, aFilter, aExpandEntityReferences),
    mDetached(PR_FALSE),
    mPointer(mRoot, PR_TRUE)
{
    aRoot->AddMutationObserver(this);         // XXX
}

nsNodeIterator::~nsNodeIterator()
{
    /* destructor code */
    if (!mDetached && mRoot)
        mRoot->RemoveMutationObserver(this);    // XXX
}

AddMutationObserver/RemoveMutationObserver will add the provided element
to a page-global list that can be queried by the NodeIterator.

content/base/src/nsGenericElement.cpp:295
void
nsINode::AddMutationObserver(nsIMutationObserver* aMutationObserver)
{
  nsSlots* slots = GetSlots();
  if (slots) {
   
slots->mMutationObservers.AppendElementUnlessExists(aMutationObserver);
  }
}

void
nsINode::RemoveMutationObserver(nsIMutationObserver* aMutationObserver)
{
  nsSlots* slots = GetExistingSlots();
  if (slots) {
    slots->mMutationObservers.RemoveElement(aMutationObserver);
  }
}

One element however does not update the mutation list. This is the
nsDOMAttribute and is defined in content/base/src/nsDOMAttribute.cpp.
This will allow someone to manipulate the nsDOMAttribute node without
the nsNodeIterator api being informed. This will bypass the type-safety
of the compiler and can lead to code execution.

Version(s)  tested: Mozilla Firefox
Platform(s) tested: Windows XP SP3

-- CREDIT --------------------------------------------------------------

This vulnerability was discovered by:
    * regenrecht
I get a js error on trunk:
Error: uncaught exception: [Exception... "Component returned failure code: 0x80004003 (NS_ERROR_INVALID_POINTER) [nsIDOMAttr.removeChild]"  nsresult: "0x80004003 (NS_ERROR_INVALID_POINTER)"  location: "JS frame :: https://bug590771.bugzilla.mozilla.org/attachment.cgi?id=469260&t=BcPe7a6cgh :: run :: line 11"  data: no]

On Firefox3.6.8, I get an alert saying "[object Text]".
Mounir will investigate.
Assignee: nobody → mounir.lamouri
I believe this has been fixed on trunk with bug 540848 with the following changesets:
http://hg.mozilla.org/mozilla-central/rev/068150b06d11
http://hg.mozilla.org/mozilla-central/rev/13571a9165ea
Status: NEW → ASSIGNED
Forget comment 3, this is not related actually.

Very likely, this is fixed on trunk because of a regression of bug 566416. I filed bug 598877 about the regression. Quickly, attributes.firstChild returns null in the test case with the current trunk which is wrong and make the script fail too early.
This bug expected, this is not fixed on trunk apparently.
Attachment #478909 - Flags: review?(jonas)
Attachment #478910 - Flags: review?(jonas)
Attachment #478910 - Flags: approval1.9.2.11?
Comment on attachment 478910 [details] [diff] [review]
Patch for 1.9.1 and 1.9.2 [checked-in]

Please get reviews before asking for branch approval
Attachment #478910 - Flags: approval1.9.2.11?
Does this affect 1.9.1? If not, do we know what bug introduced the flaw?
blocking1.9.1: --- → ?
blocking1.9.2: --- → needed
status1.9.1: --- → ?
(In reply to comment #8)
> Does this affect 1.9.1? If not, do we know what bug introduced the flaw?

I'm building 1.9.1 to check that.
Asking for 2.0 blocking considering this is affecting the trunk too.
blocking2.0: --- → ?
status2.0: --- → ?
(In reply to comment #8)
> Does this affect 1.9.1? If not, do we know what bug introduced the flaw?

1.9.1 is also affected. The same patch applies and work.
Attachment #478909 - Attachment description: Tests for 1.9.2 → Tests for 1.9.1 and 1.9.2
Attachment #478910 - Attachment description: Patch for 1.9.2 → Patch for 1.9.1 and 1.9.2
Do we need a different trunk patch after the regression is fixed?
blocking2.0: ? → betaN+
(In reply to comment #11)
> Do we need a different trunk patch after the regression is fixed?

Yes. The code has changed so the current patch will not fix the issue on trunk.
Comment on attachment 478910 [details] [diff] [review]
Patch for 1.9.1 and 1.9.2 [checked-in]

Add a comment to the declaration of AttributeChildRemoved that says that attributes never have more than 1 children and so this is always that one single child that is removed.

>diff --git a/content/base/src/nsNodeIterator.cpp b/content/base/src/nsNodeIterator.cpp
>--- a/content/base/src/nsNodeIterator.cpp
>+++ b/content/base/src/nsNodeIterator.cpp
>@@ -212,17 +212,17 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END
> NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsNodeIterator)
>   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRoot)
>   NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mFilter)
> NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
> 
> // QueryInterface implementation for nsNodeIterator
> NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsNodeIterator)
>     NS_INTERFACE_MAP_ENTRY(nsIDOMNodeIterator)
>-    NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
>+    NS_INTERFACE_MAP_ENTRY(nsIMutationObserver2)
>     NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMNodeIterator)
>     NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(NodeIterator)
> NS_INTERFACE_MAP_END

You need to keep both interfaces in the interface map. We still want QIing to nsIMutationObserver to work.


>+nsNodeUtils::AttributeChildRemoved(nsINode* aAttribute,
>+                                   nsIContent* aChild)
>+{
>+  NS_PRECONDITION(aAttribute->IsNodeOfType(nsINode::eATTRIBUTE),
>+                  "container must be a nsIAttribute");
>+
>+  // This is a variant of IMPL_MUTATION_NOTIFICATION.

Add a comment to IMPL_MUTATION_NOTIFICATION so that if someone modifies that, this code is modified too.

r=me with those changes.
Attachment #478910 - Flags: review?(jonas) → review+
Shouldn't this be blocking 1.9.1 too?
blocking1.9.1: ? → needed
Attachment #478909 - Flags: approval1.9.2.11?
Attachment #478909 - Flags: approval1.9.1.14?
Attachment #478910 - Flags: approval1.9.2.11?
Attachment #478910 - Flags: approval1.9.1.14?
Attachment #478909 - Flags: approval1.9.2.11?
Attachment #478909 - Flags: approval1.9.1.14?
Attachment #478910 - Flags: approval1.9.2.11?
Attachment #478910 - Flags: approval1.9.1.14?
Removing approval requests given that I've been told on IRC that this shouldn't land for this release.
(In reply to comment #15)
> Removing approval requests given that I've been told on IRC that this shouldn't
> land for this release.

1.9.1 and 1.9.2 branches.
Attached patch Tests for trunkSplinter Review
Attachment #479331 - Flags: review?(jonas)
Attachment #479332 - Flags: review?(jonas)
I actually found a few regressions from bug 566416 and my patch for trunk is built on top of them, so I'm marking them as dependencies.
Depends on: 598877, 600466, 600468, 600471
blocking1.9.1: needed → ?
blocking1.9.2: needed → ?
Comment on attachment 479332 [details] [diff] [review]
Patch for trunk [checked-in]

Patch pushed on mozilla-central:
http://hg.mozilla.org/mozilla-central/rev/56403a9ae8b6
Attachment #479332 - Attachment description: Patch for trunk → Patch for trunk [checked-in]
Should this be marked FIXED?

If not, what remains to do?
Patches should be pushed on branches (according to branch rules, I have to wait a few days before asking for an approval and it's not really urgent). In addition, I think that tests will have to be pushed after branch releases.
blocking1.9.1: ? → .15+
blocking1.9.2: ? → .12+
Attachment #478910 - Attachment description: Patch for 1.9.1 and 1.9.2 → Patch for 1.9.1 and 1.9.2 [checked-in]
Fixed on trunk == RESOLVED FIXED. Branches are tracked separately.
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
(In reply to comment #22)
> Patches should be pushed on branches (according to branch rules, I have to wait
> a few days before asking for an approval and it's not really urgent). In
> addition, I think that tests will have to be pushed after branch releases.

(In reply to comment #23)
> Pushed in 1.9.1 and 1.9.2:
> http://hg.mozilla.org/releases/mozilla-1.9.1/rev/bcfb355e5c2b
> http://hg.mozilla.org/releases/mozilla-1.9.2/rev/dc30f8b144a9

What happened to the "asking for an approval" thing? *all* branch patches require approval before landing.

> I will push all tests after 1.9.1 and 1.9.2 branching.

No, wait until after the release has been made before landing tests.
Attachment #478910 - Flags: approval1.9.2.13?
Attachment #478910 - Flags: approval1.9.1.16?
> (In reply to comment #23)
> > Pushed in 1.9.1 and 1.9.2:
> > http://hg.mozilla.org/releases/mozilla-1.9.1/rev/bcfb355e5c2b
> > http://hg.mozilla.org/releases/mozilla-1.9.2/rev/dc30f8b144a9
> 
> What happened to the "asking for an approval" thing? *all* branch patches
> require approval before landing.

It has been marked as 'blocking'. Isn't that enough?

> > I will push all tests after 1.9.1 and 1.9.2 branching.
> 
> No, wait until after the release has been made before landing tests.

That's what I meant.
No, the approval policy for released branches is different than the one for under-development branches. For released branches all patches require approval, regardless of blocking status.
Note that eventually Firefox 4 will end up in that state -- think of the branches as perpetual "Release Candidate" stage. Rules for each tree are always on their tinderboxen, and they differ. Please check.
Comment on attachment 478910 [details] [diff] [review]
Patch for 1.9.1 and 1.9.2 [checked-in]

retroactive approval for 1.9.2.13 and 1.9.1.16, a=dveditz for release-drivers
Attachment #478910 - Flags: approval1.9.2.13?
Attachment #478910 - Flags: approval1.9.2.13+
Attachment #478910 - Flags: approval1.9.1.16?
Attachment #478910 - Flags: approval1.9.1.16+
Alias: ZDI-CAN-898
Alias: ZDI-CAN-898
Alias: CVE-2010-3766
Verified fixed for 1.9.2 with Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13pre) Gecko/20101117 Namoroka/3.6.13pre and for 1.9.1 with Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.16pre) Gecko/20101117 Shiretoko/3.5.16pre using PoC.
Flags: in-testsuite+
status2.0: ? → ---
Group: core-security
Depends on: CVE-2011-3659
Component: DOM: Traversal-Range → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: