Reproduced Exploit

Royco: VaultOrderbook rate check is seeded with the requested rate

The first loop in allocateOrder sets tokenToRate[token] = tokenRatesRequested[i]. The vault rate is only added after that. The comparison tokenRatesRequested > tokenToRate can never fail. An order that asks for REAL at 100 is filled when previewRateAfterDeposit returns 0, and the LP loses 1,000 BAS…

Aug 2024•Ethereum•logic•3 min read

Loss

The LP's 1,000 BASE is taken while the vault will pay a REAL rate of 0

Chain

Ethereum

Category

logic

Date

Aug 2024

Source

AuditVault

EVM Playground

Source-level debugger — step opcodes and Solidity in sync

evm-hack-analyzer

The attack is replayed in an in-browser EVM preloaded with the exact dumped fork state. The execution tree shows every call; step by Solidity line or by opcode across all depths — source, Stack, Memory, Storage, Balances (native / ERC-20 / NFT), Transient storage and Return value stay in sync. Click a tree node, opcode, or source line to jump. Query opens a search over the trace: previous and next writes, balance changes and calls, breakpoints, and a diff of everything that changed between two steps. No backend, no live RPC.

Loading fork state…

Source & credit. Reproduction of a public audit finding curated by AuditVault — the original finding: 46678-rate-checks-unenforceable-due-to-faulty-cache-value-initiali. Standalone Foundry PoC and full write-up: 46678-rate-checks-unenforceable-due-to-faulty-cache-value-initiali_exp in the evm-hack-registry mirror.


Vulnerability classes: vuln/logic/state-update · vuln/logic/missing-check

Key info#

FieldValue
LossThe LP's 1,000 BASE is taken while the vault will pay a REAL rate of 0
Vulnerable contractVaultOrderbook.allocateOrder
Attacker EOA0x1111111111111111111111111111111111111111 (the allocator)
Attack contractExploit
Attack txExploit.run()
Chain / block / dateEthereum model · block 0 · 2024-08
Compilersolc 0.8.27, cancun
Bug classFirst loop copies tokenRatesRequested into the cache the check reads

TL;DR#

The first loop in allocateOrder sets tokenToRate[token] = tokenRatesRequested[i]. The vault rate is only added after that. The comparison tokenRatesRequested > tokenToRate can never fail. An order that asks for REAL at 100 is filled when previewRateAfterDeposit returns 0, and the LP loses 1,000 BASE. Deleting that loop makes the same fill revert.

The vulnerable code#

SOLIDITY
for (uint256 i; i < order.tokenRatesRequested.length; ++i) {
    tokenToRate[order.tokensRequested[i]] = order.tokenRatesRequested[i]; // @> VULN: cache starts at the requested rate, so the check below cannot fail
}
DIFF
- for (uint256 i; i < order.tokenRatesRequested.length; ++i) {
-     tokenToRate[order.tokensRequested[i]] = order.tokenRatesRequested[i];
- }

Root cause#

The cache is initialized to the value the later loop is supposed to test. Adding a non-negative vault rate cannot make the cached value smaller than the request, so OrderConditionsNotMet is dead.

Preconditions#

  • The LP requests REAL at rate 100 and has approved 1,000 BASE.
  • The matching campaign's previewRateAfterDeposit is 0.

Attack walkthrough#

  1. The LP approves 1,000 BASE.
  2. allocateOrder is called with the REAL campaign.
  3. The cache for REAL is set to 100 before the vault is read.
  4. The vault adds 0. The check is 100 > 100, which does not revert.
  5. The book pulls 1,000 BASE.
  6. The LP receives no REAL. The allocator holds the 1,000 BASE.

Diagrams#

flowchart TD A["LP requests REAL at rate 100"] --> B["Vault preview rate after the deposit is 0"] B --> C["VULN copies the requested rate into tokenToRate"] C --> D["The check compares 100 with 100 and does not revert"] D --> E["1000 BASE is pulled from the LP"] E --> F["The LP receives 0 REAL"]

Impact#

The LP loses 1,000 BASE on an order the vault cannot pay. The control, which starts the cache at 0, reverts and the deposit stays with the LP.

How to reproduce#

BASH
cd 46678-rate-checks-unenforceable-due-to-faulty-cache-value-initiali_exp
forge test -vv

Sources#


Sources & further analysis#

Reproductions & code

Alerts & third-party analyses

  • Web3Sec X hacked database: search.
  • Rekt leaderboard: search.
  • Solodit incident search: search.

These dashboards index community alerts tweets, post-mortems, and independent write-ups. Reach them through the protocol name above to cross-check this reproduction against other analyses.