Bugzilla@Mozilla – Bug 572985
Plugin Parameter EnsureCachedAttrParamArrays Remote Code Execution Vulnerability (ZDI-CAN-821)
Last modified: 2010-08-10 11:59:08 PDT
Summon comment box
Created attachment 452201 [details] PoC ZDI-CAN-821: Mozilla Firefox Plugin Parameter EnsureCachedAttrParamArrays Remote Code Execution Vulnerability -- 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 browser's method for parsing child elements out of a particular tag. The application will use a 32-bit index to enumerate them, but will store it in a 16-bit signed integer and then use it to allocate space for a cache. When populating the cache a buffer overflow will occur. This can lead to code execution under the context of the application. The issue occurs within the application's support for parameters within plugins. The application will create a cache for each parameter value and name. This is done by enumerating the total number of parameters and storing it. When calculating the size needed for the cache, the application will store the total number of results into a signed integer. This will cause the arithmetic required to undercalculate the size for the allocation. Later when this cache is populated the buffer overflow will occur. After parsing the elements required for a plugin, the application will enter the following code which will count the number of elements for the cached Attribute/Param array. layout/generic/nsObjectFrame.cpp:2873 nsresult nsPluginInstanceOwner::EnsureCachedAttrParamArrays() { if (mCachedAttrParamValues) return NS_OK; NS_PRECONDITION(((mNumCachedAttrs + mNumCachedParams) == 0) && !mCachedAttrParamNames, "re-cache of attrs/params not implemented! use the DOM " "node directy instead"); NS_ENSURE_TRUE(mOwner, NS_ERROR_NULL_POINTER); // first, we need to find out how much we need to allocate for our // arrays count up attributes mNumCachedAttrs = 0; PRUint32 cattrs = mContent->GetAttrCount(); if (cattrs < 0x0000FFFF) { // unsigned 32 bits to unsigned 16 bits conversion mNumCachedAttrs = static_cast<PRUint16>(cattrs); } else { mNumCachedAttrs = 0xFFFE; // minus one in case we add an extra "src" entry below } // now, we need to find all the PARAM tags that are children of us // however, be carefull NOT to include any PARAMs that don't have us // as a direct parent. For nested object (or applet) tags, be sure // to only round up the param tags that coorespond with THIS // instance. And also, weed out any bogus tags that may get in the // way, see bug 39609. Then, with any param tag that meet our // qualification, temporarly cache them in an nsCOMArray until // we can figure out what size to make our fixed char* array. mNumCachedParams = 0; After counting the number of elements, this will be assigned to a 16-bit integer and then used in an allocation. layout/generic/nsObjectFrame.cpp:2938 if (allParams) { PRUint32 numAllParams; allParams->GetLength(&numAllParams); // loop through every so called dependent PARAM tag to check if it // "belongs" to us for (PRUint32 i = 0; i < numAllParams; i++) { nsCOMPtr<nsIDOMNode> pnode; allParams->Item(i, getter_AddRefs(pnode)); nsCOMPtr<nsIDOMElement> domelement = do_QueryInterface(pnode); if (domelement) { ... nsCOMPtr<nsIDOMNode> mydomNode = do_QueryInterface(mydomElement); if (parent == mydomNode) { ourParams.AppendObject(domelement); } } } } } } // We're done with DOM method calls now; make sure we still have a frame. NS_ENSURE_TRUE(mOwner, NS_ERROR_OUT_OF_MEMORY); PRUint32 cparams = ourParams.Count(); // unsigned 32 bits to unsigned 16 bits conversion if (cparams < 0x0000FFFF) mNumCachedParams = static_cast<PRUint16>(cparams); else mNumCachedParams = 0xFFFF; ... PRInt16 numRealAttrs = mNumCachedAttrs; // XXX: implied type conversion from unsigned to signed nsAutoString data; if (mContent->Tag() == nsGkAtoms::object && !mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::src) && mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::data, data)) { mNumCachedAttrs++; } // now lets make the arrays mCachedAttrParamNames = (char **)PR_Calloc(sizeof(char *) * (mNumCachedAttrs + 1 + mNumCachedParams), 1); // XXX: signed integer being used in an allocation NS_ENSURE_TRUE(mCachedAttrParamNames, NS_ERROR_OUT_OF_MEMORY); mCachedAttrParamValues = (char **)PR_Calloc(sizeof(char *) * (mNumCachedAttrs + 1 + mNumCachedParams), 1); // XXX: signed integer being used in an allocation NS_ENSURE_TRUE(mCachedAttrParamValues, NS_ERROR_OUT_OF_MEMORY); The following code will then populate the allocated memory with the cache. This will overflow the underallocated buffer that was provided. layout/generic/nsObjectFrame.cpp:3038 // Some plugins (eg Flash, see bug 234675.) are actually sensitive to the // attribute order. So we want to make sure we give the plugin the // attributes in the order they came in in the source, to be compatible with // other browsers. Now in HTML, the storage order is the reverse of the // source order, while in XML and XHTML it's the same as the source order // (see the AddAttributes functions in the HTML and XML content sinks). PRInt16 start, end, increment; if (mContent->IsNodeOfType(nsINode::eHTML) && mContent->NodeInfo()->NamespaceEquals(kNameSpaceID_None)) { // HTML. Walk attributes in reverse order. start = numRealAttrs - 1; end = -1; increment = -1; } else { // XHTML or XML. Walk attributes in forward order. start = 0; end = numRealAttrs; increment = 1; } for (PRInt16 index = start; index != end; index += increment) { const nsAttrName* attrName = mContent->GetAttrNameAt(index); nsIAtom* atom = attrName->LocalName(); nsAutoString value; mContent->GetAttr(attrName->NamespaceID(), atom, value); nsAutoString name; atom->ToString(name); FixUpURLS(name, value); mCachedAttrParamNames [c] = ToNewUTF8String(name); mCachedAttrParamValues[c] = ToNewUTF8String(value); c++; } Version(s) tested: Mozilla Firefox 3.6.3 Platform(s) tested: Windows XP SP3 -- CREDIT -------------------------------------------------------------- This vulnerability was discovered by: * J23 (http://twitter.com/HansJ23)
bp-5d5557d0-e6b4-45c0-8237-0ff972100618 Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.3a6pre) Gecko/20100617 Minefield/3.7a6pre
roc: can you find an owner for this one?
I think Josh should own this
Created attachment 452967 [details] [review] trunk fix v1.0
Created attachment 452979 [details] [review] trunk fix v1.1
Created attachment 452982 [details] [review] 1.9.2 fix v1.0 This is a minimal backport of the fix to the 1.9.2 branch. However, a browser fix might not be enough. This patch fixes the crash in the browser's code but then the JEP goes down - looks like it also has a bug. JEP is used on the 1.9.2 branch but not trunk. Will ask Steven Michaud to look into it, we may have to include an updated JEP.
Comment on attachment 452979 [details] [review] trunk fix v1.1 Patch has a problem, new one coming up.
Yup. You forgot to reset nextAttrParamIndex to 0 before + for (PRUint16 i = 0; i < mNumCachedParams; i++) { + nsIDOMElement* param = ourParams.ObjectAt(i); + if (!param) { + continue; } I'll wait for your new patch.
(I assume the problem I found is also the one you found.)
Oops, scratch comment #8 -- I was wrong.
Created attachment 453092 [details] [review] trunk fix v1.2
Created attachment 453093 [details] [review] 1.9.2 fix v1.1 This updated patch for 1.9.2 fixes the JEP crash as well.
> This updated patch for 1.9.2 fixes the JEP crash as well. Thanks. Glad to hear it!
Created attachment 453150 [details] [review] 1.9.1 fix v1.0
Is 1.9.1 actually vulnerable here?
1.9.1 is vulnerable, at least to a crash. The patch I posted fixes the problem.
pushed to mozilla-central http://hg.mozilla.org/mozilla-central/rev/fae939d0a47a
FIXED on mozilla-central. We use the status1.9.1/status1.9.2 fields for branch resolution.
"in-testsuite?" as a reminder to create/add the crash test once we've shipped this on the branches.
Comment on attachment 453093 [details] [review] 1.9.2 fix v1.1 Approved for 1.9.2.6, a=dveditz for release-drivers
Comment on attachment 453150 [details] [review] 1.9.1 fix v1.0 Approved for 1.9.1.11, a=dveditz for release-drivers
pushed to mozilla-1.9.2 http://hg.mozilla.org/releases/mozilla-1.9.2/rev/c6843f08a56f
pushed to mozilla-1.9.1 http://hg.mozilla.org/releases/mozilla-1.9.1/rev/e93d30b2790d
On 1.9.1 and 1.9.2, I'm not seeing new behavior post-fix. I'm also not seeing a crash pre-fix. On 1.9.1.10 or 1.9.2.6 with the PoC, the browser doesn't crash but goes up to 98-100% CPU usage and hangs there for a few minutes. Eventually, the CPU usage goes back to normal. With 1.9.1.11 and 1.9.2.7 build 1, I'm seeming the same behavior. This is on Windows XP. What am I missing in order to see the crash?
Al - can you test on Mac OS X? I was able to reproduce on 1.9.1, 1.9.2, and 1.9.3 on Mac OS X.
Comment on attachment 453150 [details] [review] 1.9.1 fix v1.0 Requesting approval1.9.0.next on this patch so that we can take it in upcoming Camino 2.0.x security and stability updates. If approved, I'll handle the checkins, unless the patch author requests otherwise.
This caused regression bug 580874, I wouldn't take it on 1.9.0.next without the followup fix I'm going to attach there shortly.
I don't think this caused bug 580874, bug 575836 is the cause and that is a different than this (though in the same area of code).
Comment on attachment 453150 [details] [review] 1.9.1 fix v1.0 Approved for 1.9.0.20, a=dveditz
Checking in layout/generic/nsObjectFrame.cpp; /cvsroot/mozilla/layout/generic/nsObjectFrame.cpp,v <-- nsObjectFrame.cpp new revision: 1.660; previous revision: 1.659 done