Type Confusion in WebAssembly (CVE-2024-2887)
The WebAssembly type section decoder in V8 allowed a recursive type group to fill the type count to the maximum, after which a following non-recursive type was allocated at an index beyond the intended limit. This out-of-bounds type index could lead to a type confusion, which the reporter's sample exploit apparently used to achieve memory corruption.
Originally disclosed on Chrome VRP
- Fix shipped in 6 days
Filing this on behalf of the pwn2own reporter. From the writeup in the parenet bug:
void DecodeTypeSection() {
TypeCanonicalizer* type_canon = GetTypeCanonicalizer();
uint32_t types_count = consume_count("types count", kV8MaxWasmTypes); // (1)
for (uint32_t i = 0; ok() && i < types_count; ++i) {
...
uint8_t kind = read_u8<Decoder::FullValidationTag>(pc(), "type kind");
size_t initial_size = module_->types.size();
if (kind == kWasmRecursiveTypeGroupCode) {
...
uint32_t group_size =
consume_count("recursive group size", kV8MaxWasmTypes);
...
if (initial_size + group_size > kV8MaxWasmTypes) { // (2)
errorf(pc(), "Type definition count exceeds maximum %zu",
kV8MaxWasmTypes);
return;
}
...
for (uint32_t j = 0; j < group_size; j++) {
...
TypeDefinition type = consume_subtype_definition();
module_->types[initial_size + j] = type;
}
...
} else {
...
// Similarly to above, we need to resize types for a group of size 1.
module_->types.resize(initial_size + 1); // (3)
module_->isorecursive_canonical_type_ids.resize(initial_size + 1);
TypeDefinition type = consume_subtype_definition();
if (ok()) {
module_->types[initial_size] = type;
type_canon->AddRecursiveSingletonGroup(module_.get());
}
}
}
...
}
At (1), the limit kV8MaxWasmTypes (currently equal to one million) is passed as a maximum to consume_count(), ensuring that at most this many entries are read in from the type section.
When recursive type groups were added this check became quite obviously insufficient:
While only kV8MaxWasmTypes entries of the type section can be read in, each of those can potentially be a recursive type group containing more than one individual type definition.
This insufficiency was clearly noticed at the time of this change, as together with recursive
type groups a second check was added at (2). Here, for each recursive type group it is checked
that the addition of the constituent types would not exceed the kV8MaxWasmTypes limit.
However, this second check is still not enough. While it protects the indices of each type
allocated inside a recursive group, the presence of those groups also has implications for types
declared outside this group, as each recursive group adds to the total count of declared types.
To make this clearer, imagine a type section consisting of two entries: One recursive group
with kV8MaxWasmTypes many entries itself, and following that group one non-recursive type. The
check at (1) is passed, as the section only has two entries. While processing the recursive group,
the check at (2) is also passed, as the section has exactly kV8MaxWasmTypes entries. For the following single type, there is no further check: at (3) the type is simply allocated at the next free
index - in this case at kV8MaxWasmTypes (exceeding the usual maximum of kV8MaxWasmTypes-1).
If there was more than one non-recursive type at the end of the type section, they would similarly get assigned kV8MaxWasmTypes+1, kV8MaxWasmTypes+2, etc. as type indices.
While I was unable to reduce the sample exploit provided by the reporter, a code inspection makes it pretty clear that the underlying bug is still present.
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 per-group size check did not account for types declared after a recursive group, so a non-recursive type could be assigned an index at or above the maximum.
Impact
A crafted WebAssembly module could trigger a type confusion in the engine, potentially leading to memory corruption and code execution; the text does not confirm a working reduced exploit.
What the attacker needed
Attacker supplies a crafted Wasm module; victim loads it in a V8-based browser
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.
WebAssembly is a compact code format browsers run inside a sandbox. When V8 reads a module's list of types, it is supposed to cap how many types exist. A missing check let one extra type slip past the cap, so the engine could later treat data as the wrong kind of object, which is a type confusion and can be turned into code execution.
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.