Latent (?) write beyond bounds in WasmCompiledModuleStream::onCompilationComplete()
A size value computed as a 64-bit number is truncated to 32 bits when reserving a buffer, but the full untruncated size is then used when writing into that buffer, so a write beyond the allocated bounds could occur. The author believes the bug is currently latent because module size is capped, but suggests crafted metadata could still produce an oversized value, and that written data would be largely attacker-controlled.
Originally disclosed on Mozilla
- Fixed in 54 days
- Paid in 56 days
- Disclosed after 54 days
WasmCompiledModuleStream::onCompilationComplete() (dom\indexedDB\IDBObjectStore.cpp) might be able to write beyond bounds. The bug is that line 459 computes the |size_t| |compiledSize| (a 64 bit quantity on 64-bit builds) but line 462 implicitly truncates |compiledSize| to 32 bits and uses that smaller quantity to reserve memory. Lines 464-65, however, then write the untruncated number of bytes into that memory:
442: void
443: onCompilationComplete() override
444: {
445: if (!IsOnOwningThread()) {
446: MOZ_ALWAYS_SUCCEEDS(mOwningThread->Dispatch(NewCancelableRunnableMethod(
447: "WasmCompiledModuleStream::onCompilationComplete",
448: this,
449: &WasmCompiledModuleStream::onCompilationComplete)));
450: return;
451: }
452:
453: if (NS_FAILED(mStatus) || !mCallback) {
454: return;
455: }
456:
457: MOZ_ASSERT(mModule);
458:
459: size_t compiledSize = mModule->compiledSerializedSize();
460:
461: nsCString compiled;
462: compiled.SetLength(compiledSize);
463:
464: mModule->compiledSerialize(
465: reinterpret_cast<uint8_t*>(compiled.BeginWriting()), compiledSize);
466:
467: MOZ_ALWAYS_SUCCEEDS(NS_NewCStringInputStream(getter_AddRefs(mStream),
468: compiled));
469:
470: mModule = nullptr;
471:
472: CallCallback();
473: }
This bug appears to be latent, because DecodePreamble() in WasmValidate.cpp fails if the wasm module's file size is > |MaxModuleBytes| (currently 1 GB, and there's no security warning on the definition). However, the serialized stream include a variety of metadata that's implied by the wasm module and its environment, but which is not present in the wasm module itself. Therefore, it might be possible to compute a |compileSize| that's >= 4GB. I will attempt to do so.
If this write can occur, the data written is largely under the attacker's control.
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 an integer truncation: a 64-bit size is passed to a 32-bit length setter for allocation, while the original 64-bit size is used for the subsequent write.
Impact
If the oversized value can be produced, the write could go beyond the buffer with largely attacker-controlled data, though the author notes the bug appears latent because module size is capped at 1 GB.
What the attacker needed
Crafted wasm module metadata; compiled size reaching 4GB or more
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.
A program worked out how much memory it needed using a large number, then asked for a smaller amount because part of the number was cut off. It then copied the full large amount of data into the smaller space, which can spill over into neighbouring memory. Spilling past the end of a buffer can corrupt other data and sometimes let an attacker influence what gets written.
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.