Wrong computation of the amountToSellUnit variable
The rebalancer contract computed the amount of collateral to sell during a peg-down recovery with an extra 1e18 scaling factor, so the figure was far smaller than intended. An attacker or ordinary user could then trigger a recovery that sells the wrong quantity of collateral, leaving the peg correction ineffective.
Source: https://github.com/sherlock-audit/2023-05-USSD-judging/issues/535
Found by
0xRobocop, 0xlmanini, Aymen0909, Bahurum, Bauer, Juntao, Nyx, Proxy, VAD37, Vagner, WATCHPUG, __141345__, auditsea, carrotsmuggler, immeas, innertia, kiki_dev, pengun, qpzm, saidam017, sakshamguruji, toshii, tvdung94
Summary
The variable amountToSellUnits is computed wrongly in the code which will lead to an incorrect amount of collateral to be sold.
Vulnerability Detail
The BuyUSSDSellCollateral() function is used to sell collateral during a peg-down recovery event. The computation of the amount to sell is computed using the following formula:
// @audit-issue Wrong computation
uint256 amountToSellUnits = IERC20Upgradeable(collateral[i].token).balanceOf(USSD) * ((amountToBuyLeftUSD * 1e18 / collateralval) / 1e18) / 1e18;
The idea is to sell an amount which is equivalent (in USD) to the ratio of amountToBuyLeftUSD / collateralval. Flattening the equation it ends up as:
uint256 amountToSellUnits = (collateralBalance * amountToBuyLeftUSD * 1e18) / (collateralval * 1e18 * 1e18);
// Reducing the equation
uint256 amountToSellUnits = (collateralBalance * amountToBuyLeftUSD) / (collateralval * 1e18);
amountToBuyLeftUSD and collateralval already have 18 decimals so their decimals get cancelled together which will lead the last 1e18 factor as not necessary.
Impact
The contract will sell an incorrect amount of collateral during a peg-down recovery event.
Code Snippet
Tool used
Manual Review
Recommendation
Delete the last 1e18 factor
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 an unnecessary extra 1e18 scaling factor in the amountToSellUnits formula, since both amountToBuyLeftUSD and collateralval already carry 18 decimals.
Impact
The contract would sell an incorrect, much smaller amount of collateral during a peg-down recovery, so the recovery would not work as intended.
What the attacker needed
A peg-down recovery event is triggered
The fix
The extra 1e18 factor should be removed from the formula so the units cancel correctly.
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.
A stablecoin is meant to stay close to one dollar. When its price drops, the contract sells some of the assets backing it to buy the coin back and push the price up. Here the maths for 'how much to sell' had an extra multiplication by a huge number, so the contract would sell a much smaller amount than it should.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.