Malicious contracts can force excessive memory usage at minimal gas cost, threatening node stability and economic security
The WebAssembly gas metering code incorrectly charged memory growth based on the memory index rather than the actual number of pages requested, so contracts could allocate large amounts of memory for almost no gas. An attacker could deploy contracts that consume excessive node memory, potentially causing denial of service or breaking the economic model.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-12-seda-protocol-judging/issues/298
Found by
stonejiajia
Summary
Incorrect implementation of WebAssembly memory growth gas metering will cause a resource exhaustion vulnerability for blockchain node operators as attackers will deploy contracts that allocate large amounts of memory while bypassing appropriate gas costs.
Root Cause
In metering.rs https://github.com/sherlock-audit/2024-12-seda-protocol/blob/main/seda-wasm-vm/runtime/core/src/metering.rs#L56
In the gas metering code for WebAssembly operations, specifically in the get_wasm_operation_gas_cost function:
Operator::MemoryGrow { mem, mem_byte: _ } => {
GAS_MEMORY_GROW_BASE + ((WASM_PAGE_SIZE as u64 * *mem as u64) * GAS_PER_BYTE)
}
The implementation incorrectly uses the memory index (mem) instead of the actual number of pages being allocated. This is a fundamental misunderstanding of the WebAssembly specification, where:
- The
memparameter refers to the memory index (which memory segment to operate on) - The actual number of pages to grow is a runtime value taken from the stack
- In most WebAssembly modules,
memis 0 (since they only have one memory section)
This leads to almost all memory growth operations only being charged the base fee (GAS_MEMORY_GROW_BASE), regardless of how many pages are actually allocated.
Relevant specifications and references:
MDN documentation clearly states that the page count parameter for memory.grow is passed through the stack, as in (memory.grow (i32.const 1)). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Memory/Grow
The operational semantics of memory.grow in the WebAssembly Core Specification explicitly depends on the runtime stack top value (see Instructions section in Chapter 6). https://webassembly.github.io/spec/core/syntax/instructions.html
The JavaScript API design of WebAssembly.Memory.grow() also verifies that the page count is a dynamic parameter https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/Memory/grow
Internal Pre-conditions
- A WebAssembly module needs to include memory growth operations (
memory.grow) - The blockchain system must be using this gas metering implementation
- The contract must be accepted by the blockchain validators/nodes
External Pre-conditions
None identified.
Attack Path
- Attacker creates a malicious WebAssembly contract that includes
memory.growoperations requesting large amounts of memory (hundreds or thousands of pages) - Attacker deploys this contract to the blockchain
- When the contract executes, it calls
memory.growwith a large value - The VM charges only
GAS_MEMORY_GROW_BASEgas (fixed cost) for this operation, regardless of the actual memory pages allocated - The contract can now use this large memory allocation for computation without having paid the appropriate gas costs
- Attacker can repeat this process with multiple contracts, causing excessive memory consumption across blockchain nodes
Impact
The blockchain nodes suffer excessive resource consumption without corresponding gas payments. This could lead to:
- Reduced node performance due to memory pressure
- Potential denial of service for legitimate transactions
- Breaking of the economic model intended to balance resource usage with gas costs
- In extreme cases, nodes may crash due to out-of-memory conditions
The economic impact is significant as attackers can execute operations that should cost GAS_MEMORY_GROW_BASE + (WASM_PAGE_SIZE * actual_pages * GAS_PER_BYTE) but instead only pay GAS_MEMORY_GROW_BASE.
PoC
// Simple WebAssembly module (in text format) that exploits this vulnerability:
(module
(memory 1)
(func $exploit (result i32)
;; Try to grow memory by 1000 pages (65MB)
i32.const 1000
memory.grow
)
(export "exploit" (func $exploit))
)
This module would be charged only GAS_MEMORY_GROW_BASE despite allocating 1000 pages (65MB) of memory.
Mitigation
The gas metering for memory growth operations should be implemented as a runtime check rather than static analysis. Two approaches are recommended:
- Modify the metering middleware to inject runtime checks for memory growth:
// Pseudocode for runtime metering
if operation == memory.grow {
let pages_to_grow = get_value_from_stack();
let gas_cost = GAS_MEMORY_GROW_BASE + (WASM_PAGE_SIZE as u64 * pages_to_grow * GAS_PER_BYTE);
charge_gas(gas_cost);
}
- Implement a host function hook that intercepts memory.grow operations:
// Host function approach
fn intercept_memory_grow(&mut self, pages: u32) -> Result<i32, Error> {
let gas_cost = GAS_MEMORY_GROW_BASE + (WASM_PAGE_SIZE as u64 * pages as u64 * GAS_PER_BYTE);
self.charge_gas(gas_cost)?;
// Proceed with actual memory grow operation
self.original_memory_grow(pages)
}
The fix must be applied immediately as this vulnerability fundamentally breaks the economic security model of the blockchain's resource accounting.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/sedaprotocol/seda-wasm-vm/pull/59
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
Impact
Blockchain nodes could suffer excessive memory consumption without corresponding gas payments, potentially leading to reduced performance, denial of service for legitimate transactions, or node crashes due to out-of-memory conditions.
What the attacker needed
A WebAssembly module with memory.grow; Blockchain uses this gas metering; Contract accepted by validators
The fix
The protocol team fixed this issue by updating the gas metering to charge based on the actual number of pages requested at runtime.
How it was found
- Create a WebAssembly contract that includes memory.grow operations requesting large amounts of memory.
- Deploy the contract to the blockchain.
- When the contract executes, it calls memory.grow with a large value.
- The VM charges only the base gas cost regardless of the actual pages allocated.
- The contract uses the large memory allocation without paying the appropriate gas.
- Repeat with multiple contracts to cause excessive memory consumption across nodes.
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.
In a blockchain, every operation costs a small fee called gas to prevent abuse. This report found that when a smart contract asked for more memory, the system charged a fixed tiny fee instead of a fee based on how much memory was requested. So an attacker could make contracts that use huge amounts of memory very cheaply, which could slow down or crash the computers that run the blockchain.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.