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
-
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.
- The closed asset
-
Possible Incorrect margin/collateral accounting
- Active positions will disappear from the
assetslist, leading to missed collateral contributions or skipped liabilities. - Zeroed positions will remain listed, disrupting accounting
- Active positions will disappear from the
-
Liquidation safety risk
- An undercollateralized account may avoid liquidation if an underwater position, causing asset is popped (cross margin).
-
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
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.