Exploiting the Math.expm1 typing bug in V8
A V8 typer bug caused Math.expm1 to be typed as never returning negative zero, so a comparison against -0 could be folded to a constant false and a bounds check on a JavaScript array could be eliminated. An attacker could then read and write memory out of bounds and build primitives leading to code execution.
abiondo.me
Published on abiondo.me
I love browser exploitation. Must be something about breaking what I consider to be one of the most complex pieces of software we run every day.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the typer omitted the negative-zero case for Math.expm1, so a SameValue comparison against -0 was typed as always false and the resulting range let the bounds check be removed.
Impact
The author could read and write memory out of bounds on a JavaScript array, and states this can be built up into primitives and ultimately code execution.
What the attacker needed
Ability to run JavaScript in the target engine; a V8 build with the bug reintroduced
How it was found
- Call the function with a number so it is compiled optimistically, then call it many times with a string to force deoptimisation and recompilation using the builtin call.
- Place the -0 in a field of an object that escape analysis later dematerialises, so the comparison stays a SameValue node until simplified lowering.
- Use the comparison result to index an array so the typer believes the index is always in bounds.
- Run the function with -0 to trigger the out-of-bounds read past the array.
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 write-up has not been written yet. The Original tab links to the source.
JavaScript engines like Chrome's V8 speed up hot code by compiling it to machine code and reasoning about what types values will have. Here the engine wrongly believed a maths function could never return negative zero, so it removed a safety check on an array index. That let the author read and write memory outside the array, which is the first step towards running their own code.
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.