Reproduced Exploit

ConvexTradeExecutor.totalFunds ignores the WETH balance

totalFunds returns positionInWantToken, whose sum is staked LP value in ETH, idle LP value in ETH, and address(this).balance. The executor is also paid and stored in WETH. That balance is not in the sum. With 20e18 staked, 5e18 idle LP, 3 ETH, and 10 WETH, totalFunds returns 28e18. The position is…

May 2022•Ethereum•logic•3 min read

Loss

totalFunds returns 28e18. The executor holds 38e18. The missing 10e18 is WETH

Chain

Ethereum

Category

logic

Date

May 2022

Source

AuditVault

EVM Playground

Source-level debugger — step opcodes and Solidity in sync

evm-hack-analyzer

The attack replays in your browser. Step through it by Solidity line or by opcode.

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: 13256-the-weth-tokens-are-not-taken-into-account-in-the-convextrad. Standalone Foundry PoC and full write-up: 13256-the-weth-tokens-are-not-taken-into-account-in-the-convextrad_exp in the evm-hack-registry mirror.


Vulnerability classes: vuln/logic/omitted-balance · vuln/defi/valuation

Reproduction: self-contained synthetic Foundry reduction; see output.txt.

Key info#

FieldValue
LosstotalFunds returns 28e18. The executor holds 38e18. The missing 10e18 is WETH
Vulnerable contractConvexTradeExecutor.totalFunds
Attacker EOA0x1111111111111111111111111111111111111111
Attack contractExploit
Attack txExploit.run() with 3 ETH
Chain / block / dateEthereum model · block 0 · 2022-05
Compilersolc 0.8.27, cancun, via IR
Bug classPosition value adds native ETH and leaves out WETH.balanceOf

TL;DR#

totalFunds returns positionInWantToken, whose sum is staked LP value in ETH, idle LP value in ETH, and address(this).balance. The executor is also paid and stored in WETH. That balance is not in the sum. With 20e18 staked, 5e18 idle LP, 3 ETH, and 10 WETH, totalFunds returns 28e18. The position is worth 38e18. The fixed function adds WETH.balanceOf(this) and returns 38e18.

Background#

ConsenSys Diligence reviewed Brahma Fi in May 2022. Every trade executor's totalFunds is supposed to report every asset the contract holds, because later accounting uses that figure. ConvexTradeExecutor.totalFunds delegates to ConvexPositionHandler.positionInWantToken. _getTotalBalancesInETH values staked and idle Curve LP in ETH and then sets ethBalance = address(this).balance. WETH is a token balance, so it is invisible. The contract receives WETH up front and holds WETH again before a withdrawal.

The vulnerable code#

SOLIDITY
ethOut = address(this).balance;
uint256 amount = stakedOut + lpOut + ethOut; // @> VULN: ETH is included. WETH is not.

The audited return is stakedLpBalanceInETH + lpTokenBalanceInETH + ethBalance in ConvexPositionHandler.positionInWantToken, and ethBalance = address(this).balance in _getTotalBalancesInETH.

Root cause#

Native ETH and WETH are the same asset in the position and different balances in the contract. The sum reads only the native balance. Ten WETH on the contract does not change totalFunds.

Preconditions#

  • Staked LP has already been valued at 20e18 ETH.
  • Idle LP has already been valued at 5e18 ETH.
  • The executor holds 3 ETH and 10 WETH.
  • Buggy total is 28e18. Fair total is 38e18.

Attack walkthrough#

  1. run forwards 3 ETH into the executor. The 10 WETH is already there.
  2. totalFunds stores the staked leg, the idle LP leg, and the 3 ETH, then adds those three.
  3. The return value is 28e18. WETH.balanceOf is still 10e18.
  4. The 10e18 gap is minted to the sink. The control adds the WETH balance and returns 38e18.

Diagrams#

flowchart TD Legs["Staked 20e18 plus idle LP 5e18 plus 3 ETH"] --> Sum["totalFunds adds those three legs"] Sum --> Report["Reported value is 28e18"] WethBal["Contract also holds 10 WETH"] --> Report Report --> Fair["Fair value is 38e18"] Fair --> Gap["10e18 of WETH is omitted"]

Impact#

Any accounting that trusts totalFunds understates the Convex leg by the full WETH balance. Here that is 10e18 out of a 38e18 position. Share price, withdrawal value, and the split between the no-loss leg and the rest of the vault are short by that WETH. The tokens are still in the contract. They are just not in the number.

Remediation#

Add the WETH balance to the ETH-denominated sum:

DIFF
- uint256 amount = stakedLpBalanceInETH + lpTokenBalanceInETH + address(this).balance;
+ uint256 amount = stakedLpBalanceInETH + lpTokenBalanceInETH
+     + address(this).balance + WETH.balanceOf(address(this));

How to reproduce#

BASH
cd 13256-the-weth-tokens-are-not-taken-into-account-in-the-convextrad_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.