# Malicious contracts can force excessive memory usage at minimal gas cost, threatening node stability and economic security

Seda's WASM gas metering charged memory.grow by memory index instead of runtime page count, letting contracts allocate huge memory for base gas only.

- Payout: Audit contest
- Severity: medium
- Program: Seda Protocol
- Found by: stonejiajia
- Disclosed: 2024-12-20
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-12-seda-protocol-judging/issues/298
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-12-seda-protocol%3AM-15
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:sherlock sev:medium tech:rust technique:source-review vuln:accounting-error vuln:dos

**Target:** seda-wasm-vm runtime gas metering (metering.rs)
**Root cause:** In get_wasm_operation_gas_cost, Operator::MemoryGrow uses the static memory index `mem` (usually 0) instead of the runtime page count popped from the stack, so growth is charged only GAS_MEMORY_GROW_BASE.
**Steps:**
1. Read metering.rs get_wasm_operation_gas_cost; note MemoryGrow uses `*mem` (memory index) not stack value
2. Confirm WASM spec: memory.grow page count is a runtime stack operand
3. Deploy module with `i32.const 1000; memory.grow` (memory 1)
4. Observe only GAS_MEMORY_GROW_BASE charged despite 1000 pages (~65MB) allocated
5. Repeat across contracts to exhaust node memory
**How found:** Code review of the gas metering function against the WebAssembly spec, spotting that the static memory index was used where the dynamic stack operand (page count) should be.
**Exploitation:** Attacker deploys a WASM contract whose function calls memory.grow with a large constant; VM charges fixed base gas while allocating hundreds/thousands of pages, enabling cheap memory exhaustion across nodes and breaking gas economics.
**Escalation:** Repeat deployment/execution across many contracts to amplify memory pressure toward node OOM/DoS.
**Insight:** When auditing VM gas metering, verify each opcode's cost uses runtime stack operands, not static immediates; memory.grow's page count is dynamic, so any metering keyed on the memory index is a red flag.

## Proof of concept (copied exactly from the original report)

**Wasm module growing memory 1000 pages**

```
(module
  (memory 1)
  (func $exploit (result i32)
    ;; Try to grow memory by 1000 pages (65MB)
    i32.const 1000
    memory.grow
  )
  (export "exploit" (func $exploit))
)
```

