Skip to content
DoS Audit finding Intermediate

Possible DoS When calling GammaTradeMarket::_removePosition will cause user position to not be able to get liquidated

A malicious user can create many tiny positions so that removing a position requires looping over a large array, consuming excessive gas and potentially reverting liquidations. This could prevent other users from liquidating the attacker's positions.

web3km Follow with crypticdefense, nnez and 2 others

Originally disclosed on Code4rena

Submitted by web3km, also found by crypticdefense, nnez, 3n0ch, and pkqs90

https://github.com/code-423n4/2024-05-predy/blob/a9246db5f874a91fb71c296aac6a66902289306a/src/markets/gamma/ArrayLib.sol#L20-L32 https://github.com/code-423n4/2024-05-predy/blob/a9246db5f874a91fb71c296aac6a66902289306a/src/markets/gamma/GammaTradeMarket.sol#L146-L149

Impact

Griefing/DOS attack is possible when, a malicious user creates many very small positions, which could cause excessive gas consumed and even transactions reverted when other users are trying to liquidate any of the user’s positions.

Proof of Concept

The function GammaTradeMarket.sol:_removePosition is using the ArrayLib::removeItem, which is currently just looping over the items, until it finds the one it’s looking for.

function _removePosition(uint256 positionId) internal {x
        address trader = userPositions[positionId].owner;

@>        positionIDs[trader].removeItem(positionId);
    }
 function removeItem(uint256[] storage items, uint256 item) internal {
        uint256 index = getItemIndex(items, item);

        removeItemByIndex(items, index);
    }
...

    function getItemIndex(uint256[] memory items, uint256 item) internal pure returns (uint256) {
        uint256 index = type(uint256).max;

        //@review - If items length is bigger, it could revert due to reaching block gas limit
        for (uint256 i = 0; i < items.length; i++) {
            if (items[i] == item) {
                index = i;
                break;
            }
        }

        return index;
    }

This function is called multiple times inside of GammaTradeMarket.sol, it is called right after calling GammaTradeMarket::execLiquidationCall.

function execLiquidationCall(
        uint256 vaultId,
        uint256 closeRatio,
        IFillerMarket.SettlementParamsV3 memory settlementParams
    ) external override returns (IPredyPool.TradeResult memory tradeResult) {
        tradeResult =
            _predyPool.execLiquidationCall(vaultId, closeRatio, _getSettlementDataFromV3(settlementParams, msg.sender));

        if (closeRatio == 1e18) {
@>           _removePosition(vaultId);
        }
    }

A malicious user can create many really small positions with very little leverage to make sure those position will hardly be liquidated and the create, a normal position with a good amount of leverage. As a result every liquidator who tries to liquidate the user’s position will most likely fail, since the array’s length is too big and the position they want to liquidate is at the last place of the array.

Recommended Mitigation Steps

Consider using OZ’s EnumerableSet Library, which will allow to add/remove items easily without the need to loop over every item to find the one that needs to be removed.

Assessed type

DoS

syuhei176 (Predy) confirmed

0xsomeone (judge) commented via duplicate Issue #59:

A user can maliciously craft their positions in a way that would breach the block gas limit if a user were to iterate through all of them, thereby causing fillers to be unable to fulfill trades for the account and liquidators to be unable to iterate a user’s positions.

Given that liquidations can be affected as well, the present vulnerability permits a user to craft their position array in such a way that would prevent their liquidation. This misbehavior would usually result in a high-risk severity rating, however, as some submissions noted, this Denial-of-Service can be circumvented via partial liquidations up to the maximum accuracy possible which would not render it a viable strategy. As such, a medium-risk rating is better suited for both functionalities impacted.


The original report as published on Code4rena, by web3km. 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 linear search in ArrayLib::removeItem, which iterates over all position IDs, becomes prohibitively expensive when a user has many positions.

Impact

Liquidators may be unable to liquidate the attacker's positions due to high gas costs, though the judge noted this can be circumvented via partial liquidations, limiting the severity.

What the attacker needed

Attacker can create many positions; liquidator attempts full liquidation

How it was found

  1. Attacker creates many very small positions with low leverage.
  2. Attacker creates a normal position with good leverage.
  3. A liquidator tries to liquidate the attacker's position.
  4. The liquidation transaction consumes excessive gas or reverts because the position array is too long.

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.

In a smart contract, removing an item from a list often means checking each item one by one until you find the right one. If the list is very long, this takes more and more gas (the fee paid to run code). An attacker can make the list huge with tiny positions, so when someone tries to liquidate (force-close) a position, the transaction runs out of gas and fails.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by web3km

View all

1 more disclosed report, best first.

More from Predy

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.