Skip to content
Smart contracts Audit finding Intermediate

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.

0xRobocop Follow with 0xlmanini, Aymen0909 and 20 others

Originally disclosed on Sherlock

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:

Solidity
// @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:

Solidity
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

https://github.com/sherlock-audit/2023-05-USSD/blob/6d7a9fdfb1f1ed838632c25b6e1b01748d0bafda/ussd-contracts/contracts/USSDRebalancer.sol#L121

Tool used

Manual Review

Recommendation

Delete the last 1e18 factor

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

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.

More by 0xRobocop

View all

15 more disclosed reports, best first.

More from USSD - Autonomous Secure Dollar

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.