All field notes

DeFi · Retrospective

Rounding is a security property: reading the Balancer exploit as a contract writer

A balance scale with tiny blue beads slipping through a gap in one pan.
Concept illustration · AI-generated

On 15 and 16 November a panel I built pro bono for Bodhi Meditation PJ ran at the temple's Medicine Buddha Birthday Festival. Alongside that work, I have spent much of the month reading about the exploit that drained Balancer V2's Composable Stable Pools on 3 November, on Ethereum and several other chains.

This week Balancer published its post-mortem. Like its preliminary findings earlier in the month, it puts the cause in rounding: incorrect rounding in the exact-out swap path of its stable pools. No key was stolen and no signer was deceived. The pools' own arithmetic gave value away, one smallest unit at a time.

What happened, as reported

A Balancer pool holds tokens with different numbers of decimals, and some of them, such as liquid staking tokens, are valued against the others through a rate. Before doing its maths, the pool multiplies every amount by a scaling factor that normalises the decimals and applies that rate. According to Balancer's preliminary findings, as The Block reported on 6 November, when those scaling factors were not whole numbers, values in certain exact-out swaps rounded down, and the small discrepancies were used to manipulate pool balances. Balancer said its V3 pools and other pool types were not affected.

OpenZeppelin's analysis, published the next day, shows why the direction mattered. The scaling step always rounded down, whichever way the swap went. In an exact-out swap the caller names what they want to receive and the pool works out what they owe. A figure rounded down at that point undercharges the caller.

One smallest unit per step is nothing on its own. The attacker made it something in two ways. First, by pushing the pool's balances very low, where a truncated unit is a large share of each amount. Second, by repeating swaps many times inside a single batch, which Balancer's vault settles only at the end. Inside the batch the attacker could run a deficit, including in the pool's own token, and pay only the net amount once.

So the attack needed three ordinary things at once: rounding that favoured the caller, a state in which one unit mattered, and a cheap way to repeat it.

The rule a division has to follow

Every contract that divides has to decide which way to round, and "whatever the library does" is not an answer. The rule I hold contracts to is that the protocol never rounds in the caller's favour. Amounts a user receives round down. Amounts a user owes round up. Applied consistently, the worst rounding can do is leave a little dust in the contract. Applied in one direction only, it becomes a lever.

OpenZeppelin also notes that the code involved was added after its second audit of Balancer in 2021, and was outside that audit's scope. An audit covers the code it was shown. A later override of one function can change what an earlier review relied on.

Looking at my own contracts

The TRVC token contract I wrote for Binance Smart Chain in May 2021 does no division at all. The contract caps minting at a maximum supply and lets holders burn, using only additions, subtractions and comparisons. A token that only moves balances has nothing to round.

The decimals were where the rounding question actually lived. One TRVC on Trivechain is a hundred million of the chain's smallest units. The BEP20 token has 18 decimals. Moving TRVC onto BSC multiplies the smallest units by ten billion, which is exact. Moving it back divides by ten billion, which is exact only when the amount happens to be a multiple. Any bridge in that direction has to choose what happens to the remainder: refuse the transfer, leave the dust behind, or round. Whichever it chooses, it must not round in the sender's favour.

What I would ask of any contract that divides

  • Write the direction down. Beside every division, a comment saying which way it rounds and why, and a test that fails if someone changes it.
  • Test with the smallest numbers. Balances of a few units, amounts of one unit, and rates that are not whole numbers, not only the realistic values a demo uses.
  • Test sequences, not single calls. A property worth checking: no round trip of operations, in any order and any number of times, leaves the caller with more than they started with.
  • Treat batching as an amplifier. A feature that lets a caller repeat an operation many times before settling multiplies any error inside that operation.
  • Review the maths again when its inputs change. A new rate source or an override of a scaling function can look like configuration and still change every result.

Arithmetic deserves the same suspicion

Most of us learned to round to the nearest. In a contract that holds other people's money, nearest is the wrong instinct. The right one is to decide, for each division, who is allowed to lose the fraction, and to make sure it is never the protocol losing it to the person calling.

This was not a missing access check or a leaked key, the failures most reviews are organised around. It was arithmetic. I think arithmetic deserves the same suspicion as permissions.