Reproduced Exploit

QuantAMM: tokenIndex minus 4 uses a token weight as its own multiplier

For a pool with more than 4 tokens, the multiplier for token 4 lives at index 4 of the second weight word. The code subtracts 4 from tokenIndex first, so the index is 0 and the weight is passed as the multiplier. With weight 0.2e18, multiplier wrongly 0.2e18, and 2 blocks elapsed, the normalized we…

Dec 2024•Ethereum•logic•3 min read

Loss

A withdrawal pays 600e18 of token 4 instead of the fair 200e18. LPs keep 400e18 instead of 800e18

Chain

Ethereum

Category

logic

Date

Dec 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: 44295-token-index-error-in-getnormalizedweights-calculates-incorre. Standalone Foundry PoC and full write-up: 44295-token-index-error-in-getnormalizedweights-calculates-incorre_exp in the evm-hack-registry mirror.


Vulnerability classes: vuln/logic/wrong-condition

Key info#

FieldValue
LossA withdrawal pays 600e18 of token 4 instead of the fair 200e18. LPs keep 400e18 instead of 800e18
Vulnerable contractQuantAMMWeightedPool._getNormalizedWeights
Attacker EOA0x1111111111111111111111111111111111111111
Attack contractExploit
Attack txExploit.run()
Chain / block / dateEthereum model · block 0 · 2024-12
Compilersolc 0.8.27, cancun
Bug classsecond-slot multiplier index is reset to 0

TL;DR#

For a pool with more than 4 tokens, the multiplier for token 4 lives at index 4 of the second weight word. The code subtracts 4 from tokenIndex first, so the index is 0 and the weight is passed as the multiplier. With weight 0.2e18, multiplier wrongly 0.2e18, and 2 blocks elapsed, the normalized weight is 0.6e18 instead of 0.2e18. A withdrawal of the 1000e18 reserve pays 600e18.

The vulnerable code#

SOLIDITY
tokenIndex = this.shiftIndex(tokenIndex);
// shiftIndex returns tokenIndex - 4
// @> VULN: multiplier index collapses onto the weight slot

Do not subtract 4. The finding's mitigation is to leave tokenIndex at 4 so secondFourWeights[tokenIndex] is the multiplier.

Root cause#

The first slot correctly sets tokenIndex = 4 and reads multipliers at indices 4 through 7. The second slot then does tokenIndex - 4, which the comment in the report calls out as resetting the index to 0. Weight and multiplier become the same stored value, and calculateBlockNormalisedWeight treats a 0.2 wad weight as a per-block multiplier.

Preconditions#

  • nTokens is 5.
  • Token 4 weight is 2e17. The real multiplier at index 4 is 0.
  • Time since the last update is fixed at 2 so the result does not depend on the chain clock.
  • The pool reserve of token 4 is 1000e18.

Attack walkthrough#

  1. withdrawToken4 loads normalized weights.
  2. shiftIndex returns 4 - 4 = 0.
  3. Token 4 is interpolated with secondFourWeights[0], the weight, as the multiplier.
  4. The normalized weight is 6e17.
  5. The pool transfers 600e18. The fair amount at weight 2e17 is 200e18.
  6. The pool is left holding 400e18.

Diagrams#

flowchart TD A["Pool has 5 tokens and a 1000e18 token-4 reserve"] --> B["tokenIndex is set to 4 for the first slot"] B --> C["VULN shiftIndex subtracts 4"] C --> D["Multiplier slot is the weight 0.2e18"] D --> E["Normalized weight becomes 0.6e18"] E --> F["Withdrawal pays 600e18 instead of 200e18"] B --> G["Control keeps index 4 and multiplier 0"] G --> H["Fair withdrawal is 200e18"]

Impact#

LPs of token 4 lose 400e18 on this withdrawal: the caller receives 600 where a correct weight pays 200, and 400 remains in the pool instead of 800. Every later swap or join that reads normalized weight 4 is priced off the same wrong index.

How to reproduce#

BASH
cd 44295-token-index-error-in-getnormalizedweights-calculates-incorre_exp
forge test -vv

output.txt is the captured passing run. The control test checks the recommended fix.

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.