`BuyUSSDSellCollateral()` always sells 0 amount if need to sell part of collateral
The BuyUSSDSellCollateral function calculates the amount of collateral to sell using a formula that rounds down to zero when only part of the collateral needs to be sold. As a result, the protocol sells zero collateral and cannot perform the algorithmic peg between USSD and DAI.
Source: https://github.com/sherlock-audit/2023-05-USSD-judging/issues/656
Found by
J4de, T1MOH
Summary
Due to rounding error there is misbehaviour in BuyUSSDSellCollateral() function. It results in selling 0 amount of collateral.
Vulnerability Detail
Suppose the only collateral in protocol is 1 WBTC; 1 WBTC costs 30_000 USD; UniV3Pool DAI/ USSD has following liquidity: (3000e6 USSD, 2000e18 DAI) And also USSD is underpriced so call rebalance:
function rebalance() override public {
uint256 ownval = getOwnValuation(); // it low enough to dive into if statement (see line below)
(uint256 USSDamount, uint256 DAIamount) = getSupplyProportion(); // (3000e6 USSD, 2000e18 DAI)
if (ownval < 1e6 - threshold) {
// peg-down recovery
BuyUSSDSellCollateral((USSDamount - DAIamount / 1e12)/2); // 500 * 1e6 = (3000e6 - 2000e18 / 1e12) / 2
Take a look into BuyUSSDSellCollateral (follow comments):
function BuyUSSDSellCollateral(uint256 amountToBuy) internal { // 500e6
CollateralInfo[] memory collateral = IUSSD(USSD).collateralList();
//uint amountToBuyLeftUSD = amountToBuy * 1e12 * 1e6 / getOwnValuation();
uint amountToBuyLeftUSD = amountToBuy * 1e12; // 500e18
uint DAItosell = 0;
// Sell collateral in order of collateral array
for (uint256 i = 0; i < collateral.length; i++) {
// 30_000e18 = 1e8 * 1e18 / 10**8 * 30_000e18 / 1e18
uint256 collateralval = IERC20Upgradeable(collateral[i].token).balanceOf(USSD) * 1e18 / (10**IERC20MetadataUpgradeable(collateral[i].token).decimals()) * collateral[i].oracle.getPriceUSD() / 1e18;
if (collateralval > amountToBuyLeftUSD) {
// sell a portion of collateral and exit
if (collateral[i].pathsell.length > 0) {
uint256 amountBefore = IERC20Upgradeable(baseAsset).balanceOf(USSD); // 0
// amountToSellUnits = 1e8 * ((500e18 * 1e18 / 30_000e18) / 1e18) / 1e18 = 1e8 * (0) / 1e18 = 0
uint256 amountToSellUnits = IERC20Upgradeable(collateral[i].token).balanceOf(USSD) * ((amountToBuyLeftUSD * 1e18 / collateralval) / 1e18) / 1e18;
// and finally executes trade of 0 WBTC
IUSSD(USSD).UniV3SwapInput(collateral[i].pathsell, amountToSellUnits);
amountToBuyLeftUSD -= (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore); // 0 = 0 - 0
DAItosell += (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore); // 0 += 0
...
So protocol will not buy DAI and will not sell DAI for USSD in UniswapV3Pool to support peg of USSD to DAI
Impact
Protocol is not able of partial selling of collateral for token. It block algorithmic pegging of USSD to DAI
Code Snippet
Tool used
Manual Review, VS Code
Recommendation
Refactor formula of amountToSellUnits
// uint256 amountToSellUnits = (decimals of collateral) * (DAI amount to get for sell) / (price of 1 token of collateral)
uint256 amountToSellUnits = collateral[i].token).decimals() * amountToBuyLeftUSD / collateral[i].oracle.getPriceUSD()
Discussion
T1MOH593
Escalate for 10 USDC
This is not a duplicate of #111
This report describes that partially collateral can't be sold, because amountToSellUnits is 0 due to rounding issue. Noticed #183 is similar to my issue
sherlock-admin
Escalate for 10 USDC
This is not a duplicate of #111 This report describes that partially collateral can't be sold, because
amountToSellUnitsis 0 due to rounding issue. Noticed #183 is similar to my issue
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
ctf-sec
I agree this issue and #183 are not duplicate of #111 and can be grouped together as a new valid medium, will see if this is a duplicate of other issue
hrishibhat
Result: Medium Has duplicates
sherlock-admin
Escalations have been resolved successfully!
Escalation status:
- T1MOH593: accepted
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 formula for amountToSellUnits performs integer division in a way that truncates the result to zero when the amount to buy is small relative to the collateral value.
Impact
The protocol cannot partially sell collateral, which blocks the algorithmic pegging of USSD to DAI.
What the attacker needed
USSD is underpriced; Only one collateral type with high value; Partial sale needed
The fix
The formula for amountToSellUnits should be refactored to correctly compute the amount of collateral to sell based on its decimals and price.
How it was found
- Have only 1 WBTC as collateral, with 1 WBTC worth 30,000 USD.
- Have a UniV3Pool DAI/USSD with liquidity of 3000e6 USSD and 2000e18 DAI.
- USSD is underpriced, so call rebalance().
- rebalance() calls BuyUSSDSellCollateral with amountToBuy = 500e6.
- Inside BuyUSSDSellCollateral, amountToSellUnits is calculated as 0 due to rounding.
- The function executes a trade of 0 WBTC, so no DAI is bought or sold.
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.
This is a smart contract bug where a calculation that decides how much cryptocurrency to sell ends up as zero because of how numbers are rounded down in the code. When the protocol tries to sell only part of its collateral to stabilise its stablecoin, it instead sells nothing, so the stabilisation fails.