Array.reduceRight() info leak and potential code execution
Originally disclosed on Mozilla
- Fixed in 25 hours
- Paid in 2.1 years
- Disclosed after 25 hours
Created attachment 539035 ruby server PoC
Chris Rohlf and Yan Ivnitskiy are giving a talk at BlackHat 2011 on the security risks of JIT engines. https://www.blackhat.com/html/bh-us-11/bh-us-11-briefings.html#Rohlf
The have provided a Ruby Sinatra server PoC and the following details:
=begin
Mozilla Firefox 4.0.1 Array.reduceRight() Vulnerability
Firefox 4.0.1 contains an integer overflow that can lead to an information leak and arbitrary code execution.
Quick Reproduction Steps:
- Create an Array object
- Set Array object length to MAX_UINT-1
- Call reduceRight(Function()) on new Array
When the reduceRight method is called on an Array object instance execution transferred to array_extra() in jsarray.cpp. This function first determines if the argument passed to the method is a callable object (such as a function) that will be called for each element of the array. If this test passes then the following code is executed:
-
line 2740 jsarray.cpp reads in the Array.length property to an unsigned integer: ... jsuint length; if (!js_GetLengthProperty(cx, obj, &length)) return JS_FALSE; ...
-
line 2767 jsarray.cpp start, end and step are initialized but reversed if the method called is reduceRight(). These variables are all signed integers ... jsint start = 0, end = length, step = 1;
switch (mode) { case REDUCE_RIGHT: start = length - 1, end = -1, step = -1; ...
-
A call to GetElement is made on line 2784. Below is the function prototype for the GetElement function: ... GetElement(JSContext *cx, JSObject *obj, jsdouble index, JSBool *hole, Value *vp) ...
This first call to GetElement can be avoided by providing >= 2 arguments to the reduceRight() call.
The call to GetElement on line 2839 is the call we want to reach. This call lies within a giant for loop that iterates over each element of the array calling the Javascript callback provided as the first argument to reduceRight()
-
Now that a controllable signed index value is passed into GetElement the following if statement is executed: ... JS_ASSERT(index >= 0); // <- NOT EXECUTED UNLESS DEBUG BUILD if (obj->isDenseArray() && index < obj->getDenseArrayCapacity() && !(*vp = obj->getDenseArrayElement(uint32(index))).isMagic(JS_ARRAY_HOLE)) { *hole = JS_FALSE; return JS_TRUE; } ...
-
The call to obj->isDenseArray() should succeed.
-
The return value of obj->getDenseArrayCapacity() should return 16
-
The call to '*vp = obj->getDenseArrayElement(uint32(index))).isMagic(JS_ARRAY_HOLE)' boils down to a call to getSlot(index) on line 334 of jsobjinlines.h This getSlot(index) call is on 686 of jsobj.h basically executes:
obj->slots[attacker_controlled_index]
-
The *vp pointer now points out of bounds from the obj->slots array. If this doesn't point to a mapped page then the process will crash. If the page is mapped then we read this value and return it as the value of that location in the array.
-
*vp is actually a Value class instance which contains a jsval_layout union, which means our leaked Value may be interpreted as an Integer, String or Object value.
Continuing down the vulnerable code path...
- Back in array_extra() the following code is executed
... for (jsint i = start; i != end; i += step) { JSBool hole; ok = JS_CHECK_OPERATION_LIMIT(cx) && GetElement(cx, obj, i, &hole, tvr.addr()); // line 2839 of jsarray.cpp (shown in #4) ... uintN argi = 0; if (REDUCE_MODE(mode)) // setup the arguments to reduceRight() session[argi++] = *vp; // prev session[argi++] = tvr.value(); // current (our information leak) session[argi++] = Int32Value(i); // index session[argi] = objv; // array
/* Do the call. */ ok = session.invoke(cx); // invoke the javascript callback...
-
So now in our reduceRight() callback function we have a leaked value (current)
-
Code execution is possible via heap spray. We simply spray a bunch of fake Value's tagged as type JSVAL_TYPE_OBJECT and control their asPtr and ptr members which will lead to all sorts of exploitable conditions.
-
Below is a working ruby Sinatra server that will demonstrate a reliable information leak and somewhat reliable code execution.
When run under 32bit linux this POC should leak many libxul pointers stored just beneath the obj->slots[] array in memory. Heres the output of GDB that proves code execution is possible. Once the Value is controlled by the attacker in the reduceRight callback we trigger a method off the object such as setting an element and execution is transferred to the JIT'd SetElem method which calls obj->setProperty()
(gdb) bt 5 #0 0x070c0c0c in ?? () #1 0x01e76af4 in setProperty (f=...) at /builds/slave/rel-2.0-lnx-bld/build/js/src/jsobj.h:1232 #2 js::mjit::stubs::SetElem<0> (f=...) at /builds/slave/rel-2.0-lnx-bld/build/js/src/methodjit/StubCalls.cpp:567 #3 0x08cf3b4a in ?? () #4 0x01daf61a in js::mjit::EnterMethodJIT (cx=0xb1178680, fp=0xb2eff0c0, code=0x8cf3a10, stackLimit=0xb2f8baf0) at /builds/slave/rel-2.0-lnx-bld/build/js/src/methodjit/MethodJIT.cpp:749 (More stack frames follow...) (gdb) i r eax 0xb1178680 -1323858304 ecx 0x1 1 edx 0x70c0c0c 118230028 ebx 0x22827b4 36186036 esp 0xbfe67e6c 0xbfe67e6c ebp 0xbfe67ec0 0xbfe67ec0 esi 0xb2eff108 -1292898040 edi 0xc0c0c0c 202116108 eip 0x70c0c0c 0x70c0c0c eflags 0x210206 [ PF IF RF ID ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51
Disassembly of SetElement once edx is controlled:
0x1e76ac8 <js::mjit::stubs::SetElem<0>(js::VMFrame&)+152>: mov 0x64(%edx),%edx <- edx = 0x0c0c0c0c 0x1e76acb <js::mjit::stubs::SetElem<0>(js::VMFrame&)+155>: test %edx,%edx <- edx = 0x070c0c0c 0x1e76acd <js::mjit::stubs::SetElem<0>(js::VMFrame&)+157>: je 0x1e76b88 <js::mjit::stubs::SetElem<0>(js::VMFrame&)+344> 0x1e76ad3 <js::mjit::stubs::SetElem<0>(js::VMFrame&)+163>: lea 0x28(%esp),%eax 0x1e76ad7 <js::mjit::stubs::SetElem<0>(js::VMFrame&)+167>: mov %eax,0xc(%esp) 0x1e76adb <js::mjit::stubs::SetElem<0>(js::VMFrame&)+171>: mov 0x20(%esp),%eax 0x1e76adf <js::mjit::stubs::SetElem<0>(js::VMFrame&)+175>: movl $0x0,0x10(%esp) 0x1e76ae7 <js::mjit::stubs::SetElem<0>(js::VMFrame&)+183>: mov %ecx,0x8(%esp) 0x1e76aeb <js::mjit::stubs::SetElem<0>(js::VMFrame&)+187>: mov %edi,0x4(%esp) 0x1e76aef <js::mjit::stubs::SetElem<0>(js::VMFrame&)+191>: mov %eax,(%esp) 0x1e76af2 <js::mjit::stubs::SetElem<0>(js::VMFrame&)+194>: call *%edx <- call 0x070c0c0c (setProperty)
This vulnerability was discovered and researched by Chris Rohlf of Matasano Security.
** This has only been tested on 32bit Ubuntu 11.04 with Firefox 4.0.1 **
=end
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A info disclosure bug in JavaScript Engine. Mozilla made it public on 14 June 2011 as CVE-2011-2371.
About Info disclosure
Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is Info disclosure?
Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.