Pool fraction is not truncated when allocating the tokens allowing to receive more rewards than owed
The AllocateTokens function used Quo() instead of QuoTruncate() when calculating the pool fraction, causing rounding up instead of down. This allowed validators to receive a larger fraction of rewards than they were owed.
Originally disclosed on Code4rena
Submitted by ABAIKUNANBAEV
At the moment, the function AllocateTokens() uses the Quo() operation when calculating the pool fraction for a particular validator pool. The problem is that it does not round down to 0, allowing to receive a bigger fraction than it actually is.
Proof of Concept
Let’s take a look at how the operation is implemented:
https://github.com/initia-labs/initia/blob/main/x/distribution/keeper/allocation.go#L83
poolFraction := rewardWeight.Weight.Quo(weightsSum)
Here, the rewardWeight is divided by the WeightsSum using the Quo() arithmetic and not QuoTruncate() that rounds the number down. The number is not truncated here as the Quo() function is used. The problem lies in the fact of how the Quo() method rounds the number:
Quo() calls chopPrecisionAndRound():
https://github.com/piplabs/cosmos-sdk/blob/7ce4a34e92b12fc3aed8eec6e080b6493554072d/math/dec.go#L356
func (d LegacyDec) QuoMut(d2 LegacyDec) LegacyDec {
// multiply by precision twice
d.i.Mul(d.i, squaredPrecisionReuse)
d.i.Quo(d.i, d2.i)
chopPrecisionAndRound(d.i)
if d.i.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return d
}
QuoTruncate() (which should be used instead) calls chopPrecisionAndTruncate() under the hood:
https://github.com/piplabs/cosmos-sdk/blob/7ce4a34e92b12fc3aed8eec6e080b6493554072d/math/dec.go#L376
// mutable quotient truncate
func (d LegacyDec) QuoTruncateMut(d2 LegacyDec) LegacyDec {
// multiply precision twice
d.i.Mul(d.i, squaredPrecisionReuse)
d.i.Quo(d.i, d2.i)
chopPrecisionAndTruncate(d.i)
if d.i.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return d
}
The method always rounds down and has to be used instead; otherwise, the users will get the bigger poolFraction and therefore, bigger rewards. Take a look at the vanilla CosmosSDK implementation and how it rounds down the fractions:
https://github.com/cosmos/cosmos-sdk/blob/main/x/distribution/keeper/allocation.go#L71
powerFraction := math.LegacyNewDec(vote.Validator.Power).QuoTruncate(math.LegacyNewDec(totalPreviousPower))
Recommended mitigation steps
Use QuoTruncate() instead of Quo().
beer-1 (Initia) confirmed and commented:
Seems to be a valid report. Fixed here.
Use
quo truncateat distribution pool fraction calculation.
Status: Mitigation confirmed. Full details in reports from 0xAlix2, berndartmueller and Franfran.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code used a division function that rounds to nearest instead of one that truncates (rounds down), so the calculated pool fraction could be slightly larger than the true value.
Impact
Validators could receive slightly more rewards than they are owed, at the expense of other participants or the system.
What the attacker needed
Validator participating in reward distribution
The fix
The code was fixed by using QuoTruncate() instead of Quo() for the pool fraction calculation.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When splitting rewards, the code divides one number by another. The division method used rounds the result to the nearest value, which can round up. This means a validator could get slightly more than their fair share. The fix is to always round down.