Skip to content
Business logic Audit finding Intermediate

Incorrect Removal Of Asset In UpdateAccount Due To Wrong Implementation in _movePop

The _movePop function in ClearingHouse.sol incorrectly removes the last element of the assets array instead of the intended asset when a position is closed, leaving the closed asset in place and removing an active one. This corrupts the account's asset list, potentially causing incorrect margin accounting, missed liquidations, and inaccessibility of funds.

SeveritySquad Follow with 0xTiwa, dreamcoder and 6 others

Originally disclosed on Code4rena

Submitted by SeveritySquad, also found by 0xTiwa, dreamcoder, jpmendes, kimnoic, NexusAudits, pfapostol, udogodwin, and valicera

perps/types/ClearingHouse.sol #L729-L736

A wrong implementation in the _movePop function causes the wrong element to be removed from the account’s asset list whenever a position is closed. Instead of removing the intended asset, _movePop removes the last element of the array while leaving the closed position’s asset in place.

This causes incorrect changes in the asset list (assets) and therefore causes still open positions to be removed from the contracts accounting, breaking a core accounting functionality of the protocol.

Every asset in the list corresponds to a position with amount > 0 and every position with amount > 0 must appear in the asset list for each subaccount.

The wrong implementation in _movePop is as follows:

       function _movePop(DynamicArrayLib.DynamicArray memory array, bytes32 asset) private pure {
        uint256 index = array.indexOf(asset);

if (index == type(uint256).max) return;

@>    array.set(index, asset);
        array.pop();
    }

Intended Behaviour: Swap the target element slot with the last element and pop the last element

Actual Behaviour: Leaves the target asset untouched and always pops the last element in the array

When updateAccount calls _movePop:

     @>         if (positions[positionIdx].amount == 0) _movePop(assets, tradedAsset);
        self.setAssets(account, subaccount, assets.length(), tradedAsset);

The wrong element is removed, and the corrupted assets list is then added to storage via setAssets.

Impact

  1. State discrepancies

    • The closed asset (amount == 0) remains in the account’s assets list.
    • A different asset (the last one) is removed, even if its position is still active.
  2. Possible Incorrect margin/collateral accounting

    • Active positions will disappear from the assets list, leading to missed collateral contributions or skipped liabilities.
    • Zeroed positions will remain listed, disrupting accounting
  3. Liquidation safety risk

    • An undercollateralized account may avoid liquidation if an underwater position, causing asset is popped (cross margin).
  4. Possible Inaccessibility of Funds

    • Assets with active balances but missing from the list will become unreachable by user or system operations.

Recommended mitigation steps

Fix the _movePop function to replace the target element for the last element of the array, and then pop the last element.

View detailed Proof of Concept


The original report as published on Code4rena, by SeveritySquad. 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 _movePop function sets the target index to the asset itself instead of swapping it with the last element before popping, so the last element is always removed.

Impact

This could cause incorrect margin and collateral accounting, allow undercollateralized accounts to avoid liquidation, and make active assets unreachable, though the text does not confirm actual exploitation.

What the attacker needed

A position is closed (amount becomes 0)

The fix

The _movePop function should be fixed to replace the target element with the last element of the array and then pop the last element.

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 a trader closes a position, the smart contract should remove that position's asset from a list. Instead, a coding mistake makes it remove a different asset, often one that is still active. This can mess up the contract's records of what a trader owns, which might lead to wrong calculations about their collateral or even prevent them from accessing funds.

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 SeveritySquad

View all

10 more disclosed reports, best first.

More from GTE Perps and Launchpad

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.