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

warRoom - Rollover users are being minted previous epoch amounts instead of entitled share amount. #229

Closed
sherlock-admin opened this issue Mar 27, 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 Mar 27, 2023

warRoom

high

Rollover users are being minted previous epoch amounts instead of entitled share amount.

Summary

When premium vault users who have won the epoch and also chose to roll over into the next epoch will loose their profit, as during mintRollover() call, their asset amount is being rollover to the next epoch instead of entitled shares.

Moreover, these users won't be able to call withdraw function as their assets are burnt during mintRollover() call.
Resulting in a loss of their profit

Vulnerability Detail

Where : In Carousel.sol- function mintRollover()
When : During mintRollover() execution.
Description :
  • Alice deposits a certain amount of assets in particular epoch X in the premium vault.
  • Alice enlists herself for rollover in rolloverQueue()
  • The premium side had won the epoch X.
  • Relayer/Bob calls the mintRollover() function.
    • During which Alice's entitleShares that are increased; should be transferred to the next epoch.
    • But instead, only the amount of assets Alice had enlisted are being rollover to epoch X+1.
  • Here entitlesShares is calculated on per pro-rata basis.
                uint256 entitledShares = previewWithdraw(
                    queue[index].epochId,
                    queue[index].assets
                );
  • But, here the amount of the assets get rollovered instead of entitledShares
                    uint256 assetsToMint = queue[index].assets - relayerFee;
                    _mintShares(queue[index].receiver, _epochId, assetsToMint);
  • When Alice tries to call withdraw() function for epoch X. She won't be able to claim her winnings as her assets are burnt.

POC

  • We have made changes to EndToEndCarouselTest file. Please update the file with following code.
  • To run the script : forge test --match-test testEndToEndCarousel -vv
pragma solidity 0.8.17;

import "../Helper.sol";
    import "../../../src/v2/TimeLock.sol";
    import "../../../src/v2/Carousel/CarouselFactory.sol";
import "../../../src/v2/interfaces/ICarousel.sol";
import "../../../src/v2/Carousel/Carousel.sol";
import "../../../src/v2/Controllers/ControllerPeggedAssetV2.sol";

contract EndToEndCarouselTest is Helper {
    using stdStorage for StdStorage;

    CarouselFactory public factory;
    ControllerPeggedAssetV2 public controller;

    address public emissionsToken;
    address public oracle;
    address public premium;
    address public collateral;

    uint256 public relayerFee;
    uint256 public depositFee;
    uint256 public strike;
    uint256 public marketId;
    uint256 public premiumEmissions;
    uint256 public collatEmissions;
    uint256 public epochId;
    uint256 public nextEpochId;
    uint256 public collateralQueueLength;
    uint256 public premiumQueueLength;
    uint256 public arbForkId;

    uint40 public begin;
    uint40 public end;
    uint40 public nextEpochBegin;
    uint40 public nextEpochEnd;
    address public constant USER3 = address(0xc);
    uint16 public fee;

    string public arbitrumRpcUrl = vm.envString("ARBITRUM_RPC_URL");

    function setUp() public {
        arbForkId = vm.createFork(arbitrumRpcUrl);
        vm.selectFork(arbForkId);

        emissionsToken = address(new MintableToken("Emissions Token", "EMT"));
        UNDERLYING = address(new MintableToken("UnderLyingToken", "utkn"));

        TimeLock timelock = new TimeLock(ADMIN);

        factory = new CarouselFactory(
            WETH,
            TREASURY,
            address(timelock),
            emissionsToken
        );

        controller = new ControllerPeggedAssetV2(address(factory), ARBITRUM_SEQUENCER, TREASURY);
        factory.whitelistController(address(controller));

        relayerFee = 2 gwei;
        depositFee = 50; // 0,5%

        //oracle = address(0x3);
        //strike = uint256(0x2);
        string memory name = string("USD Coin");
        string memory symbol = string("USDC");

        uint depegPrice = uint(2 ether);
        // deploy market
        (
            premium,
            collateral,
            marketId
        ) = factory.createNewCarouselMarket(
            CarouselFactory.CarouselMarketConfigurationCalldata(
                USDC_TOKEN,
                depegPrice  ,
                USDC_CHAINLINK,
                UNDERLYING,
                name,
                symbol,
                address(controller),
                relayerFee,
                depositFee)
        );

        // deploy epoch
        begin = uint40(block.timestamp - 5 days);
        end = uint40(block.timestamp - 3 days);
        fee = uint16(50); //0,5%
        premiumEmissions = 1000 ether;
        collatEmissions = 100 ether;

        // approve emissions token to factory
        vm.startPrank(TREASURY);

        MintableToken(emissionsToken).mint(address(TREASURY), 5000);
        MintableToken(emissionsToken).approve(address(factory),  5000 ether);

        vm.stopPrank();

       ( epochId, ) = factory.createEpochWithEmissions(
                marketId,
                begin,
                end,
                fee,
                premiumEmissions,
                collatEmissions
        );

        //deploy second epoch
        nextEpochBegin = uint40(block.timestamp - 10 hours);
        nextEpochEnd = uint40(block.timestamp - 5 hours);

        ( nextEpochId, ) = factory.createEpochWithEmissions(
                marketId,
                nextEpochBegin,
                nextEpochEnd,
                fee,
                premiumEmissions,
                collatEmissions
        );

        deal(UNDERLYING, USER, 20 ether, true);
        deal(UNDERLYING, USER2, 100 ether, true);
        deal(UNDERLYING, USER3, 200 ether, true);

    }

    function testEndToEndCarousel() public {
        vm.startPrank(USER);

        //warp to deposit period
       console.log("--------------------------------------------");
        console.log("Alice(USER) deposit in premium vault in epoch-1 : 2 ether");
       
        vm.warp(begin - 1 days);

        //approve ether deposit
        IERC20(UNDERLYING).approve(premium, 10 ether);
        IERC20(UNDERLYING).approve(collateral, 10 ether);

        //deposit in carousel vaults
        Carousel(premium).deposit(0, 2 ether, USER);
        // Carousel(collateral).deposit(0, 10 ether, USER);

        vm.stopPrank();
   
        vm.startPrank(USER2);

         //warp to deposit period
        vm.warp(begin - 1 days);

        //approve ether deposit
        IERC20(UNDERLYING).approve(collateral, 100 ether);
        IERC20(UNDERLYING).approve(premium,100 ether);
        //deposit in carousel vault
        Carousel(collateral).deposit(0, 20 ether, USER2);

        vm.stopPrank();
             vm.startPrank(USER3);
        vm.warp(begin - 1 days);

        //approve ether deposit
        IERC20(UNDERLYING).approve(premium, 100 ether);
        IERC20(UNDERLYING).approve(collateral, 100 ether);

        //deposit in carousel vaults
        Carousel(collateral).deposit(0, 20 ether, USER3);

        vm.stopPrank();
        
       console.log("--------------------------------------------");
        console.log("Other users deposit collateral vault : 40 ether");
    


        //warp to deposit period
        vm.warp(begin - 1 days);

        //assert queue length
        collateralQueueLength = 2;
        premiumQueueLength = 1;
        assertEq(Carousel(collateral).getDepositQueueLenght(), collateralQueueLength);
        assertEq(Carousel(premium).getDepositQueueLenght(), premiumQueueLength);

        //mint deposit in queue
        Carousel(collateral).mintDepositInQueue(epochId, collateralQueueLength);
        Carousel(premium).mintDepositInQueue(epochId, premiumQueueLength);

        vm.startPrank(USER);
 
        console.log("--------------------------------------------");
        console.log("Alice enlist in rollover with all of its assets");
    
        //enlist in rollover for next epoch
        console.log("Balance  of Alice before mint rollovers/before epoch end");
        console.log(Carousel(premium).balanceOf(USER,epochId));
        Carousel(premium).enlistInRollover(epochId, Carousel(premium).balanceOf(USER,epochId), USER);

        vm.stopPrank();

         vm.warp(begin + 1 hours);
    
        //trigger end epoch
        // controller.triggerEndEpoch(marketId, epochId);
        
        console.log("--------------------------------------------");
        console.log("Trigger depeg event occurs, premium vault wins");
    
        controller.triggerDepeg(marketId, epochId);
        console.log("Balance of Alice after epoch-1 end (Trigger depeg) before mint rollover");
        console.log(Carousel(premium).balanceOf(USER,epochId));
        uint entitledShares = Carousel(premium).previewWithdraw(epochId, Carousel(premium).balanceOf(USER,epochId));
        console.log("--------------------------------------------");
        console.log("Alice availibility to withdraw after epoch-1 before mint rollovers:");
      
        console.log(entitledShares);
        // //check vault balances on withdraw
        // assertEq(Carousel(premium).previewWithdraw(epochId, 12 ether), 0);
        // assertEq(Carousel(collateral).previewWithdraw(epochId, 20 ether), COLLATERAL_MINUS_FEES);

       console.log("--------------------------------------------");
        console.log("Relayer calls mint rollovers for next epoch");
 
        // let relayer rollover for users
        Carousel(premium).mintRollovers(nextEpochId,4);
        console.log("Balance of Alice after mint rollovers for epoch-1");
        console.log(Carousel(premium).balanceOf(USER,epochId));
        console.log("Entitled shares after mint rollovers for epoch-1");
        console.log(Carousel(premium).previewWithdraw(epochId, Carousel(premium).balanceOf(USER,epochId)));

        //assert rollover accounting
        assertEq(Carousel(premium).rolloverAccounting(nextEpochId), 1);
        assert(Carousel(premium).previewWithdraw(epochId, Carousel(premium).balanceOf(USER,epochId))==0);

        // ------------- EPOCH 2 -----------------
        
        console.log("----------------------------------------------------------------------------");
        console.log("Next Epoch Starts");
        console.log("Balance of Alice for next epoch-2");
        console.log(Carousel(premium).balanceOf(USER, nextEpochId));
        vm.startPrank(USER2);
        vm.warp(nextEpochBegin - 1 days);
        Carousel(premium).deposit(0,0.1 ether, USER2);
        Carousel(collateral).deposit(0, 2 ether, USER2);
        vm.stopPrank();
        vm.startPrank(USER3);
        vm.warp(nextEpochBegin -1 days);
        Carousel(collateral).deposit(0,2 ether, USER3);
        vm.stopPrank();

        vm.warp(nextEpochBegin - 1 days);

        collateralQueueLength = 2;
        premiumQueueLength = 1;
        assertEq(Carousel(collateral).getDepositQueueLenght(), collateralQueueLength);
        assertEq(Carousel(premium).getDepositQueueLenght(), premiumQueueLength);

        Carousel(collateral).mintDepositInQueue(nextEpochId, collateralQueueLength);
        Carousel(premium).mintDepositInQueue(nextEpochId, premiumQueueLength);

        vm.warp(nextEpochBegin + 1 hours);
    
        console.log("--------------------------------------------");
        console.log("Other users deposit collateral vault for epoch-2 : 4 ether(very less than epoch-1)");

        //trigger end epoch
        controller.triggerDepeg(marketId, nextEpochId);

        console.log("--------------------------------------------");
        console.log("Epoch-2 ends, premium vault wins again");

    //   uint entitledShares2 = Carousel(premium).previewWithdraw(nextEpochId, 1.8 ether)/10**18;
   uint entitledShares2 = Carousel(premium).previewWithdraw(nextEpochId, Carousel(premium).balanceOf(USER,nextEpochId));
     
        console.log("--------------------------------------------");
        console.log("Alice availibility to withdraw after epoch-2:");
        console.log(entitledShares2);
        console.log("--------------------------------------------");
        console.log("Loss of Alice premium rewards of epoch-1 which is greater than 30 ether:");
        console.log(entitledShares - entitledShares2);
        // console.log(entitledShares - entitledShares2);
        assert(entitledShares - entitledShares2 > 30 ether);
    }
}
  • Output:

Screenshot 2023-03-26 at 6 21 46 PM

Impact

  • Loss of profit for users

Code Snippet

https://github.com/sherlock-audit/2023-03-Y2K/blob/main/Earthquake/src/v2/Carousel/Carousel.sol#L395-L445

Tool used

Manual Review

Recommendation

Mint the (entiltledShares-relayerFee) for the next epoch in mintRollover() function.

Duplicate of #163

@github-actions github-actions bot closed this as completed Apr 3, 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 Apr 3, 2023
@sherlock-admin sherlock-admin added the Reward A payout will be made for this issue label Apr 11, 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