Skip to content
Business logic Audit finding Intermediate

User can possess less value than before when V2Migrator.migrate function is called to give up bathTokenV1 tokens and hold bathTokenV2 tokens

The V2Migrator.migrate function withdraws bathTokenV1 tokens, which incurs a fee, and then mints bathTokenV2 tokens using the reduced amount. As a result, the user receives less value after migration than they held before.

rbserver Follow with Arz, karanctf and 3 others

Originally disclosed on Code4rena

Submitted by rbserver, also found by Arz, karanctf, 0xnacho, 0xnev, and zaevlad.

Calling the following V2Migrator.migrate function executes uint256 amountWithdrawn = bathTokenV1.withdraw(bathBalance).

https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/V2Migrator.sol#L38-L74

    function migrate(IBathToken bathTokenV1) external {
        //////////////// V1 WITHDRAWAL ////////////////
        uint256 bathBalance = bathTokenV1.balanceOf(msg.sender);
        require(bathBalance > 0, "migrate: ZERO AMOUNT");

        /// @dev approve first
        bathTokenV1.transferFrom(msg.sender, address(this), bathBalance);

        // withdraw all tokens from the pool
        uint256 amountWithdrawn = bathTokenV1.withdraw(bathBalance);

        //////////////// V2 DEPOSIT ////////////////
        IERC20 underlying = bathTokenV1.underlyingToken();
        address bathTokenV2 = v1ToV2Pools[address(bathTokenV1)];

        underlying.approve(bathTokenV2, amountWithdrawn);
        require(
            CErc20Interface(bathTokenV2).mint(amountWithdrawn) == 0,
            "migrate: MINT FAILED"
        );
        /// @dev v2 bathTokens shouldn't be sent to this contract from anywhere other than this function
        IERC20(bathTokenV2).transfer(
            msg.sender,
            IERC20(bathTokenV2).balanceOf(address(this))
        );
        ...
    }

Then, calling bathTokenV1.withdraw(bathBalance) further calls the following BathTokenV1._withdraw function, which executes amountWithdrawn = r.sub(_fee) so amountWithdrawn has _fee deducted. When underlyingToken.safeTransfer(receiver, amountWithdrawn) is executed, such amountWithdrawn is transferred to the V2Migrator contract.

https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/periphery/BathTokenV1.sol#L532-L564

    function _withdraw(
        uint256 _shares,
        address receiver
    ) internal returns (uint256 amountWithdrawn) {
        uint256 r = (underlyingBalance().mul(_shares)).div(totalSupply);
        _burn(msg.sender, _shares);
        uint256 _fee = r.mul(feeBPS).div(10000);
        // If FeeTo == address(0) then the fee is effectively accrued by the pool
        if (feeTo != address(0)) {
            underlyingToken.safeTransfer(feeTo, _fee);
        }
        amountWithdrawn = r.sub(_fee);
        underlyingToken.safeTransfer(receiver, amountWithdrawn);
        ...
    }

Back in the V2Migrator.migrate function, amountWithdrawn that already has _fee deducted is used to call CErc20Interface(bathTokenV2).mint(amountWithdrawn), and the minted bathTokenV2 tokens are then transferred to the user. The whole migration process basically makes the user send their bathTokenV1 tokens in exchange of the bathTokenV2 tokens accordingly, so the value owned by the user before and after the migration should be the same. However, after deducting _fee from amountWithdrawn, which is used to mint the new bathTokenV2 tokens, has become less than the underlying token value that is equivalent to the user’s original bathTokenV1 token balance. Although the user should own the same value either holding the bathTokenV1 tokens before the migration or bathTokenV2 tokens after the migration, the user is forced to pay _fee and possess less value than before the V2Migrator.migrate function is called to give up bathTokenV1 tokens and hold bathTokenV2 tokens. Losing value because of the migration is unfair to the user.

Proof of Concept

The following steps can occur for the described scenario.

  1. The bathTokenV1 tokens owned by Alice are worth 1000 USDC.
  2. Alice calls the V2Migrator.migrate function to migrate her bathTokenV1 tokens to bathTokenV2 tokens.
  3. When the V2Migrator.migrate function is called, a fee of 10 USDC is transferred to feeTo so the bathTokenV2 tokens minted to Alice are worth only 990 USDC.
  4. As a result, Alice loses 10 USDC because of the migration, even though she should own the same value either holding the bathTokenV1 tokens before the migration or bathTokenV2 tokens after the migration.

Tools Used

VSCode

Recommended Mitigation Steps

The BathHouse contract corresponding to bathHouse used in the BathToken V1 proxy contract can be upgraded to allow its BathHouse.setBathTokenFeeBPS function, which its current implementation is shown below, to be additionally callable by the V2Migrator contract. Then, the V2Migrator.migrate function can be updated to call this BathHouse.setBathTokenFeeBPS function to change the BathToken V1 proxy contract’s feeBPS to 0 before calling bathTokenV1.withdraw(bathBalance) and restore the BathToken V1 proxy contract’s feeBPS to its original value after calling bathTokenV1.withdraw(bathBalance).

https://optimistic.etherscan.io/address/0x29da5213c75a1976452a27c8054e4c65ab0a3c53#code#F1#L337

    function setBathTokenFeeBPS(address bathToken, uint256 newBPS)
        external
        onlyAdmin
    {
        IBathToken(bathToken).setFeeBPS(newBPS);
    }

daoio (Rubicon) acknowledged


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

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

The migration function does not account for the withdrawal fee charged by the old token contract, so the new tokens are minted for a smaller amount.

Impact

The user loses the withdrawal fee amount, receiving less value after migration than before.

What the attacker needed

User holds bathTokenV1 tokens; feeBPS is greater than zero

The fix

The BathHouse contract should be upgraded to allow V2Migrator to set the feeBPS to 0 before withdrawal and restore it afterwards.

How it was found

  1. Alice holds bathTokenV1 tokens worth 1000 USDC.
  2. Alice calls V2Migrator.migrate to convert her bathTokenV1 tokens to bathTokenV2 tokens.
  3. A fee of 10 USDC is transferred to feeTo, so the minted bathTokenV2 tokens are worth only 990 USDC.
  4. Alice loses 10 USDC due to the migration.

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.

When you move your tokens from one version to another, you expect to keep the same value. Here, the old token charges a fee when you withdraw it, so you end up with less after the move. This means you lose money just by migrating.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by rbserver

View all

51 more disclosed reports, best first.

More from Rubicon v2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.