Balancer V2 · composable stable pools · 2025-11-03

Reproduced to the wei. Then made unwritable.

The attacker drained the osETH/wETH pool in three transactions. Dimension's Fe port of Balancer runs every one of their 360 steps from the real chain state and computes exactly what Balancer's Vault computed. The same steps on Dimension's typed kernel pay the pool, until the attacker's batch reverts.

The attack, replayed

Every step of the real exploit, on both arithmetics.

Value behind each pool share, after every step of the three attack transactions, replayed from the real chain state
Balancer V2 (the faithful Fe port: every step equal to the Vault)Dimension's typed kernel
exitsswaps at dustjoins10.10.0110⁻³10⁻⁴10⁻⁵10⁻⁶transaction 1 · block 23,717,397the batch reverts at step 25: BAL#001transaction 2 · block 23,717,398transaction 3 · block 23,717,399
The same values as a table
TransactionStepsBalancer: value per share before → afterTyped: reverts at stepTyped: value per share before it stops
1 · block 23,717,3971211.0273 → 0.020225 (BAL#001)1.1856
2 · block 23,717,3981210.0202 → 4.12×10⁻⁴25 (BAL#001)0.0233
3 · block 23,717,3991184.12×10⁻⁴ → 9.19×10⁻⁶22 (BAL#001)4.76×10⁻⁴

Fixtures are the swaps Balancer's Vault executed on this pool, read from each transaction's events. Each transaction is replayed on a mainnet fork at that transaction. The faithful kernel must compute every amount Balancer computed, and must end in the pool's real state once the real transaction runs on the same fork. The typed kernel charges more once balances reach dust, and the attacker's plan stops fitting the pool: step 25 (counting from 0) fails Balancer's own BAL#001 check.

The attacker's own sequence cannot run against the typed kernels. An attacker who knew the pool rounds correctly would search for another, so the replay runs one. From the dust state the attacker's own exits leave, it re-plans each of 90 swaps, trying about 95 amounts in both directions and keeping the one that leaves the value per share lowest.

Re-planning attackerStartLowestAfter 90 swaps
Balancer (faithful)1.02930.5771 (-43.9%)0.5784 (-43.8%)
Typed1.02931.0293 (the start)1.1640 (+13.1%)
Guarded1.02931.0293 (the start)1.1640 (+13.1%)

Value per share: the invariant over the BPT supply. Evidence, not proof: the candidates are a fixed set and each choice is greedy. The exhaustive search below covers every single operation, on smaller pools.

  • holds
    Does the Fe port compute every step of the real exploit exactly as Balancer's Vault did?
    360 of 360 steps across the three attack transactions (121, 121, 118): every amount equal
  • holds
    After each attack transaction, does the faithful kernel hold exactly what the real pool held?
    WETH and osETH balances and BPT supply equal after each transaction; value per BPT 1.02735 -> 0.02019 -> 0.000412136 -> 9.19223e-06
  • holds
    Do the typed and guarded kernels stop the attacker's own sequence?
    yes: transaction 1 reverts at step 25 with BAL#001; transaction 2 reverts at step 25 with BAL#001; transaction 3 reverts at step 22 with BAL#001. Before that, value per BPT on the typed kernel rose from 1.0273 to 1.1856
  • holds
    From the real dust state, does an attacker who re-plans every swap find a loss on Balancer's arithmetic, and none on Dimension's?
    on Balancer's arithmetic: -43.9% in 90 swaps. On the typed and guarded kernels, never below where it started; after 90 swaps it ends +13.1%
The port is Balancer

Three independent proofs, before any type is applied.

220,000
vectors against Balancer's own Solidity (solc 0.7.6), on identical calldata: 0 mismatches, 49,100 identical reverts
$ make differential
669
quotes against the deployed pool at block 23,717,396, the last before the attack, for amounts up to a fifth of its 4,922 WETH: 0 mismatches
$ make fidelity
360/360
attack steps computed exactly as the Vault did, and every end state equal to the real pool's
$ make replay

The port covers FixedPoint, Math and StableMath line by line, the composable stable pool's swap, join and exit paths, and the protocol fees it mints on every join and exit. Without the fees the replay could not match: the pool minted fee shares 33 times in the first attack transaction alone.

The line

Written down, reviewed, shipped.

pkg/solidity-utils/contracts/helpers/ScalingHelpers.solsolidity
function _upscale(uint256 amount, uint256 scalingFactor) pure returns (uint256) {
    // Upscale rounding wouldn't necessarily always go in the same direction: in a swap for example the balance of
    // token in should be rounded up, and that of token out rounded down. This is the only place where we round in
    // the same direction for all amounts, as the impact of this rounding is expected to be minimal.
    return FixedPoint.mulDown(amount, scalingFactor);
}
the same line, put back into Dimension's typed poolshell
$ make show CASE=typed_pool_with_balancer_upscale
  the bug:   rejected by the compiler, error[8-0000]: type mismatch
             expected `Quantity<_, Out, Admitted>`, but `u256` is given
  the fix:   -    let scaled_out = upscale(amount_out.value(), factor.value())
             +    let scaled_out = admit(amount_out, factor)
  fixed:     compiles; PASS charges_what_the_typed_pool_charges_and_more_than_balancer

The typed kernel changes Balancer in exactly the two places it rounded an outgoing amount down. Everything else is identical, every StableMath call included, and the typed kernel reaches StableMath only through signatures that demand admitted amounts.

Exhaustive search

4,608 pool states, every operation, three kernels.

Natively, in exact integers: 3.14 million operations per kernel. Each cell counts operations that lowered the value behind each pool share, and operations that stored an invariant its balances do not support.

OperationBalancer (faithful)TypedTyped + guard
swapGivenOut48 / 00 / 00 / 0
exitGivenOut57,990 / 108,9768 / 160 / 0
joinGivenIn9 / 149 / 140 / 0
joinGivenOut0 / 570 / 570 / 0

The types remove the losses at Balancer's two rounding sites. What remains comes from StableMath's own precision, identical with and without the types, and the guard rejects exactly those operations.

Greedy attacker, repeated exact-out exitsBalancerTypedGuarded
300 / 300 wei, rate 1.058, amp 50-0.743-95.011-95.011
8 / 8 wei, rate 1.150, amp 50+1.386-2.493-2.493
1000 / 1000 wei, rate 1.210, amp 200+20.923-151.416-151.416

The attacker's gain, in invariant units. Positive only on Balancer's arithmetic.

On an EVM

The three kernels as deployed contracts.

  • holds
    Deployed on an EVM, does the faithful pool let a repeated-exit attacker take value from LPs?
    value per BPT -3.38% over 51 exits
  • holds
    Do the typed and guarded pools keep the value behind each BPT under the same attack?
    typed +19.51%, guarded +19.51%
  • holds
    Does every optimization level compute the same attack, to the wei?
    the attack's exits and value per BPT on each kernel are identical at -O0, -O1, -O2 and -Os
  • holds
    Built on Dimension itself, does typed code return exactly what the same code written by hand on raw u256 returns, at every optimization level?
    4,096 calls per level over 8 functions (the four roundings, an ERC-4626 vault's four previews), reverts included: all equal at -O0, -O1, -O2 and -Os
  • holds
    Above -O0, is Dimension's typing free, gas for gas, against the same code written by hand?
    at -O1, -O2 and -Os the roundings compile to identical bytes, and the vault to the same size with identical gas on every call. At -O0, which no one deploys, typed costs rounding +7.8%, vault +15.0% per call
Gas, the pool's real parametersBalancer (faithful)TypedGuarded
swapGivenIn15,16416,104 (+6.2%)22,644 (+49.3%)
swapGivenOut15,98117,000 (+6.4%)23,553 (+47.4%)
exitGivenIn23,24024,189 (+4.1%)24,995 (+7.6%)
exitGivenOut24,49325,424 (+3.8%)26,230 (+7.1%)
joinGivenIn25,58526,472 (+3.5%)27,278 (+6.6%)
joinGivenOut23,45324,452 (+4.3%)25,258 (+7.7%)
mean gas, -O029,85032,779 (+9.8%)36,056 (+20.8%)
mean gas, -O121,31922,274 (+4.5%)24,993 (+17.2%)
mean gas, -O221,30322,258 (+4.5%)24,978 (+17.3%)
mean gas, -Os22,74423,783 (+4.6%)26,587 (+16.9%)
bytecode, -O019,423 B29,161 B29,167 B
bytecode, -O18,014 B9,412 B9,867 B
bytecode, -O27,992 B9,400 B9,855 B
bytecode, -Os9,139 B10,299 B10,685 B

The type machinery itself compiles away. The typed contract's near-constant extra cost is real work: reading its rate through the banded boundary and checking its factors on every operation. The guard costs one invariant solve on swaps. Starting that solve from the invariant before the swap would be cheaper, but at dust balances Newton's integer iteration then settles on a different value: tried in the search, it let 13 losing operations through, so the guard keeps Balancer's own solve. Every optimization level computes the same attack to the wei; -Os costs more gas than -O2 as well as more bytes. Value per share under the EVM attack: -3.38% on Balancer's arithmetic, +19.51% typed, starting from 1.00.

The same contracts on Base Sepolia, attacked on chain →
The full account, with every limit →