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.
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.
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.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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
Deploy the test system and initialise the pool, exchange and vault.
Set the asset price and have the first user deposit funds into the vault.
Record the token price before opening a position.
Open a position so that trading fees are paid.
Record the new, lower token price.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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.