Skip to content
Business logic Audit finding Intermediate

First Depositors will incurr a rebalancing Loss

The vault's token price drops after a deposit is invested because trading fees are paid, so later depositors can buy vault tokens at a discount and receive more tokens for the same funds. The report claims this disadvantages early depositors, though the project disputed that the price drop is intended behaviour.

GalloDaSballo Follow

Originally disclosed on Code4rena

Submitted by GalloDaSballo

    function getTokenPrice() public view returns (uint256) {
        if (totalFunds == 0) {
            return 1e18;
        }

        uint256 totalSupply = getTotalSupply();
        if (positionData.positionId == 0) {
            return totalFunds.divWadDown(totalSupply);
        }

After a deposit, the funds are invested, doing so incurs a fee, which causes getTokenPrice to reduce, this offers a discount to later depositors.

Because the fee impacts the token price, future depositors will be able to purchase the Vault token at a discount.

This gives them an advantage as they’ll be receiving yield without incurring the additional cost.

Coded POC

The following POC is built by creating a new test file TestRebaseAttack.t.t.sol

The salient output from the test is the following

    ├─ emit Debug(name: tokenPrice, value: 1000000000000000000)
    ├─ emit Debug(name: newTokenPrice, value: 998684000000000000)

Which means that after creating the position to be delta neutral, due to fees being paid, the price of the token is lower, meaning it’s cheaper to deposit after openPosition instead of before.

Below the full code for you to verify:

Can be run with forge test --match-test testOpenRebaseAttack -vvvvv

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import {console2} from "forge-std/console2.sol";

import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {Exchange} from "../src/Exchange.sol";
import {TestSystem} from "./utils/TestSystem.sol";
import {KangarooVault} from "../src/KangarooVault.sol";
import {MockERC20Fail} from "../src/test-helpers/MockERC20Fail.sol";
import {LiquidityPool} from "../src/LiquidityPool.sol";
import {IPerpsV2MarketBaseTypes} from "../src/interfaces/synthetix/IPerpsV2MarketBaseTypes.sol";

contract TestRebaseAttack is TestSystem {
    using FixedPointMathLib for uint256;

    uint256 public constant initialPrice = 1200e18;

    Exchange private exchange;
    MockERC20Fail private susd;
    KangarooVault private kangaroo;
    LiquidityPool private pool;

    uint256 private leverage;
    uint256 private collRatio;

    bytes[] private emptyData;

    function setUp() public {
        deployTestSystem();
        initPool();
        initExchange();
        preparePool();
        setAssetPrice(initialPrice);
        initKangaroo();

        exchange = getExchange();
        susd = getSUSD();
        kangaroo = getKangaroo();
        pool = getPool();

        susd.mint(user_1, 5e23);

        vm.startPrank(user_1);
        susd.approve(address(kangaroo), 5e23);
        kangaroo.initiateDeposit(user_1, 1e23);
        vm.stopPrank();

        leverage = kangaroo.leverage();
        collRatio = kangaroo.collRatio();
    }

    event Debug(string name, uint256 value);

    function testOpenRebaseAttack() public {
        uint256 tokenPrice = kangaroo.getTokenPrice();
        assertEq(tokenPrice, 1e18, "!1e18");

        kangaroo.openPosition(1e19, 0);

        uint256 newTokenPrice = kangaroo.getTokenPrice();

        vm.startPrank(user_1);
        kangaroo.initiateDeposit(user_1, 1e18);
        vm.stopPrank();

        emit Debug("tokenPrice", tokenPrice);
        emit Debug("newTokenPrice", newTokenPrice);

        assertTrue(tokenPrice != newTokenPrice, "No change");
    }
}

Recommended Mitigation

It may be best to have a deposit or withdrawal fee that evens out the hedging costs as not to discourage early depositors.

mubaris (Polynomial) disputed and commented:

Token price is meant to go down after you open a position, that’s how it’s supposed to work.

Dravee (judge) commented:

Token price is meant to go down after you open a position, that’s how it’s supposed to work.

Very hard to argue with such an argument. If the warden can think of some counter-arguments in Post-Judging QA, I’ll be open to hear them.

GalloDaSballo (warden) commented:

Thank you for the opportunity to add more info. I believe the initial submission shows that the price for consecutive depositors is cheaper. For example if we let a big deposit happen, then we will get more tokens for less cost.

The updated tests simply has a second depositor instead of checking the price, you can see that if I deposit as the second person I get 5 times more tokens.

function testOpenRebaseAttack() public {
        uint256 tokenPrice = kangaroo.getTokenPrice();
        assertEq(tokenPrice, 1e18, "!1e18");

        kangaroo.openPosition(1e19, 0);

        uint256 newTokenPrice = kangaroo.getTokenPrice();

        vm.startPrank(user_2);
        kangaroo.initiateDeposit(user_2, 5e23);
        vm.stopPrank();

        kangaroo.processDepositQueue(1);


        emit Debug("tokenPrice", tokenPrice);
        emit Debug("newTokenPrice", newTokenPrice);
        emit Debug("VAULT_TOKEN.balanceOf(user_2)", VAULT_TOKEN.balanceOf(user_2));
        emit Debug("VAULT_TOKEN.balanceOf(user_1))", VAULT_TOKEN.balanceOf(user_1));

        assertTrue(tokenPrice != newTokenPrice, "No change");
        assertTrue(VAULT_TOKEN.balanceOf(user_2) > VAULT_TOKEN.balanceOf(user_1));
    }

Log shows that I get 5e23 tokens vs 1e23

    ├─ emit Debug(name: tokenPrice, value: 1000000000000000000)
    ├─ emit Debug(name: newTokenPrice, value: 998684000000000000)
    ├─ [542] VaultToken::balanceOf(0x53E2Cd188FE5E37D9bA9D267828B6be3975801D5) [staticcall]
    │   └─ ← 500658867069062886758974
    ├─ emit Debug(name: VAULT_TOKEN.balanceOf(user_2), value: 500658867069062886758974)
    ├─ [2542] VaultToken::balanceOf(0xe5ecc448cf264e5AB26D08C16e33263eF9E91676) [staticcall]
    │   └─ ← 100000000000000000000000
    ├─ emit Debug(name: VAULT_TOKEN.balanceOf(user_1)), value: 100000000000000000000000)
    ├─ [542] VaultToken::balanceOf(0xe5ecc448cf264e5AB26D08C16e33263eF9E91676) [staticcall]
    │   └─ ← 100000000000000000000000
    ├─ [542] VaultToken::balanceOf(0x53E2Cd188FE5E37D9bA9D267828B6be3975801D5) [staticcall]
    │   └─ ← 500658867069062886758974
    └─ ← ()

mubaris (Polynomial) acknowledged and commented:

What you’re saying is true, but token price reflects the price of each token against the total asset held by the vault. Once you pay fee for an action like trading, that fee is lost forever from the vault. If we start accounting that (some value that doesn’t exist), we’ll run in to accounting errors down the line. So it doesn’t make sense to add the fees paid by the vault in value held by the vault.

It is also true that, the second user gets much more tokens than the first user. But any funds added to the vault gets used for opening a new position and the user has stay in the vault until the new position becomes profitable to be profitable. There’s no immediate exit process here. We use the same mechanism for our options vaults which has been live for a while now.


The original report as published on Code4rena, by GalloDaSballo. 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

Likely the vault's share price is calculated from total funds divided by total supply, and trading fees reduce total funds without adjusting the share accounting, so new deposits buy shares more cheaply.

Impact

Later depositors could receive more vault tokens for the same funds, potentially disadvantaging early depositors, though the project argued the price drop is intended and that funds remain locked in the vault.

What the attacker needed

A deployed vault with a position; ability to deposit funds; trading fees are paid on position opening

The fix

The reporter suggested adding a deposit or withdrawal fee to offset hedging costs, but the project disputed the issue and no fix was confirmed.

How it was found

  1. Deploy the test system and initialise the pool, exchange and vault.
  2. Set the asset price and have the first user deposit funds into the vault.
  3. Record the token price before opening a position.
  4. Open a position so that trading fees are paid.
  5. Record the new, lower token price.
  6. Have a second user deposit and observe they receive more vault tokens than the first user for a similar amount.

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.

A vault token represents a share of the pool. When the vault pays a fee to open a trade, the pool's total value falls, so each share is worth less. Someone who deposits after that fee can buy more shares for the same money, which the reporter says is unfair to the first depositor.

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 GalloDaSballo

View all

30 more disclosed reports, best first.

More from Polynomial Protocol contest

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.