Skip to content
Business logic Audit finding Intermediate

Rebalance will fail due to low precision of percentages

The rebalance function requires the contract's token balance to be zero after deposits, but because percentages are expressed in basis points with low precision, small amounts of dust remain and the check reverts. This causes rebalancing to fail when the last market does not support the token.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel, also found by hyh

The AssetManager.rebalance function has a check at the end to ensure that all tokens are deposited again:

require(token.balanceOf(address(this)) == 0, "AssetManager: there are remaining funds in the fund pool");

The idea is that the last market deposits all remainingTokens but the last market does not have to support the token in which case the transaction will fail, or the percentages parameter needs to be chosen to distribute all tokens before the last one (they need to add up to 1e4). However, these percentages have a low precision as they are in base points, i.e, the lowest unit is 1 = 0.01%. This will leave dust in the contract in most cases as the tokens have much higher precision.

POC

Assume the last market does not support the token and thus percentages are chosen as [5000, 5000] to rebalance the first two markets. Withdrawing all tokens form the markets leads to a tokenSupply = token.balanceOf(address(this)) = 10,001:

Then the deposited amount is amountToDeposit = (tokenSupply * percentages[i]) / 10000 = 10,001 * 5,000 / 10,000 = 5,000. The two deposits will leave dust of 10,001 - 2 * 5,000 = 1 in the contract and the token.balanceOf(address(this)) == 0 balance check will revert.

Impact

Rebalancing will fail in most cases if the last market does not support the token due to precision errors.

Recommended Mitigation Steps

Remove the final zero balance check, or make sure that the last market that is actually deposited to receives all remaining tokens.

kingjacob (Union) confirmed

The original report as published on Code4rena, by cmichel. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

Likely the use of integer percentages in basis points (0.01% precision) causes rounding down, leaving dust that violates the zero-balance requirement.

What the attacker needed

Last market does not support the token; percentages do not divide token supply evenly

The fix

Remove the final zero balance check, or make sure that the last market that is actually deposited to receives all remaining tokens.

How it was found

  1. Choose percentages that sum to 10000 but do not evenly divide the token supply.
  2. Withdraw all tokens from markets, resulting in a token supply that is not a multiple of 10000.
  3. Deposit according to percentages, leaving a small remainder.
  4. The final balance check fails because the contract still holds dust.

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 a contract moves tokens between markets, it checks that no tokens are left over. But the percentages used to split the tokens are not precise enough, so tiny amounts get left behind. That makes the check fail and the whole operation stops.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by cmichel

View all

196 more disclosed reports, best first.

More from Union Finance contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.