Skip to content
Memory corruption Disclosed report Advanced

Probable write beyond bounds in CodeSegment::create()

The CodeSegment::create() function in SpiderMonkey's WebAssembly code can truncate or overflow size calculations when the generated code is very large, leading to an undersized buffer allocation and a subsequent write beyond bounds. An attacker could potentially exploit this to cause memory corruption, though no proof of concept is provided.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 13 days
  • Paid in 16 days
  • Disclosed after 13 days
Text
CodeSegment::create() (js\src\wasm\WasmCode.cpp) likely can experience truncation and/or integer overflow, followed by a consequent buffer underallocation and write beyond bounds.

If |masm.bytesNeeded()| (line 197 in the first overload of ::create()) or |unlinkedBytes.length()| (line 223) is large enough, those lines can truncate the value (a |size_t|), causing succeeding lines to underallocate the buffer |codeBytes| and therefore write beyond bounds.

Additionally, even if truncation does not occur, if one of the values is large enough such that rounding it up to the next multiple of |gc::SystemPageSize| (probably 4096) (lines 198-99 and lines 223-24) causes an overflow, or such that rounding it up to the next multiple of 64k (inside AllocateCodeBytes() called on lines 201 and 226) causes an overflow, |codeBytes| will, again, be underallocated, causing a write beyond bounds.

I don't currently have a POC. I encountered https://bugzilla.mozilla.org/show_bug.cgi?id=1444668 (another write beyond bounds with a very large generated code segment) while crafting a POC, which prevented further progress.

The buggy code is:

188: /* static */ UniqueCodeSegment
189: CodeSegment::create(Tier tier,
190:                     MacroAssembler& masm,
191:                     const ShareableBytes& bytecode,
192:                     const LinkDataTier& linkData,
193:                     const Metadata& metadata)
194: {
195:     // Round up the code size to page size since this is eventually required by
196:     // the executable-code allocator and for setting memory protection.
197:     uint32_t bytesNeeded = masm.bytesNeeded();
198:     uint32_t padding = ComputeByteAlignment(bytesNeeded, gc::SystemPageSize());
199:     uint32_t codeLength = bytesNeeded + padding;
200: 
201:     UniqueCodeBytes codeBytes = AllocateCodeBytes(codeLength);
202:     if (!codeBytes)
203:         return nullptr;
204: 
205:     // We'll flush the icache after static linking, in initialize().
206:     masm.executableCopy(codeBytes.get(), /* flushICache = */ false);
207: 
208:     // Zero the padding.
209:     memset(codeBytes.get() + bytesNeeded, 0, padding);
210: 
211:     return create(tier, Move(codeBytes), codeLength, bytecode, linkData, metadata);
212: }
213: 
214: /* static */ UniqueCodeSegment
215: CodeSegment::create(Tier tier,
216:                     const Bytes& unlinkedBytes,
217:                     const ShareableBytes& bytecode,
218:                     const LinkDataTier& linkData,
219:                     const Metadata& metadata)
220: {
221:     // The unlinked bytes are a snapshot of the MacroAssembler's contents so
222:     // round up just like in the MacroAssembler overload above.
223:     uint32_t padding = ComputeByteAlignment(unlinkedBytes.length(), gc::SystemPageSize());
224:     uint32_t codeLength = unlinkedBytes.length() + padding;
225: 
226:     UniqueCodeBytes codeBytes = AllocateCodeBytes(codeLength);
227:     if (!codeBytes)
228:         return nullptr;
229: 
230:     memcpy(codeBytes.get(), unlinkedBytes.begin(), unlinkedBytes.length());
231:     memset(codeBytes.get() + unlinkedBytes.length(), 0, padding);
232: 
233:     return create(tier, Move(codeBytes), codeLength, bytecode, linkData, metadata);
234: }

The original report as published on Mozilla's Bugzilla, by mozillabugs. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

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 use of 32-bit unsigned integers (uint32_t) to store sizes that can exceed 4GB, combined with arithmetic operations that can overflow during rounding, leading to an undersized allocation.

Impact

Could lead to a write beyond bounds, potentially allowing arbitrary code execution or a crash, but no exploit has been demonstrated.

What the attacker needed

Ability to generate very large WebAssembly code

The fix

Should be fixed by using size_t for size calculations and adding overflow checks before allocation.

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.

When a program needs memory, it calculates how much to ask for. If that calculation is wrong because a number is too big to fit, the program might ask for less memory than it needs. Then, when it writes data, it can spill over into other memory, which can crash the program or let an attacker run 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.

More by mozillabugs

View all

41 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.