Skip to content
Business logic Audit finding Intermediate

KangarooVault.removeCollateral updates storage without actually removing collateral, resulting in lost collateral

The KangarooVault.removeCollateral function updates internal accounting without calling the exchange to actually remove collateral, so the collateral remains in the exchange but is no longer tracked. When the position is later closed, the vault receives less collateral than it should, causing a loss of funds.

joestakey Follow with juancito, auditor0517 and 5 others

Originally disclosed on Code4rena

Submitted by joestakey, also found by juancito, auditor0517, bin2chen, KIntern_NA, chaduke, Bauer, and 0x52

The admin can call KangarooVault.addCollateral to add additional collateral to a Power Perp position.

File: src/KangarooVault.sol
424:     function addCollateral(uint256 additionalCollateral) external requiresAuth nonReentrant {
425:         SUSD.safeApprove(address(EXCHANGE), additionalCollateral);
426:         EXCHANGE.addCollateral(positionData.positionId, additionalCollateral);
427:
428:         usedFunds += additionalCollateral;
429:         positionData.totalCollateral += additionalCollateral;
430:
431:         emit AddCollateral(positionData.positionId, additionalCollateral);
432:     }

This transfers SUSD to the EXCHANGE and updates the usedFunds and positionData.totalCollateral

The function KangarooVault.removeCollateral allows the admin to remove collateral if a position is healthy enough.

File: src/KangarooVault.sol
436:     function removeCollateral(uint256 collateralToRemove) external requiresAuth nonReentrant {
437:         (uint256 markPrice,) = LIQUIDITY_POOL.getMarkPrice();
438:     uint256 minColl = positionData.shortAmount.mulWadDown(markPrice);
439:         minColl = minColl.mulWadDown(collRatio);
440:
441:         require(positionData.totalCollateral >= minColl + collateralToRemove);
442:
443:         usedFunds -= collateralToRemove;
444:         positionData.totalCollateral -= collateralToRemove;
445:
446:         emit RemoveCollateral(positionData.positionId, collateralToRemove);
447:     }

The issue is that this function does not call EXCHANGE.removeCollateral. While it updates storage, it does not actually retrieve any collateral.

Impact

2 problems arising:

  • processWithdrawalQueue will revert unexpectedly, as usedFunds will be lower than it should, leading to availableFunds being greater than the real balance of SUSD available (KangarooVault.sol#L279).
  • the main problem: the amount of collateral “removed” in removeCollateral will be lost :

When closing a position in KangarooVault._closePosition, the amount of collateral to retrieve is written in tradeParams.collateralAmount. As you can see below, it is capped by positionData.totalCollateral, which was decremented in removeCollateral.

File: src/KangarooVault.sol
687:         if (amt >= positionData.shortAmount) {
688:             longPositionToClose = positionData.longPerp;
689:
690:             tradeParams.amount = positionData.shortAmount;
691:             tradeParams.collateralAmount = positionData.totalCollateral; //@audit here
692:         } else {
693:             longPositionToClose = amt.mulDivDown(positionData.longPerp, positionData.shortAmount);
694:             uint256 collateralToRemove = amt.mulDivDown(positionData.totalCollateral, positionData.shortAmount);
695:
696:             tradeParams.amount = amt;
697:             tradeParams.collateralAmount = collateralToRemove;
698:         }
699:
700:         SUSD.safeApprove(address(LIQUIDITY_POOL), maxCost);
701:         uint256 totalCost = EXCHANGE.closeTrade(tradeParams);

This is the amount of collateral that will be transferred back to the trader (here the KangarooVault)

src/Exchange.sol _closeTrade()
316: shortCollateral.sendCollateral(params.positionId, params.collateralAmount);
File: src/ShortCollateral.sol
106:     function sendCollateral(uint256 positionId, uint256 amount) external override onlyExchange nonReentrant {
107:         UserCollateral storage userCollateral = userCollaterals[positionId];
108:
109:         userCollateral.amount -= amount;
110:
111:         address user = shortToken.ownerOf(positionId);
112:
113:         ERC20(userCollateral.collateral).safeTransfer(user, amount); //@audit capped by `positionData.totalCollateral`

In conclusion, calling removeCollateral will result in that amount being lost.

Proof of Concept

Amend this test to KangarooVault.t.sol, which shows how collateral is not transferred upon calling removeCollateral().

429:     function testCollateralManagement() public {
430:         uint256 amt = 1e18;
431:         uint256 collDelta = 1000e18;
432:
433:         kangaroo.openPosition(amt, 0);
434:         skip(100);
435:         kangaroo.executePerpOrders(emptyData);
436:         kangaroo.clearPendingOpenOrders(0);
437:
438:         (,,,,,,, uint256 initialColl,) = kangaroo.positionData();
+439:         uint256 balanceBefore = susd.balanceOf(address(kangaroo));
440:
441:         kangaroo.addCollateral(collDelta);
+442:         uint256 balanceAfter = susd.balanceOf(address(kangaroo));
+443:         assertEq(collDelta, balanceBefore - balanceAfter);
444:         (,,,,,,, uint256 finalColl,) = kangaroo.positionData();
445:
446:         assertEq(finalColl, initialColl + collDelta);
447:
+448:         uint256 balanceBefore2 = susd.balanceOf(address(kangaroo));
449:         kangaroo.removeCollateral(collDelta);
+450:         uint256 balanceAfter2 = susd.balanceOf(address(kangaroo));
+451:         assertEq(0, balanceAfter2 - balanceBefore2); //@audit collateral not removed
452:
453:         (,,,,,,, uint256 newColl,) = kangaroo.positionData();
454:
455:         assertEq(newColl, initialColl);
456:     }

Tools Used

Manual Analysis, Foundry

Recommended Mitigation

Ensure Exchange.removeCollateral is called:

File: src/KangarooVault.sol
436:     function removeCollateral(uint256 collateralToRemove) external requiresAuth nonReentrant {
437:         (uint256 markPrice,) = LIQUIDITY_POOL.getMarkPrice();
438:         uint256 minColl = positionData.shortAmount.mulWadDown(markPrice);
439:         minColl = minColl.mulWadDown(collRatio);
440:
441:         require(positionData.totalCollateral >= minColl + collateralToRemove);
442:
443:         usedFunds -= collateralToRemove;
444:         positionData.totalCollateral -= collateralToRemove;
445:
+            EXCHANGE.removeCollateral(positionData.positionId, collateralToRemove)
446:         emit RemoveCollateral(positionData.positionId, collateralToRemove);
447:     }

mubaris (Polynomial) confirmed via duplicate issue #111

Dravee (judge) increased severity to High


The original report as published on Code4rena, by joestakey. 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 removeCollateral function updates internal state but omits the external call to the exchange to transfer the collateral out.

Impact

The collateral amount that was 'removed' is lost when the position is closed, and processWithdrawalQueue may revert due to incorrect availableFunds.

What the attacker needed

Admin privileges; position is healthy enough

The fix

The fix is to call EXCHANGE.removeCollateral to actually transfer the collateral out before updating storage.

How it was found

  1. Open a position and add collateral to the vault.
  2. Call removeCollateral with the same amount.
  3. Observe that the vault's SUSD balance does not change, but the recorded totalCollateral decreases.
  4. Close the position and see that the collateral returned is based on the reduced totalCollateral, resulting in a loss.

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 lets an admin add or remove collateral for a trading position. The remove function forgot to tell the exchange to send the collateral back, so the vault's records show less collateral than it actually has. Later, when the position is closed, the vault gets back only the reduced amount, and the difference is lost.

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 joestakey

View all

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