Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

nobody2018 - Attacker can manipulate the return value of USSDRebalancer.getOwnValuation for profit #252

Closed
sherlock-admin opened this issue May 23, 2023 · 0 comments
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label High A valid High severity issue Reward A payout will be made for this issue

Comments

@sherlock-admin
Copy link
Contributor

sherlock-admin commented May 23, 2023

nobody2018

high

Attacker can manipulate the return value of USSDRebalancer.getOwnValuation for profit

Summary

Anyone can call USSDRebalancer.rebalance. The execution path of USSDRebalancer.rebalance depends on the return value of USSDRebalancer.getOwnValuation. By flash loan, attacker pumps the price of collateral, and affects the return value of USSDRebalancer.getOwnValuation, then makes profit.

Vulnerability Detail

getOwnValuation uses sqrtPriceX96 returned by uniPool.slot0() to calculate own value. Below I will call it value.

function getOwnValuation() public view returns (uint256 price) {
      (uint160 sqrtPriceX96,,,,,,) =  uniPool.slot0();
      if(uniPool.token0() == USSD) {
        price = uint(sqrtPriceX96)*(uint(sqrtPriceX96))/(1e6) >> (96 * 2);
      } else {
        price = uint(sqrtPriceX96)*(uint(sqrtPriceX96))*(1e18 /* 1e12 + 1e6 decimal representation */) >> (96 * 2);
        // flip the fraction
        price = (1e24 / price) / 1e12;
      }
    }

sqrtPriceX96 can be manipulated to affect value returned by getOwnValuation.

Bob deploys an attack contract A. The whole attack is completed in one tx, the process is as follows:

  1. A initiates a flash loan to borrow a certain amount of DAI, which is divided into two parts, one part is used to affect uniPool (USSD-DAI), and the other part is used to buy various collateral tokens.

  2. A calls uniRouter.exactInput to buy USSD in uniPool, so as to achieve the goal of value > 1e6 + threshold.

  3. A calls uniRouter.exactInput (once for each collateral) to buy the collateral token in the specified pool for each collateral. This will pump the price of the collateral.

  4. A calls USSD.mintRebalancer to mint some amount of USSD token for USSD contract. This will make the return value of USSD.collateralFactor smaller. USSD.collateralFactor is used in [SellUSSDBuyCollateral](https://github.com/sherlock-audit/2023-05-USSD/blob/main/ussd-contracts/contracts/USSDRebalancer.sol#L178).

  5. A calls USSDRebalancer.rebalance, and the flow enters SellUSSDBuyCollateral() due to the operation of the second step. Internally, this function will swap all USSD token in the USSD contract to DAI, and then use all DAI to buy various collaterals. This will further pump the price of the collateral.

  6. A calls uniRouter.exactInput to sell all collateral tokens and USSD for profit.

  7. A repays flash loan.

On the contrary, you can also trigger the execution of BuyUSSSellCollateral to make a profit.

Impact

Attacker profits. The protocol suffers a funds loss.

Code Snippet

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

Tool used

Manual Review

Recommendation

Use time-weighted prices, similar to the implementation in UniswapV3StaticOracle.sol.

Duplicate of #451

@github-actions github-actions bot closed this as completed Jun 5, 2023
@github-actions github-actions bot added High A valid High severity issue Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label labels Jun 5, 2023
@sherlock-admin sherlock-admin added the Reward A payout will be made for this issue label Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label High A valid High severity issue Reward A payout will be made for this issue
Projects
None yet
Development

No branches or pull requests

1 participant