Type Confusion in V8 (CVE-2024-5837)
A type confusion occurs in V8's JSPI feature when a re-exported imported JavaScript function is called via WebAssembly.promising. The wrong register value is passed to the wasm-to-js wrapper, causing a type confusion that could lead to memory corruption.
- Fix shipped in 20 days
TITLE
A security issue when using the JSPI feature.
VULNERABILITY DETAILS
I found that there is a type confusion when using the JSPI feature.
When using a re-exported imported JS function by WebAssembly.promising, the function NewPromisingWasmExportedFunction will construct the WasmExportedFunction object.
It uses the js-to-wasm wrapper here:
i::Handle<i::Code> wrapper =
with_suspender_param ? BUILTIN_CODE(i_isolate, WasmPromisingWithSuspender)
: BUILTIN_CODE(i_isolate, WasmPromising);
It refers to the macro JSToWasmWrapperHelper in file js-to-wasm.tq,
When the exported function is called, JSToWasmWrapperHelper runs and calls WasmReturnPromiseOnSuspendAsm:
// Both `trustedInstanceData` and `resultArray` get passed separately as
// parameters to make them GC-safe. They get passed over the stack so that
// they get scanned by the GC as part of the outgoing parameters of this
// Torque builtin.
let result: JSAny;
if constexpr (switchStack) {
result = WasmReturnPromiseOnSuspendAsm(
wrapperBuffer, trustedInstanceData, resultArray);
} else {
result =
JSToWasmWrapperAsm(wrapperBuffer, trustedInstanceData, resultArray);
}
WasmReturnPromiseOnSuspendAsm is finished in JSToWasmWrapperHelper in file builtin-x64.c for x64 system. And it will finally call the target.
__ call(call_target);
For re-exported imported JS function, the target is the wasm-to-js wrapper.
A param ref with type WasmApiFunctionRef should be passed to the wasm-to-js wrapper at the register rsi:
@export
transitioning macro WasmToJSWrapper(ref: WasmApiFunctionRef): WasmToJSResult
However, I found that in WasmReturnPromiseOnSuspendAsm, the value of rsi never changes to a WasmApiFunctionRef before calling the target and keeps the value in JSToWasmWrapperFrameConstants::kInstanceDataParamOffset as a kWasmInstanceRegister.
Thus a type confusion will occur in the WasmToJSWrapper.
I construct a POC to prove it.
You can run it at the least version of v8 (commit 21af1bc9eec78ba8d1d17e6ffd5977b12b581c9e)
You should use python3 tools/dev/gm.py x64.debug to build v8.
commond line:
out\x64.debug\d8 --experimental-wasm-jspi poc.js
VERSION
Chrome Version: V8 12.7.0
Operating System: Ubuntu
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 WasmReturnPromiseOnSuspendAsm builtin fails to set the register holding the WasmApiFunctionRef before calling the wasm-to-js wrapper, leaving a WasmInstanceData value instead.
Impact
A type confusion in the engine could lead to memory corruption and potentially arbitrary code execution.
What the attacker needed
V8 with JSPI enabled; ability to run JavaScript
How it was found
- Build V8 in debug mode using the provided command.
- Run d8 with the experimental JSPI flag and the proof-of-concept script.
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.
This is a bug in V8, the JavaScript engine used by Chrome. When a particular WebAssembly feature (JSPI) calls a JavaScript function, the engine passes the wrong kind of internal object to a wrapper, confusing the types. This can cause the program to crash or potentially be exploited.
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.