Skip to content
DoS Audit finding Intermediate

rebalance process incase of selling the collateral, could revert because of underflow calculation

The rebalance function in the USSD contract can revert due to an underflow when subtracting the amount of base asset received from a swap from the remaining amount to buy, because the swap output may exceed the expected amount. An attacker could potentially trigger this condition, causing the rebalance process to fail.

0xHati Follow with Dug, GimelSec and 10 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-05-USSD-judging/issues/111

Found by

0xHati, Dug, GimelSec, Juntao, PokemonAuditSimulator, T1MOH, WATCHPUG, XDZIBEC, ast3ros, saidam017, toshii, tsvetanovv, twicek

Summary

rebalance process, will try to sell the collateral in case of peg-down. However, the process can revert because the calculation can underflow.

Vulnerability Detail

Inside rebalance() call, if BuyUSSDSellCollateral() is triggered, it will try to sell the current collateral to baseAsset. The asset that will be sold (amountToSellUnits) first calculated. Then swap it to baseAsset via uniswap. However, when subtracting amountToBuyLeftUSD, it with result of (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore). There is no guarantee amountToBuyLeftUSD always bigger than (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore).

This causing the call could revert in case (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore) > amountToBuyLeftUSD.

There are two branch where amountToBuyLeftUSD -= (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore) is performed :

  1. Incase collateralval > amountToBuyLeftUSD

https://github.com/sherlock-audit/2023-05-USSD/blob/main/ussd-contracts/contracts/USSDRebalancer.sol#L116-L125

collateralval is calculated using oracle price, thus the result of swap not guaranteed to reflect the proportion of amountToBuyLefUSD against collateralval ratio, and could result in returning baseAsset larger than expected. And potentially (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore) > amountToBuyLeftUSD

Solidity
        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);
            uint256 amountToSellUnits = IERC20Upgradeable(collateral[i].token).balanceOf(USSD) * ((amountToBuyLeftUSD * 1e18 / collateralval) / 1e18) / 1e18;
            IUSSD(USSD).UniV3SwapInput(collateral[i].pathsell, amountToSellUnits);
            amountToBuyLeftUSD -= (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
            DAItosell += (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
          } else {
  1. Incase collateralval < amountToBuyLeftUSD

https://github.com/sherlock-audit/2023-05-USSD/blob/main/ussd-contracts/contracts/USSDRebalancer.sol#L132-L138

This also can't guarantee (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore) < amountToBuyLeftUSD.

Solidity
          if (collateralval >= amountToBuyLeftUSD / 20) {
            uint256 amountBefore = IERC20Upgradeable(baseAsset).balanceOf(USSD);
            // sell all collateral and move to next one
            IUSSD(USSD).UniV3SwapInput(collateral[i].pathsell, IERC20Upgradeable(collateral[i].token).balanceOf(USSD));
            amountToBuyLeftUSD -= (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
            DAItosell += (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
          }

Impact

Rebalance process can revert caused by underflow calculation.

Code Snippet

https://github.com/sherlock-audit/2023-05-USSD/blob/main/ussd-contracts/contracts/USSDRebalancer.sol#L116-L125 https://github.com/sherlock-audit/2023-05-USSD/blob/main/ussd-contracts/contracts/USSDRebalancer.sol#L132-L138

Tool used

Manual Review

Recommendation

Check if (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore) > amountToBuyLeftUSD, in that case, just set amountToBuyLeftUSD to 0.

Solidity
          ...
            uint baseAssetChange = IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
            if (baseAssetChange > amountToBuyLeftUSD) {
                amountToBuyLeftUSD = 0;
            } else {
                amountToBuyLeftUSD -= baseAssetChange;
           }
            DAItosell += baseAssetChange;
          ...

Discussion

hrishibhat

This is a valid medium

The original report as published on Sherlock, by 0xHati. 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 the code assumes the swap output will never exceed the remaining amount to buy, but due to price differences it can, leading to an underflow when subtracting.

What the attacker needed

Collateral value exceeds amount to buy; swap returns more base asset than expected

The fix

The code should check if the received amount exceeds the remaining amount and set it to zero in that case, otherwise subtract.

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.

In a smart contract, numbers have a minimum value (usually zero). If you try to subtract a larger number from a smaller one, the operation 'underflows' and the transaction fails. Here, the contract subtracts the actual amount of stablecoin received from a swap from a planned amount, but sometimes the received amount is larger, causing the subtraction to underflow and the whole rebalance to revert.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by 0xHati

View all

4 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.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.